Macos 如何创建Applescript来设置键盘快捷键?
如果有人能帮我一把,我正在尝试为一些服务自动创建键盘快捷方式Macos 如何创建Applescript来设置键盘快捷键?,macos,applescript,shortcut,Macos,Applescript,Shortcut,如果有人能帮我一把,我正在尝试为一些服务自动创建键盘快捷方式 到目前为止我得到的是 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 ta
到目前为止我得到的是
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
但现在我被困住了,我不能做的是:
- 第59行的
…不太好,我想用快捷方式的名称来代替这个索引。我知道快捷方式的名称在
中,但我不知道如何进行嵌套选择,例如,改为UI元素“我的服务名称”
执行类似行的…第59行…
(其中UI元素“我的服务名称”)
- 仅当服务具有以前的快捷方式时,此脚本才起作用,如果快捷方式为“无”,则根本不起作用
提前感谢大家。我对您的脚本进行了一点重构,因为我更喜欢层次结构的“告诉块”结构(我发现它更易于阅读和遵循),并且我已经将其全部放在一个处理程序中以对其进行概括。但除此之外,诀窍是使用
where
搜索来查找UI结构并查找可识别的功能(注意,where
是更传统的的同义词,其)。请参阅脚本中的注释。请注意,我正在更改键盘快捷键首选项“Spotlight”部分的“Show Finder search window”行,但通过更改处理程序调用的值,您可以更改任何首选项
要实现这一点,您需要知道您的服务属于哪个部分,以及列表中显示的服务名称。如果您为new\u 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
我对您的脚本进行了一些重构,因为我更喜欢层次结构的“tell block”结构(我发现它更易于阅读和遵循),并且我将其全部放入了一个处理程序中以对其进行概括。但除此之外,诀窍是使用where
搜索来查找UI结构并查找可识别的功能(注意,where
是更传统的的同义词,其)。请参阅脚本中的注释。请注意,我正在更改键盘快捷键首选项“Spotlight”部分的“Show Finder search window”行,但通过更改处理程序调用的值,您可以更改任何首选项
要实现这一点,您需要知道您的服务属于哪个部分,以及列表中显示的服务名称。如果您为new\u 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
我所知道的唯一可以确定的方法是使用用户界面脚本和系统首选项键盘快捷键服务(或快捷键下的任何其他类别)基本上是模拟手动操作时发生的所有步骤。这就是为什么只有在没有其他方法完成手头的任务时,我才会使用UI脚本编写
因为在您的代码中,您使用的是选择第6行…
并希望针对第59行
,我假设您使用的是macOS Mojave或macOS Catalina,并且针对的是服务类别,因为这是唯一一个实际有那么多行的类别,或更多,以将键盘快捷键指定给
下面进一步显示的示例AppleScript代码在脚本编辑器中的macOS Mojave和macOS Catalina下进行了测试,以及macOS High Sierra只需一次小编辑,即可正常工作,在我的系统上,使用美国英语进行系统首选项中的语言和地区设置
这也是为了专门针对服务类而编写的,因为其他类别需要不同的编码
您需要设置三个变量的值,serviceName
,regularKey
,modifierKeys
,后者基于脚本开头注释中的列表
-- 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
它最初设置为使用键盘快捷键导入图像⇧⌘9在修改脚本之前,应按原样测试脚本
注意:当按下键盘快捷键时,任何具有焦点的应用程序的键盘快捷键集都必须是唯一的
示例AppleScript代码:
——#调用SetChangeServicesKeyboardShortcut(se