首页 > 解决方案 > 在 AppleScript 列表中的每个字符之间添加“+”字符

问题描述

我正在尝试使用“+”字符编辑快捷键列表和单独的键。

所以“⌃⌥B”变成“⌃+⌥+B”等等

到目前为止,我已经能够将我的快捷方式提取到一个名为“hotkeyShortcutList”的列表中。

我的示例 hotkeyShortcutList 列表如下所示:

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 {}
repeat with i from 1 to length of hotkeyShortcutList
set theCurrentListItem to item i of hotkeyShortcutList
set processedListItem to (do shell script ("<<<" & theCurrentListItem & " sed -E 's/.{1}/&+/g ; s/-$//'"))
set processedListItem to characters 1 thru -2 of processedListItem as text
set end of processedShortcutList to processedListItem
end repeat
return processedShortcutList

但是我有3个问题:

  1. shell 脚本似乎适用于常规字符,但在尝试处理“⇧”、“⌃”等特殊字符时会出错。和“⌥”。我对 shell 脚本的理解非常有限,但这似乎是我可以通过谷歌进行文本处理的方式......

  2. 添加“+”后,我需要用文本替换特殊字符。⇧替换为“shift”,⌃替换为“control”,⌥替换为“command”,⌥替换为“command”等。

  3. 我想为具有多个字母的键添加一些边缘情况保护,例如空格键的“空格”和 F 键的“F1”,所以我不会以“s+p+a+c+e”结尾和“F+1”。

我知道 AppleScript 可能不是最简单的方法,但我将使用 AppleScript 中的值,所以我觉得将它们放在一起会很好。有什么建议么?

标签: pythonshellapplescript

解决方案


How about just replacing the special characters with their descriptive word and appending a plus sign — all in one go? As you have a modest number of odd characters, this has the benefit of obviousness as well as making it easy to add or edit substitutions.

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", "⌃Å&quot;, "⌃]", "⇧⌃Å", "⇧⌃}", "⇧⌃M", "⇧⌃⌥!", "⇧⌃⌥@", "⇧⌃⌥£&quot;, "⇧⌃⌥$", "⇧⌃⌥%", "⇧⌃⌥^", "⌃1", "⌃2", "⌃3", "⌃4", "⌃5", "⌃6", "⇧⌃!", "⇧⌃\"", "⇧⌃#", "⇧⌃€&quot;, "⇧⌃%", "⇧⌃&", "K", "⌃K", "⌃V", "⇧⌃⌥K", "A", "Y", "Z", "⇧⌃*", "⇧⌃⌥*", "X", "⌃,", "⌃.", "⇧⌃;", "⇧⌃:", "⌃P", "⇧⌃)", "⇧⌃?", "⌃+", "Space", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "F"}

set newList to {}
repeat with hotkey in hotkeyShortcutList
    do shell script "echo '" & hotkey & "' | sed -e 's/⌃/control+/g' -e 's/⌥/option+/g' -e 's/⇧/shift+/g' -e 's/⌫/backspace+/g'"
    set end of newList to result
end repeat
newList

NB You have some odd characters in your source text. For example, your 'control' character is actually an 'up arrowhead' (U+2303). If your text ever has the more conventional 'circumflex accent' (U+005E) then you will need to edit the sed parameter.

The above generates the following result for me with your source list. Let me know if this is the desired output.

{"$", "U", "J", "G", "R", "shift+R", "shift+Y", "shift+G", "shift+B", "shift+P", "shift+backspace+", "control+M", "control+W", "control+S", "control+X", "control+C", "control+V", "control+N", "shift+control+N", "control+U", "control+B", "shift+control+A", "control+A", "option+I", "option+O", "shift+option+I", "shift+option+O", "option+B", "option+D", "option+S", "control+option+M", "control+option+B", "control+option+X", "shift+control+G", "shift+control+option+R", "shift+control+option+L", "control+Å", "control+]", "shift+control+Å", "shift+control+}", "shift+control+M", "shift+control+option+!", "shift+control+option+@", "shift+control+option+£", "shift+control+option+$", "shift+control+option+%", "shift+control+option+^", "control+1", "control+2", "control+3", "control+4", "control+5", "control+6", "shift+control+!", "shift+control+\"", "shift+control+#", "shift+control+€&quot;, "shift+control+%", "shift+control+&", "K", "control+K", "control+V", "shift+control+option+K", "A", "Y", "Z", "shift+control+*", "shift+control+option+*", "X", "control+,", "control+.", "shift+control+;", "shift+control+:", "control+P", "shift+control+)", "shift+control+?", "control++", "Space", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "F"}

推荐阅读