我知道的唯一一种可以使用 UI 脚本 和 系统偏好设置 > 键盘 > 快捷方式的方法strong> > Services(或Shortcuts下的任何其他类别)基本上模拟了手动执行时会发生的所有步骤。这就是为什么我只在没有其他方法来完成手头的任务时才使用 UI 脚本。
由于在您的 代码 中,您使用的是 select row 6 ... 并希望定位 row 59,我假设您使用的是 macOS Mojave 或 macOS Catalina,并以服务 类别为目标,因为这是唯一类别,实际上会有那么多行 或更多,分配一个键盘快捷键。
示例 AppleScript 代码(如下所示)在macOS 下的Script Editor 中进行了测试Mojave 和 macOS Catalina,以及 macOS High Sierra 进行了一次小修改,按原样在我的系统上使用美国英语,因为它在系统偏好设置中的语言和地区设置。
这也是专门针对服务 类别而编写的,因为其他类别需要不同的编码。
你需要设置三个变量的值,serviceName,regularKey,modifierKeys,后者是基于list 在 script 的开头 cmets 中。
它最初是使用键盘快捷键 ⇧⌘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 以及可能更早的版本,请将 comment 下的 repeat 2 times -- # Set focus to the first item of Services. 设置为:repeat 3 times
注意:示例 AppleScript 代码就是这样,并且没有现有的错误处理,不包含任何适当的额外错误处理。用户有责任根据需要或需要添加任何错误处理。查看AppleScript Language Guide 中的try statement 和error statement。另请参阅Working with Errors。此外,在适当的情况下,可能需要在事件之间使用delay 命令,例如delay 0.5,延迟的值设置得当。