AppleScript处理快捷键列表添加+分隔符及特殊字符替换问题求助
AppleScript 完整实现方案
-- 配置项:修饰符符号与对应文本的映射,可按需调整 set modifierMap to {⇧:"shift", ⌃:"control", ⌥:"command", ⌫:"delete"} -- 配置项:不需要拆分的多字符按键名,可按需新增 set reservedKeys to {"Space", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12"} -- 你的原始快捷键列表 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 processedShortcutList to {} set oldDelimiters to AppleScript's text item delimiters repeat with eachHotkey in hotkeyShortcutList set currentKey to contents of eachHotkey -- 多字符按键保护:匹配到保留词直接加入结果,不拆分 if reservedKeys contains currentKey then set end of processedShortcutList to currentKey else set keyParts to {} -- 遍历单个字符,替换修饰符 repeat with i from 1 to count of characters of currentKey set char to character i of currentKey as text try -- 匹配到修饰符则替换为对应文本 set mappedValue to (modifierMap's (char as constant)) set end of keyParts to mappedValue on error -- 普通字符直接保留 set end of keyParts to char end try end repeat -- 用+拼接所有按键片段 set AppleScript's text item delimiters to "+" set processedKey to keyParts as text set end of processedShortcutList to processedKey set AppleScript's text item delimiters to oldDelimiters end if end repeat -- 恢复默认分隔符,避免影响后续脚本运行 set AppleScript's text item delimiters to oldDelimiters return processedShortcutList
效果说明
- 输入
⌃⌥B会输出control+command+B,完全符合格式要求 Space、F1等多字符按键会直接保留,不会被拆分- 完全使用AppleScript原生能力实现,无shell调用,不存在Unicode字符解析错误问题
内容的提问来源于stack exchange,提问作者Baldrian Sector
相关产品推荐
相关产品推荐

