我正在尝试编辑快捷方式列表和带有“+”字符的独立键。
所以“⌃⌥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
但我有三个问题:
shell脚本似乎适用于常规字符,但在尝试处理特殊字符(如“⇧", "⌃" 及"⌥“。我对shell脚本的理解非常有限,但这似乎是我可以在谷歌上搜索的文本处理方式
我需要在添加“+”后用文本替换特殊字符。⇧ 替换为“移位”,⌃ 替换为“控制”,⌥ 替换为“命令”,⌥ 替换为“命令”等
我想为具有多个字母的键添加一些边缘大小写保护,例如空格键的“空格”和F键的“F1”,因此我不会以“s+p+a+c+e”和“F+1”结束
我知道AppleScript可能不是最简单的方法,但我会使用AppleScript中的值,所以我觉得把它们放在一起会很好。有什么建议吗
一些AppleScriptObjC(好的,几乎所有的东西)可能更快,但是即使使用常规AppleScript做了很长一段时间,如果不使用shell脚本,也会更快,因为多次调用shell脚本的开销是昂贵的。在我的机器上,shell脚本事件占用了94%的时间,而您的脚本还有很多事情要做
漫长的过程包括单步遍历每个热键项的字符,用描述替换特殊字符,并添加
+
分隔符。通过在热键项中使用常规space
并将其替换为与其他修改器字符相同的字符,以及在找到F
后仅追加热键字符串的其余部分(修改器键通常位于第一位),可以在遍历字符时处理边缘大小写请注意,控制符号
⌃
与插入符号^
(shift+6)不同:这里有一个比其他答案更快的方法,因为它不需要在命令的结果强制回列表
repeat
循环中运行,因为它将列表强制到linefeedseparatedtext以使用单个do shell script
命令和sed
处理它,然后将^{在脚本调试器中进行测试时,以下示例AppleScript代码花费了0.01秒,而来自red_威胁的答案中的代码花费了0.05秒,来自Mockman的答案中的代码花费了0.93秒
示例AppleScript代码:
结果:
把特殊字符替换成描述性的单词,加上一个加号怎么样?由于您有少量的奇数字符,这样做的好处是显而易见,并且可以方便地添加或编辑替换
注意:您的源文本中有一些奇怪的字符。例如,“控制”字符实际上是一个“向上箭头”(U+2303)。如果您的文本具有更传统的“扬抑重音”(U+005E),则需要编辑sed参数
上面为我生成了下面的源列表结果。让我知道这是否是所需的输出
相关问题 更多 >
编程相关推荐