diff --git a/scripts/macmoji b/scripts/macmoji index ae3635e..4c56a96 100755 --- a/scripts/macmoji +++ b/scripts/macmoji @@ -4,15 +4,15 @@ cd "$(dirname "$(realpath "$0")")"; -DATE=`date +%s` -USER=`id -un` -USER_DICTIONARY_FOLDER=`ls -td -- ~/Library/Dictionaries/CoreDataUbiquitySupport/"${USER}"~*/UserDictionary/* | head -n 1` +DATE=$(date +%s) +USER=$(id -un) +USER_DICTIONARY_FOLDER=$(ls -td -- ~/Library/Dictionaries/CoreDataUbiquitySupport/"${USER}"~*/UserDictionary/* | head -n 1) USER_DICTIONARY="${USER_DICTIONARY_FOLDER}/store/UserDictionary.db" GLOBAL_PREFERENCES="/Users/${USER}/Library/Preferences/.GlobalPreferences.plist" PLIST_BUDDY="/usr/libexec/PlistBuddy" SQLITE="/usr/bin/sqlite3" EMOJI_SUBSTITUTIONS="../emoji substitutions.plist" -COUNT=`${PLIST_BUDDY} -c 'Print' "${EMOJI_SUBSTITUTIONS}" | grep 'Dict' | wc -l` +COUNT=$(${PLIST_BUDDY} -c 'Print' "${EMOJI_SUBSTITUTIONS}" | grep -c 'Dict') contains () { local e @@ -20,7 +20,7 @@ contains () { declare -a arr=("${@:3}") for e in "${arr[@]}"; do if [[ "$e" == "$1" ]]; then - if [[ "${arr[$(($i + 1))]}" == "$2" ]]; then + if [[ "${arr[$i + 1]}" == "$2" ]]; then return 0 fi fi @@ -32,20 +32,20 @@ contains () { install() { echo "Installing Macmoji text replacements..." - PK_LAST=`${SQLITE} "${USER_DICTIONARY}" "SELECT Z_PK FROM ZUSERDICTIONARYENTRY ORDER BY Z_PK DESC LIMIT 1;"` + PK_LAST=$(${SQLITE} "${USER_DICTIONARY}" "SELECT Z_PK FROM ZUSERDICTIONARYENTRY ORDER BY Z_PK DESC LIMIT 1;") - pk=$(($PK_LAST)) - cnt=`echo -e "${COUNT}" | tr -d '[[:space:]]'` + pk=$PK_LAST + cnt=$(echo -e "${COUNT}" | tr -d '[[:space:]]') plist='' sql='' - while [ $cnt -gt 0 ]; do - let cnt-=1 - let pk+=1 - replace=`${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}"` - with=`${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}"` + while [ "${cnt}" -gt 0 ]; do + ((cnt-=1)) + ((pk+=1)) + replace=$(${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}") + with=$(${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}") plist+="{on=1;replace=\"${replace}\";with=\"${with}\";}," - sql+="INSERT INTO 'ZUSERDICTIONARYENTRY' VALUES($((${pk})),1,1,0,0,0,0,${DATE},NULL,NULL,NULL,NULL,NULL,\"${with}\",\"${replace}\",NULL);" + sql+="INSERT INTO 'ZUSERDICTIONARYENTRY' VALUES(${pk},1,1,0,0,0,0,${DATE},NULL,NULL,NULL,NULL,NULL,\"${with}\",\"${replace}\",NULL);" done $SQLITE "${USER_DICTIONARY}" "${sql}" @@ -56,28 +56,28 @@ uninstall() { echo "Uninstalling Macmoji text replacements..." seen=() - cnt=`echo -e "${COUNT}" | tr -d '[[:space:]]'` + cnt=$(echo -e "${COUNT}" | tr -d '[[:space:]]') - while [ $cnt -gt 0 ]; do - let cnt-=1 - let pk+=1 - replace=`${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}"` - with=`${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}"` + while [ "${cnt}" -gt 0 ]; do + ((cnt-=1)) + ((pk+=1)) + replace=$(${PLIST_BUDDY} -c "Print :${cnt}:shortcut" "${EMOJI_SUBSTITUTIONS}") + with=$(${PLIST_BUDDY} -c "Print :${cnt}:phrase" "${EMOJI_SUBSTITUTIONS}") arr=("$replace" "$with") seen+=("${arr[@]}") sql+="DELETE FROM 'ZUSERDICTIONARYENTRY' WHERE ZPHRASE = \"${with}\" AND ZSHORTCUT = \"${replace}\";" done $SQLITE "${USER_DICTIONARY}" "${sql}" - EXISTING_COUNT=`${PLIST_BUDDY} -c 'Print :NSUserDictionaryReplacementItems' "${GLOBAL_PREFERENCES}" | grep 'Dict' | wc -l` - existing_cnt=`echo -e "${EXISTING_COUNT}" | tr -d '[[:space:]]'` + EXISTING_COUNT=$(${PLIST_BUDDY} -c 'Print :NSUserDictionaryReplacementItems' "${GLOBAL_PREFERENCES}" | grep -c 'Dict') + existing_cnt=$(echo -e "${EXISTING_COUNT}" | tr -d '[[:space:]]') - while [ $existing_cnt -gt 0 ]; do - let existing_cnt-=1 - replace=`${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$(($existing_cnt)):replace" "${GLOBAL_PREFERENCES}"` - with=`${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$(($existing_cnt)):with" "${GLOBAL_PREFERENCES}"` - if contains $replace $with "${seen[@]}"; then - ${PLIST_BUDDY} -c "Delete :NSUserDictionaryReplacementItems:$(($existing_cnt))" "${GLOBAL_PREFERENCES}" + while [ "$existing_cnt" -gt 0 ]; do + ((existing_cnt-=1)) + replace=$(${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$existing_cnt:replace" "${GLOBAL_PREFERENCES}") + with=$(${PLIST_BUDDY} -c "Print :NSUserDictionaryReplacementItems:$existing_cnt:with" "${GLOBAL_PREFERENCES}") + if contains "$replace" "$with" "${seen[@]}"; then + ${PLIST_BUDDY} -c "Delete :NSUserDictionaryReplacementItems:$existing_cnt" "${GLOBAL_PREFERENCES}" fi done }