在AppleScript列表中的每个字符之间添加"+"字符



我正在尝试编辑一个快捷键列表,并使用"+"性格。

"⌃⌥B"成为"⌃+⌥+ B"等等

到目前为止,我已经能够将我的快捷键提取到一个名为"热键快捷列表"的列表中。

我的示例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

但是我有三个问题:

  1. shell脚本似乎可以处理常规字符,但是当试图处理特殊字符时,如" ", "和"⌥"。我对shell脚本的理解非常有限,但它似乎是我可以谷歌文本处理的方式…

  2. 我需要在添加了&;+&;之后用文本替换特殊字符。⌘替换为"shift", ctrl替换为"control", ctrl替换为"command"等。

  3. 我想添加一些边缘情况保护键有多个字母,如&;空格&;空格和"F1"对于f键,所以我不会以&;s+p+a+c+e&;和"F + 1"。

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

这是一个比其他答案更快的方法,因为它不需要在repeat循环中运行,因为它强制列表换行分隔的文本处理它与单个do shell script命令sed,然后强制do shell script命令返回到列表。

Script Debugger中进行测试以下示例AppleScript代码耗时0.01秒,而来自red_menace的答案中的代码耗时0.05秒,来自Mockman的答案中的代码耗时0.93秒。

AppleScript<代码/em>:

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 AppleScript's text item delimiters to linefeed
set foo to hotkeyShortcutList as text
set AppleScript's text item delimiters to {}
set processedShortcutList to paragraphs of (do shell script "sed -e 's/⌃/control+/g' -e 's/⌥/option+/g' -e 's/⇧/shift+/g' -e 's/⌫/backspace+/g' <<< " & foo's quoted form)

结果:

{"$", "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+€", "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"}

用它们的描述词替换特殊字符并附加一个加号怎么样?由于奇数字符的数量不多,这样做的好处是很明显,而且可以很容易地添加或编辑替换。

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 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

注:你的源文本中有一些奇怪的字符。例如,你的"控制"角色实际上是一个"向上箭头"(U+2303)。如果你的文本有更传统的'回旋重音' (U+005E),那么你需要编辑sed参数。

上面的代码用您的源列表为我生成以下结果。如果这是想要的输出,请告诉我。

{"$", "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+€", "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"}

一些AppleScriptObjC(好吧,几乎所有的东西)可能会更快,但即使使用常规AppleScript进行长时间的操作,也会在不使用shell脚本的情况下更快,因为多次调用shell脚本的开销是昂贵的。在我的机器上,shell脚本事件占用了94%的时间,而您的脚本还有更多的事情要做。

长方法包括遍历每个热键项的字符,用它们的描述替换特殊字符,并添加+分隔符。当在热键项中使用常规的space并将其替换为与其他修饰符字符相同的字符时,通过在发现F后附加热键字符串的其余部分来处理边缘情况(修饰符键通常先出现)。

注意控件的符号不同于插入符号^(shift+6):

set hotkeyShortcutList to {"$", "U", "J", "G", "R", "⇧R", "⇧Y", "⇧G", "⇧B", "⇧P", "⇧⌫", "⌃M", "⌃W", "⌃^", "⌃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", "⇧⌃)", "⇧⌃?", "⌃+", "⌘ ", "[", "]", "V", "L", "P", "S", "N", "Q", "O", "T", "E", "D", "W", "C", "⌘F1", "⌃F12"}
set replacements to {{" ", "space"}, {"⇧", "shift"}, {"⌃", "control"}, {"⌥", "option"}, {"⌘", "command"}, {"↵", "enter"}, {"↩", "return"}, {"⌫", "backspace"}} -- key symbols to replace
set processedShortcutList to {}
repeat with anItem in hotkeyShortcutList
set processedItem to ""
set anItem to contents of anItem
repeat with here from 1 to (count anItem)
set char to item here of anItem
repeat with replacement in replacements -- replace key symbol with its description
if first item of replacement is char then
set char to second item of replacement
exit repeat
end if
end repeat
considering case and diacriticals -- tighten the comparison a little
if char is "F" then -- can be function key, so just append the rest
set processedItem to processedItem & text here thru -1 of anItem & "+"
exit repeat
end if
end considering
set processedItem to processedItem & char & "+"
end repeat
set end of processedShortcutList to text 1 thru -2 of processedItem -- strip last
end repeat
return processedShortcutList