此shell脚本允许以编程方式在GlobalPreferences.plist
和UserDictionary.db
上保存macOS的文本替换。
date=$(date +%s)
while read -r replace with; do
plist+="{on=1;replace=\"$replace\";with=\"$with\";},"
sql+="INSERT INTO 'ZUSERDICTIONARYENTRY' VALUES($((++i)),1,1,0,0,0,0,$date,NULL,NULL,NULL,NULL,NULL,\"$with\",\"$replace\",NULL);"
done < <(sed 's/\\/\\\\/g;s/"/\\"/g' ~/replacements.txt)
sqlite3 ~/Library/Dictionaries/CoreDataUbiquitySupport/$USER~*/UserDictionary/local/store/UserDictionary.db "delete from ZUSERDICTIONARYENTRY;$sql"
defaults write -g NSUserDictionaryReplacementItems "(${plist%?})"
如何自定义它以便它可以读取先前声明的数组而不是~/replacements.txt
外部文件?
答案 0 :(得分:2)
假设(这些是大而无根据的假设,由于问题的模糊性而必需),你的数组每行输入文件包含一个条目:
替换
sed 's/\\/\\\\/g;s/"/\\"/g' ~/replacements.txt
使用:
printf '%s\n' "${replacements[@]}" | sed 's/\\/\\\\/g;s/"/\\"/g'
要清楚,这是假设一个数字索引的数组。例如,您可以使用以下代码从replacements.txt
生成此类数组:
# in bash 4.0 or newer
mapfile -t replacements <~/replacements.txt
...或:
# in bash 3.x or newer
replacements=( )
while IFS= read -r line; do replacements+=( "$line" ); done <~/replacements.txt