如何创建Applescript来设置键盘快捷方式



如果有人能帮我一把,我正在尝试为一些服务自动创建键盘快捷键
到目前为止我得到的是

tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.keyboard"
delay 1
tell application "System Events"
click radio button "Shortcuts" of tab group 1 of window "Keyboard" of application process "System Preferences"
delay 1
select row 6 of table 1 of scroll area 1 of splitter group 1 of tab group 1 of window 1 of application process "System Preferences"
set value of text field 1 of UI element "My Service Name" of row 59 of outline 1 of scroll area 2 of splitter group of tab group 1 of window 1 of application process "System Preferences" to "⌥⌘1"
end tell
end tell

但现在我被卡住了,我不能做的是:

  • 这个...of row 59...不好,我想用快捷方式的名称代替这个索引。我知道快捷方式的名称在UI element "My Service Name"中,但我不知道如何进行嵌套选择,例如...of row 59...会做类似of row (where UI element "My Service Name")的操作
  • 此脚本仅在服务以前有快捷方式时有效,如果快捷方式为"无"则根本不起作用

有人提示我如何解决这个问题吗?

如果有其他方法可以在没有appescapet的情况下创建快捷方式,那将是受欢迎的

提前谢谢大家。

据我所知,使用UI脚本系统首选项键盘快捷键服务(或快捷方式下的任何其他类别)的唯一方法是模拟手动执行时可能发生的所有步骤。这就是为什么如果没有其他方法来完成手头的任务,我只会使用UI脚本

由于在代码中,您使用的是select row 6 ...,并希望以row 59为目标,我假设您使用macOS MojavemacOS Catalina,并以服务类别为目标,因为这是唯一一个类别实际上会有那么多或更多行来为其分配键盘快捷键

下面进一步显示的示例AppleScript代码macOS MojavemacOS Catalina下的脚本编辑器中进行了测试,以及macOS High Sierra进行了一次小编辑,并在我的系统上使用美国英语作为其语言和amp;系统首选项中的区域设置。

这也是专门针对服务类别编写的,因为其他分类需要不同的编码

您需要设置三个变量serviceNameregularKeymodifierKeys,后者基于脚本开始注释中的list

它最初设置为使用键盘快捷键导入图像⇧⌘9,在修改脚本之前,您应该按原样测试它。

注意:当按下键盘快捷键时,任何快捷键集都必须是任何具有焦点的应用程序唯一的。

示例 AppleScript 代码

--  # Call the SetChangeServicesKeyboardShortcut(serviceName, regularKey, modifierKeys)
--  # handler using the parameters as defined below:
--  # serviceName defines the name of the target service under:
--  # System Preferences > Keyboard > Shortcuts > Services
--  # regularKey defines the regular key to press.
--  # modifierKeys define the modifier keys to be pressed. 
--  # Use the value based on the list below:
--  # 
--  #   1 = {command down}
--  #   2 = {shift down, command down}
--  #   3 = {control down, command down}
--  #   4 = {option down, command down}
--  #   5 = {control down, option down, command down}
--  #   6 = {shift down, control down, command down}
--  #   7 = {shift down, option down, command down}
--  #   8 = {shift down, control down, option down, command down}
--  #   
--  # |  shift = ⇧ | control = ⌃ | option = ⌥ | command = ⌘ |
--  #   

my SetChangeServicesKeyboardShortcut("Import Image", "9", "2")

--  ##################################
--  ## Do not modify code below unless necessary, as ##
--  ## it's tokenized for the variables defined above.   ##
--  ##################################

--  ## Handlers ##
on SetChangeServicesKeyboardShortcut(serviceName, regularKey, modifierKeys)
--  # Need to start with System Preferences closed.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
end if
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
--  # Open System Preferences to the target pane.
tell application "System Preferences"
activate
reveal pane id "com.apple.preference.keyboard"
end tell
--  # Navigate to Shortcuts > Services and select the
--  # target service, then change/set its keyboard shortcut.
tell application "System Events"
tell application process "System Preferences"
tell its window 1
--  # Wait until the Shortcuts tab can be clicked.          
repeat until exists (radio buttons of tab group 1)
delay 0.1
end repeat
--  # Click the Shortcuts tab.          
click radio button "Shortcuts" of tab group 1
--  # Wait until Services can be selected.                      
repeat until exists ¬
(rows of table 1 of scroll areas of splitter group 1 of tab group 1 ¬
whose name of static text 1 is equal to "Services")
delay 0.1
end repeat
--  # Select Services.          
try
select (rows of table 1 of scroll area 1 of splitter group 1 of tab group 1 ¬
whose name of static text 1 is equal to "Services")
end try
tell outline 1 of scroll area 2 of splitter group 1 of tab group 1
--  # Wait until the services under Services are available.                 
repeat until exists (row 1)
delay 0.01
end repeat
--  # Set focus to the first item of Services.              
repeat 2 times
key code 48 -- # tab key
delay 0.25
end repeat
--  # Get the name of every service under Services.             
set serviceNames to (get name of UI element 2 of rows)
--  # Get the row number of the target service under Services.              
set countRows to (count serviceNames)
repeat with i from 1 to countRows
if contents of item i of serviceNames is equal to serviceName then
set rowNumber to i
exit repeat
end if
end repeat
--  # Select the row of the target target service under Services.               
select (row rowNumber)
--  # Change/Set the keyboard shortcut of the target service under Services.                
if exists (button "Add Shortcut" of UI element 2 of row rowNumber) then
click button "Add Shortcut" of UI element 2 of row rowNumber
my shortcutKeystrokes(regularKey, modifierKeys)
else
key code 36 -- # return key
my shortcutKeystrokes(regularKey, modifierKeys)
end if
select (row 1)
end tell
end tell
end tell
end tell
quit application "System Preferences"
end SetChangeServicesKeyboardShortcut
on shortcutKeystrokes(regularKey, modifierKeys)
tell application "System Events"
if modifierKeys is equal to "1" then
keystroke regularKey using {command down}
else if modifierKeys is equal to "2" then
keystroke regularKey using {shift down, command down}
else if modifierKeys is equal to "3" then
keystroke regularKey using {control down, command down}
else if modifierKeys is equal to "4" then
keystroke regularKey using {option down, command down}
else if modifierKeys is equal to "5" then
keystroke regularKey using {control down, option down, command down}
else if modifierKeys is equal to "6" then
keystroke regularKey using {shift down, control down, command down}
else if modifierKeys is equal to "7" then
keystroke regularKey using {shift down, option down, command down}
else if modifierKeys is equal to "8" then
keystroke regularKey using {shift down, control down, option down, command down}
end if
end tell
end shortcutKeystrokes

