如何在AppleScript中对列表每一项执行文本查找替换操作
AppleScript 列表快捷键符号批量替换方案
你已有的单字符串查找替换handler可以直接复用,只需补充列表遍历、批量规则替换的逻辑即可,完整可运行代码如下:
-- 你原有可用的文本替换handler on findAndReplaceInText(theText, theSearchString, theReplacementString) set AppleScript's text item delimiters to theSearchString set theTextItems to every text item of theText set AppleScript's text item delimiters to theReplacementString set theText to theTextItems as string set AppleScript's text item delimiters to "" return theText end findAndReplaceInText -- 主逻辑 on run -- 你的原快捷键列表 set hotkeyShortcutList to {"$", "U", "J", "G", "R", "⇧+R", "⇧+Y", "⇧+G", "⇧+B", "⇧+P", "⇧+⌫", "⌃+M", "⌃+W", "⌃+S", "⌃+X", "⌃+C", "⌃+V", "⌃+N", "⇧+⌃+N", "⌃+U", "⌃+B", "⇧+⌃+A", "⌃+A", "⌥+I", "⌥+O", "⇧+⌥+I", "⇧+⌥+O", "⌥+B", "⌥+D", "⌥+S", "⌃+⌥+M", "⌃+⌥+B", "⌃+⌥+X", "⇧+⌃+G", "⇧+⌃+⌥+R", "⇧+⌃+⌥+L", "⌃+Å", "⌃+]", "⇧+⌃+Å", "⇧+⌃+}", "⇧+⌃+M", "⇧+⌃+⌥+!", "⇧+⌃+⌥+@", "⇧+⌃+⌥+£", "⇧+⌃+⌥+$", "⇧+⌃+⌥+%", "⇧+⌃+⌥+^", "⌃+1", "⌃+2", "⌃+3", "⌃+4", "⌃+5", "⌃+6", "⇧+⌃+!", "⇧+⌃+\"", "⇧+⌃+#", "⇧+⌃+€", "⇧+⌃+%", "⇧+⌃+&", "K", "⌃+K", "⌃+V", "⇧+⌃+⌥+K", "A", "Y", "Z", "⇧+⌃+*", "⇧+⌃+⌥+*", "X", "⌃+,", "⌃+.", "⇧+⌃+;", "⇧+⌃+:", "⌃+P", "⇧+⌃+)", "⇧+⌃+?", "⌃++", "Space", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "F"} -- 替换规则,后续新增规则直接按{待替换符号, 目标文本}格式追加即可 set replacementRules to {¬ {"⇧", "shift"}, ¬ {"⌃", "control"}, ¬ {"⌥", "option"}, ¬ {"⌘", "command"}, ¬ {"⌫", "backspace"}, ¬ {"→", "arrow right"} ¬ } set processedShortcutList to {} -- 遍历原列表每一项 repeat with eachShortcut in hotkeyShortcutList set currentShortcut to eachShortcut as string -- 遍历所有替换规则,逐个替换当前快捷键的符号 repeat with eachRule in replacementRules set currentShortcut to findAndReplaceInText(currentShortcut, item 1 of eachRule, item 2 of eachRule) end repeat -- 处理完成后加入结果列表 set end of processedShortcutList to currentShortcut end repeat -- 可根据需要返回或打印结果 return processedShortcutList end run
效果示例
替换前后对应效果如下:
- 原
⇧+⌃+N→ 替换后shift+control+N - 原
⇧+⌫→ 替换后shift+backspace - 原
⇧+⌃+⌥+R→ 替换后shift+control+option+R
如果需要调整分隔符(比如把+替换为空格),直接在replacementRules追加{"+", " "}即可,无需修改其他逻辑。
内容的提问来源于stack exchange,提问作者Baldrian Sector
相关产品推荐
相关产品推荐

