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

2024-10-01 02:22:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编辑快捷方式列表和带有“+”字符的独立键。

所以“⌃⌥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

但我有三个问题:

  1. shell脚本似乎适用于常规字符,但在尝试处理特殊字符(如“⇧", "⌃" 及"⌥“。我对shell脚本的理解非常有限,但这似乎是我可以在谷歌上搜索的文本处理方式

  2. 我需要在添加“+”后用文本替换特殊字符。⇧ 替换为“移位”,⌃ 替换为“控制”,⌥ 替换为“命令”,⌥ 替换为“命令”等

  3. 我想为具有多个字母的键添加一些边缘大小写保护,例如空格键的“空格”和F键的“F1”,因此我不会以“s+p+a+c+e”和“F+1”结束

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


Tags: ofto脚本列表shell字符end快捷方式
3条回答

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

这里有一个比其他答案更快的方法,因为它不需要在repeat循环中运行,因为它将列表强制到linefeedseparatedtext以使用单个do shell script命令sed处理它,然后将^{命令的结果强制回列表

脚本调试器中进行测试时,以下示例AppleScript代码花费了0.01秒,而来自red_威胁的答案中的代码花费了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"}

把特殊字符替换成描述性的单词,加上一个加号怎么样?由于您有少量的奇数字符,这样做的好处是显而易见,并且可以方便地添加或编辑替换

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

相关问题 更多 >