使用 Applescript 更改系统偏好设置
Using Applescript to change System Preferences
所以我找到了 这主要解决了我的问题,但我想做的不仅仅是一个复选框,对于我的选项,即启用鼠标键,你还需要导航子菜单和其他东西.我已经走了这么远:
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.universalaccess"
delay 2
#More Code here to select submenu "Pointer Controls", "Alternative Control Methods" and click the checkbox "Enable Mouse Keys"
end tell
如果你问我为什么不直接点击 5 次选项按钮启用鼠标键
tell application "System Events"
repeat 5
key code 58 #key code for the option key
delay 0.05
end repeat
end tell
我试过了,就是不行。
下面显示的示例AppleScript代码在[=45=下进行了测试]macOS Catalina,系统偏好设置中的语言和地区设置设置为英语(美国)—主要 并且毫无问题地为我工作1.
- 1 假定在 系统偏好设置 > 安全和隐私 > 隐私 已根据需要set/addressed。
请注意,各种 事件 已被注释掉,因此您可以取消注释您想要的那些。
示例 AppleScript 代码:
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
tell application "System Preferences"
reveal anchor "Alternate_Pointer_Actions" of ¬
pane id "com.apple.preference.universalaccess"
activate -- # Is needed because of 'sheet 1'.
end tell
tell application "System Events"
tell application process "System Preferences"
tell window 1
set i to 0
repeat until (exists checkbox 1 of tab group 1 of group 1)
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
tell tab group 1 of group 1
-- if (value of checkbox 1 as boolean) then click checkbox 1 -- # "Enable Mouse Keys"
-- if not (value of checkbox 1 as boolean) then click checkbox 1 -- # "Enable Mouse Keys"
-- click checkbox 1 -- # "Enable Mouse Keys"
-- click button 1 -- # "Options…"
end tell
set i to 0
repeat until (exists sheet 1)
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
tell sheet 1
-- # [] Ignore built-in trackpad when Mouse Keys is on
-- if (value of checkbox 1 as boolean) then click checkbox 1
-- if not (value of checkbox 1 as boolean) then click checkbox 1
-- click checkbox 1
-- # [] Press the Option key five times to toggle Mouse Keys
if (value of attribute "AXEnabled" of checkbox 2) then
-- if (value of checkbox 2 as boolean) then click checkbox 2
-- if not (value of checkbox 2 as boolean) then click checkbox 2
-- click checkbox 2
end if
-- # Initial Delay:
-- set value of slider 1 to 3 -- # Valid values, as a whole number: 0 thru 4
-- # Maximum Speed:
-- set value of slider 2 to 6 -- # Valid values, as a whole number: 0 thru 10
-- click button 1 -- # "OK"
-- click button 2 -- # "Cancle"
end tell
end tell
end tell
end tell
tell application "System Preferences" to quit
备注:
在tell sheet 1
块中,它用[]编码,当鼠标键打开时忽略内置触控板checkbox 首先检查它还会检查 [] 按 Option 键五次以切换鼠标键 checkbox并禁用它。您可能还需要添加一些额外的逻辑,具体取决于您尝试切换这些复选框状态的准确程度.
注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。
所以我找到了
tell application "System Preferences"
activate
delay 2
set the current pane to pane id "com.apple.preference.universalaccess"
delay 2
#More Code here to select submenu "Pointer Controls", "Alternative Control Methods" and click the checkbox "Enable Mouse Keys"
end tell
如果你问我为什么不直接点击 5 次选项按钮启用鼠标键
tell application "System Events"
repeat 5
key code 58 #key code for the option key
delay 0.05
end repeat
end tell
我试过了,就是不行。
下面显示的示例AppleScript代码在[=45=下进行了测试]macOS Catalina,系统偏好设置中的语言和地区设置设置为英语(美国)—主要 并且毫无问题地为我工作1.
- 1 假定在 系统偏好设置 > 安全和隐私 > 隐私 已根据需要set/addressed。
请注意,各种 事件 已被注释掉,因此您可以取消注释您想要的那些。
示例 AppleScript 代码:
-- # Check to see if System Preferences is
-- # running and if yes, then close it.
-- #
-- # This is done so the script will not fail
-- # if it is running and a modal sheet is
-- # showing, hence the use of 'killall'
-- # as 'quit' fails when done so, if it is.
-- #
-- # This is also done to allow default behaviors
-- # to be predictable from a clean occurrence.
if running of application "System Preferences" then
try
tell application "System Preferences" to quit
on error
do shell script "killall 'System Preferences'"
end try
delay 0.1
end if
-- # Make sure System Preferences is not running before
-- # opening it again. Otherwise there can be an issue
-- # when trying to reopen it while it's actually closing.
repeat while running of application "System Preferences" is true
delay 0.1
end repeat
tell application "System Preferences"
reveal anchor "Alternate_Pointer_Actions" of ¬
pane id "com.apple.preference.universalaccess"
activate -- # Is needed because of 'sheet 1'.
end tell
tell application "System Events"
tell application process "System Preferences"
tell window 1
set i to 0
repeat until (exists checkbox 1 of tab group 1 of group 1)
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
tell tab group 1 of group 1
-- if (value of checkbox 1 as boolean) then click checkbox 1 -- # "Enable Mouse Keys"
-- if not (value of checkbox 1 as boolean) then click checkbox 1 -- # "Enable Mouse Keys"
-- click checkbox 1 -- # "Enable Mouse Keys"
-- click button 1 -- # "Options…"
end tell
set i to 0
repeat until (exists sheet 1)
delay 0.1
set i to i + 1
if i ≥ 30 then return
end repeat
tell sheet 1
-- # [] Ignore built-in trackpad when Mouse Keys is on
-- if (value of checkbox 1 as boolean) then click checkbox 1
-- if not (value of checkbox 1 as boolean) then click checkbox 1
-- click checkbox 1
-- # [] Press the Option key five times to toggle Mouse Keys
if (value of attribute "AXEnabled" of checkbox 2) then
-- if (value of checkbox 2 as boolean) then click checkbox 2
-- if not (value of checkbox 2 as boolean) then click checkbox 2
-- click checkbox 2
end if
-- # Initial Delay:
-- set value of slider 1 to 3 -- # Valid values, as a whole number: 0 thru 4
-- # Maximum Speed:
-- set value of slider 2 to 6 -- # Valid values, as a whole number: 0 thru 10
-- click button 1 -- # "OK"
-- click button 2 -- # "Cancle"
end tell
end tell
end tell
end tell
tell application "System Preferences" to quit
备注:
在tell sheet 1
块中,它用[]编码,当鼠标键打开时忽略内置触控板checkbox 首先检查它还会检查 [] 按 Option 键五次以切换鼠标键 checkbox并禁用它。您可能还需要添加一些额外的逻辑,具体取决于您尝试切换这些复选框状态的准确程度.
注意:示例 AppleScript code 就是这样,没有包含任何 错误处理 不包含任何额外的 错误处理 可能是适当的。用户有责任根据需要添加任何 错误处理 。查看 try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay 命令 在适当的事件之间可能是必要的,例如delay 0.5
,适当设置延迟的值。