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

Add "+" character in between every character in AppleScript List

我正在尝试编辑快捷键列表,并使用“+”字符分隔键。

所以“⌃⌥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 脚本的理解非常有限,但这似乎是我所能处理的文本处理方式 Google...

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

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

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

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

如何将特殊字符替换为描述性词并附加一个加号——一次性完成?由于您有少量的奇数字符,因此这样做的好处是显而易见,并且可以轻松添加或编辑替换内容。

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

注意您的源文本中有一些奇怪的字符。例如,您的 'control' 字符实际上是一个 'up arrowhead' (U+2303)。如果您的文本有更传统的 'circumflex accent' (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"}

这是一种比其他答案更快的方法,因为它不需要 运行 在 repeat loop 中强制 listlinefeed 分隔 text 用单个 do shell script 处理它命令 sed,然后将 do shell script 命令 的结果强制返回列表。

Script Debugger 中测试以下 example AppleScript code 用了 0.01 秒,而 red_menace 的答案中的代码用了 0.05 秒,Mockman 的答案中的代码用了 0.93 秒。

示例 AppleScript 代码:

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