anion155

Script for adding menus to unity quicklists

Mar 9th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.95 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ##
  4. # Author: Mikhaylov Anton aka anion155 <[email protected]>
  5. ##
  6.  
  7. ## Usage string
  8. USAGE="Usage: $0 [-h] [-c] [-d] [-t] [-r] -f <launcher_name> [-o <order_of_actions>] -- <Actions>
  9.  
  10.     -f <launcher_name>      Name of the launcher to change, can be with out expansion
  11.     -o <order_of_actions>   Order of actions wich must be separated by ';', ' ' or ','. Example:
  12.         'FirstItem SecondItem;ThirdItem/ForthItem'.
  13.     <Actions>               String for every adding action.
  14.         Example:
  15.             'ActionName\\Name=New Action\\Name[ru_RU]=Новое действие\\Exec=whoiam'
  16.         Format:
  17.             After action name follow all lines you want. 'OnlyShowIn=Unity;' isn't neccecery line.
  18.             '\\' is symbol of the new line.
  19.     -c Delete all actions before adding.
  20.     -d Copy file from /usr/share/applications/ folder.
  21.     -t Test launch. Print result in stdout insteed launcher.
  22.     -r Restore backup.
  23.     -h This prompt \n"
  24.  
  25. ## Messages
  26. ERROR="$0: Error:"
  27. WARNING="$0: Warning:"
  28.  
  29. ## System vars
  30. SYS_FOLDER='/usr/share/applications'
  31. USER_FOLDER="$HOME/.local/share/applications"
  32.  
  33. ## Launcher name
  34. declare LAUNCHER_NAME
  35.  
  36. ## Source file
  37. declare SOURCE
  38.  
  39. ## Target file
  40. declare TARGET
  41.  
  42. ## Backup file
  43. declare BACKUP
  44.  
  45. ## Silence of the script
  46. SILENCE='/dev/null'
  47.  
  48. ## Clean file from other actions
  49. CLEAN_FILE=false
  50.  
  51. ## Copy file from /usr/share/applications
  52. FORCE_USE_FROM_SYS=false
  53.  
  54. ## Print result in stdout insteed real file
  55. TEST=false
  56.  
  57. ## Restore from backup
  58. RESTORE=false
  59.  
  60. ## Old actions
  61. declare -a OLD_ORDER
  62. declare -a OLD_ACTION_NAME
  63. declare OLD_ACTIONS_CONTENT
  64.  
  65. ## Added actions
  66. declare -a ORDER
  67. declare -a ACTION_NAME
  68. declare -a ACTION_CONTENT
  69. declare ACTIONS_COUNT
  70.  
  71. ## Options check and parse
  72. while getopts ":chf:dto:r" opt; do
  73.     case "$opt" in
  74.         "h") echo -e "$USAGE"; exit 0;;
  75.         "f")
  76.             OPTARG="$(basename $OPTARG)"
  77.             if [ ${OPTARG:${#OPTARG}-8} == '.desktop' ]; then
  78.                 OPTARG=${OPTARG:0:${#OPTARG}-8}
  79.             fi
  80.             if [ -f "$USER_FOLDER/$OPTARG.desktop" ]; then
  81.                 SOURCE="$USER_FOLDER"
  82.             elif [ -f "$SYS_FOLDER/$OPTARG.desktop" ]; then
  83.                 SOURCE="$SYS_FOLDER"
  84.             else
  85.                 echo "$ERROR Invalid launcher name $OPTARG. Launcher must be in $SYS_FOLDER or in $USER_FOLDER folder!"
  86.                 exit 1
  87.             fi
  88.             LAUNCHER_NAME="$OPTARG"
  89.             ;;
  90.         "o") ORDER=( `echo $OPTARG | sed -e 's/,/ /g;s/;/ /g'` );;
  91.         "c") CLEAN_FILE=true;;
  92.         "d") FORCE_USE_FROM_SYS=true;;
  93.         "r") RESTORE=true;;
  94.         "t") TEST=true;;
  95.         "?") echo "$ERROR Invalid option $OPTARG. Try '$0 -h' for help"; exit 1;;
  96.         *) echo "$ERROR Unknown error while processing options"; exit 1;;
  97.     esac
  98. done
  99. i=$OPTIND
  100. index=0
  101. while [ $i -le $# ]; do
  102.     action="${!i}"
  103.     ACTION_NAME[$index]="`echo $action | cut -d\\\\ -f1`"
  104.     k=0
  105.     while [ $k -lt $index ]; do
  106.         if [[ "${ACTION_NAME[$k]}" == "${ACTION_NAME[$index]}" ]]; then
  107.             echo "$ERROR Action initialization should be carried out only once!"
  108.             exit 1
  109.         fi
  110.         ((k++))
  111.     done
  112.     ACTION_CONTENT[$index]="`echo $action | cut -d\\\\ -f2- | sed -e 's/\\\\/\n/g'`"
  113.     ((i++))
  114.     ((index++))
  115. done
  116. ACTIONS_COUNT=$index
  117. unset i
  118. unset index
  119. unset k
  120.  
  121. ## Invalid arguments
  122. if ! ( $TEST || ( $RESTORE && [ "$LAUNCHER_NAME" ] ) || [ "$SOURCE" ] ); then
  123.     echo "$ERROR Invalid options. Try '$0 -h' for help"
  124.     exit 1
  125. fi
  126.  
  127. ## Restore backup
  128. if $RESTORE; then
  129.     if [ -f "$USER_FOLDER/$LAUNCHER_NAME.desktop" ]; then
  130.         cp "$USER_FOLDER/$LAUNCHER_NAME.desktop"{'.backup',}
  131.         exit 0
  132.     else
  133.         echo "$WARNING Backup for $LAUNCHER_NAME made by this script doesn't exist."
  134.         exit 2
  135.     fi
  136. fi
  137.  
  138. ## Initiate target
  139. if $TEST; then
  140.     SILENCE='1'
  141. else
  142.     ## Make new file, if $SOURCE in root directory, and backup file
  143.     if [ -f "$USER_FOLDER/$LAUNCHER_NAME.desktop" ]; then
  144.         cp "$USER_FOLDER/$LAUNCHER_NAME.desktop"{,'.backup'}
  145.     fi
  146.     if $FORCE_USE_FROM_SYS || [[ "$SOURCE" == "$SYS_FOLDER" ]]; then
  147.         cp {"$SYS_FOLDER","$USER_FOLDER"}"/$LAUNCHER_NAME.desktop"
  148.     fi
  149.     ## Target
  150.     TARGET="$USER_FOLDER/$LAUNCHER_NAME.desktop"
  151. fi
  152.  
  153. if ! ( $CLEAN_FILE || $TEST ) && [ "$SOURCE" ]; then
  154.     ## Initializade file
  155.     OLD_ORDER=( `cat "$TARGET" | sed -e '/^Actions=/!d' -e 's/^Actions=//' -e 's/\;/ /'` )
  156.     OLD_ACTION_NAME=( `cat "$TARGET" | sed -e '/^\[Desktop Action /!d' | cut -d\  -f3 | sed -e 's/\]//'` )
  157.     OLD_ACTIONS_CONTENT="`cat "$TARGET" | awk '/^\[[^\]]+\]/{a=0}/^\[Desktop Action*/{a=1}{if(a)print}'`"
  158.     i=0
  159.     while [ $i -lt $ACTIONS_COUNT ]; do
  160.         k=0
  161.         while [ $k -lt ${#OLD_ACTION_NAME} ]; do
  162.             if [[ "${ACTION_NAME[$i]}" == "${OLD_ACTION_NAME[$k]}" ]]; then
  163.                 echo "$ERROR Action initialization should be carried out only once!"
  164.                 exit 1
  165.             fi
  166.             ((k++))
  167.         done
  168.         ((i++))
  169.     done
  170.     unset i
  171.     unset k
  172. fi
  173.  
  174. if ! $TEST; then
  175.     ## Clean up file from actions
  176.     sed -e '/^Actions=/d' "$TARGET" --in-place
  177.     awk '/^\[[^\]]+\]/{a=0}/^\[Desktop Action*/{a=1}{if(!a)print}' "$TARGET" | tee "$TARGET" >/dev/null
  178. fi
  179.  
  180. ## Echo actions order
  181. echo -e -n '\nActions=' | tee -a "$TARGET" 2>/dev/null >&$SILENCE
  182. if ! $TEST; then
  183.     i=0
  184.     while [ $i -lt ${#OLD_ORDER[@]} ]; do
  185.         echo -n "${OLD_ORDER[$i]};" | tee -a "$TARGET" 2>/dev/null >&$SILENCE
  186.         ((i++))
  187.     done
  188. fi
  189. i=0
  190. if [ "$ORDER" ]; then
  191.     buf=( ${ORDER[@]} )
  192. else
  193.     buf=( ${ACTION_NAME[@]} )
  194. fi
  195. while [ $i -lt ${#buf[@]} ]; do
  196.     echo -n "${buf[$i]};" | tee -a "$TARGET" 2>/dev/null >&$SILENCE
  197.     ((i++))
  198. done
  199. echo -e -n "\n" | tee -a "$TARGET" 2>/dev/null >&$SILENCE
  200. unset buf
  201.  
  202. ## Echo actions content
  203. if ! $TEST; then
  204.     echo -e -n "\n$OLD_ACTIONS_CONTENT" | tee -a "$TARGET" 2>/dev/null >&$SILENCE
  205. fi
  206. i=0
  207. while [ $i -lt $ACTIONS_COUNT ]; do
  208.     NAMES="`echo -e "${ACTION_CONTENT[$i]}" | sed -e '/^Name/!d'`"
  209.     EXEC="`echo -e "${ACTION_CONTENT[$i]}" | sed -e '/Exec=/!d'`"
  210.     ONLYSHOWIN="`echo -e "${ACTION_CONTENT[$i]}" | sed -e '/^OnlyShowIn=/!d'`"
  211.     if ! [ $ONLYSHOWIN ]; then
  212.         ONLYSHOWIN='OnlyShowIn=Unity;'
  213.     fi
  214.     echo -e -n "\n[Desktop Action ${ACTION_NAME[$i]}]\n$NAMES\n$EXEC\n$ONLYSHOWIN\n" | tee -a "$TARGET" 2>/dev/null >&$SILENCE
  215.     ((i++))
  216. done
  217. unset i
  218. unset NAMES
  219. unset EXEC
  220. unset ONLYSHOWIN
  221.  
  222. if ! $TEST; then
  223.     desktop-file-validate "$TARGET"
  224. fi
Advertisement
Add Comment
Please, Sign In to add comment