注意:对于macOS High Sierra,以及可能更早的版本,将注释-- # Set focus to the first item of Services.下的repeat 2 times设置为:repeat 3 times


注意:示例AppleScript代码就是这样,并且在没有现有错误处理的情况下,不包含任何额外的错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript语言指南中的try语句error语句。另请参阅处理错误。此外,在适当的情况下,可能需要在事件之间使用延迟命令,例如delay 0.5,并适当设置延迟

我对您的脚本进行了一些重组,因为我更喜欢分层的"tell-block"结构(我发现它更容易阅读和遵循),并且我将其全部放入一个处理程序中进行泛化。但除此之外,诀窍是使用where搜索来查找UI结构并找到可识别的功能(注意,where是更传统的whose的同义词)。请参阅脚本中的注释。请注意,我正在更改键盘快捷键首选项的"聚光灯"部分的"显示查找器搜索窗口"行,但通过更改处理程序调用的值,您可以更改任何首选项。

要做到这一点,您需要知道您的服务属于哪个部分,以及列表中显示的服务名称。如果你为new_val输入一个文本字符串,它会把它当作你键入的第一个字母;如果你输入一个整数,它会把它当作一个键代码,允许你使用非打印字符和功能键作为快捷键。请参阅:钥匙代码列表。

已更正此脚本,以修复几个错误,并适应"服务"部分的层次结构,以及通过单击"添加快捷方式"按钮添加新快捷方式的能力。请注意,如果您是第一次添加快捷方式,则在选择其他行之前,UI仍将显示"添加快捷方式"按钮。即使手动添加快捷方式也是如此,所以这是GUI的限制,而不是脚本的限制。

-- sets the 'Send Message' shortcut to cmd-opt-A
my change_shortcut("Services", "Text", "Send Message", "a", {"command", "option"})
-- sets the 'Call' service shortcut to cmd-F6
my change_shortcut("Services", "Text", "Call", 97, {"command"})
on change_shortcut(shortcut_region, section_name, shortcut_title, new_val, special_key_list)
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.keyboard"
delay 1
tell application "System Events"
tell process "System Preferences"'s window "Keyboard"'s first tab group
click radio button "Shortcuts"
tell first splitter group
set sidebar_obj to first table of first scroll area
tell sidebar_obj
(* 
this looks to find the first row in the sidebar that contains a static text 
element with the value of `shortcut_region`
*)
set sidebar_entry to first row where (its first static text's value is shortcut_region)
select sidebar_entry
end tell
set outline_obj to first outline of second scroll area
tell outline_obj
(* 
if the shortcut outline view is arranged in groups, this section 
finds the correct group and make sure its disclosure triangle is 
opened, exposing the settings within
*)
if section_name is not "" and section_name is not missing value then
set wanted_section_row to first row where (its last UI element's name is section_name)
tell wanted_section_row's second UI element
set disclosure_tri to first UI element whose role description is "disclosure triangle"
if disclosure_tri's value is 0 then click disclosure_tri
end tell
delay 0.5
end if
(* 
this looks to find the first row in the outline that contains two 
UI elements (the row's cells) the second of which contains a static text 
element with the value of shortcut_title
*)
set wanted_entry to first row where (its last UI element's name is shortcut_title)
tell wanted_entry
select
set new_shortcut_flag to false
tell second UI element
if exists button "Add Shortcut" then
click button "Add Shortcut"
set new_shortcut_flag to true
end if
UI elements
-- set up a list of special keys
set special_keys to {}
if special_key_list contains "command" then set end of special_keys to command down
if special_key_list contains "control" then set end of special_keys to control down
if special_key_list contains "option" then set end of special_keys to option down
if special_key_list contains "shift" then set end of special_keys to shift down
-- opens the text field for editing, then write or keycode in the text
tell first text field
if not new_shortcut_flag then perform action "AXConfirm"
if class of new_val is text then
keystroke new_val using special_keys
else
key code new_val using special_keys
end if
end tell
-- select the cell to submit the result
select
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end change_shortcut