如何使用 applescript 单击系统偏好设置上的按钮?
How to click a button on System Preferences using applescript?
我正在尝试编写一个 applescript 来更改系统偏好设置中的热点设置。
这是我到目前为止得到的。
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.desktopscreeneffect"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Hot Corners..."
end tell
end tell
但是我得到这个错误:error "System Events got an error: Can’t get button \"Hot Corners...\" of process \"System Preferences\"." number -1728 from button "Hot Corners..." of process "System Preferences"
。如果有人能解释这里出了什么问题,我将不胜感激,另外还有什么方法可以获取窗格的属性(例如可用按钮)?
变化:
click button "Hot Corners..."
收件人:
click button "Hot Corners…" of tab group 1 of window 1
注意使用 …
、一个 省略号 与 ...
三个 点 ,以及缺少的 UI 脚本 分层 元素.
在代码的click
行之后,只是为了获取各种UI元素你'如果您需要与之交互,请使用以下 example AppleScript code 获取该信息:
get UI elements of sheet 1 of window 1
get UI elements of group "Active Screen Corners" of sheet 1 of window 1
code 的这些行不会保留在完成的脚本中。
综上所述,这主要是为了指出您问题中的 code 有什么问题,但是另一个答案中的方法是到达的方法热点.
更新地址评论…
what does it mean 'UI Scripting hierarchical elements"
来自 AppleScript 词典 系统事件:
UI element n : A piece of the user interface of a process
这意味着构成用户界面的元素存在层次结构,例如,如行所示代码:
button "Hot Corners…" of tab group 1 of window 1
如以下 Accessibility Inspector 屏幕截图的顶部所示,Xcode 的一部分:
这是我设置的显示 Desktop:
的热点之一
如果你在你的问题中提到你想要在热点区域使用哪个下拉菜单select…我可以在我的解决方案中发布额外的代码。
此 AppleScript 代码适用于使用最新版本的 macOS Big Sur 的我。
(* Quits "System Preferences" If Running *)
if application "System Preferences" is running then ¬
do shell script "killall 'System Preferences'"
(* Makes Sure "System Preferences" Is Not Running
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences"
(* Launches "System Preferences" Without Bringing It
To The Foreground, Going Directly To The Window Where You Can
`click button "Hot Corners..."` *)
reveal anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect"
(* Makes Sure anchor "ScreenSaverPref_HotCorners" exists
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat while not (exists of anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect")
delay 0.1
end repeat
(*.Makes application "System Preferences"
The Frontmost Visible App, Allowing You To Perform
Any Click Commands *)
activate
end tell
delay 0.1
tell application "System Events"
(* "Upper Left Dropdown Menu (pop up button 1) This Can Also Be Repeated
For `(pop up button 2) <-- Bottom Left, (pop up button 3) <-- Upper Right,
& (pop up button 4) <-- Bottom Right" *)
(*. By Now You Should Start Understanding The Purpose And Function
Of These Repeat Loops *)
repeat while not (exists of pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences")
delay 0.1
end repeat
click pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
delay 0.1
repeat while not (exists of menu item "Launchpad" of menu 1 of ¬
pop up button 1 of group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences")
delay 0.1
end repeat
-- Replace "Launchpad" Which Which Ever You Want
click menu item "Launchpad" of menu 1 of pop up button 1 of ¬
group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences"
delay 0.3
click UI element "OK" of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
end tell
delay 0.5
tell application "System Preferences" to quit
我正在尝试编写一个 applescript 来更改系统偏好设置中的热点设置。
这是我到目前为止得到的。
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.desktopscreeneffect"
end tell
tell application "System Events"
tell process "System Preferences"
click button "Hot Corners..."
end tell
end tell
但是我得到这个错误:error "System Events got an error: Can’t get button \"Hot Corners...\" of process \"System Preferences\"." number -1728 from button "Hot Corners..." of process "System Preferences"
。如果有人能解释这里出了什么问题,我将不胜感激,另外还有什么方法可以获取窗格的属性(例如可用按钮)?
变化:
click button "Hot Corners..."
收件人:
click button "Hot Corners…" of tab group 1 of window 1
注意使用 …
、一个 省略号 与 ...
三个 点 ,以及缺少的 UI 脚本 分层 元素.
在代码的click
行之后,只是为了获取各种UI元素你'如果您需要与之交互,请使用以下 example AppleScript code 获取该信息:
get UI elements of sheet 1 of window 1
get UI elements of group "Active Screen Corners" of sheet 1 of window 1
code 的这些行不会保留在完成的脚本中。
综上所述,这主要是为了指出您问题中的 code 有什么问题,但是另一个答案中的方法是到达的方法热点.
更新地址评论…
what does it mean 'UI Scripting hierarchical elements"
来自 AppleScript 词典 系统事件:
UI element n : A piece of the user interface of a process
这意味着构成用户界面的元素存在层次结构,例如,如行所示代码:
button "Hot Corners…" of tab group 1 of window 1
如以下 Accessibility Inspector 屏幕截图的顶部所示,Xcode 的一部分:
这是我设置的显示 Desktop:
的热点之一如果你在你的问题中提到你想要在热点区域使用哪个下拉菜单select…我可以在我的解决方案中发布额外的代码。
此 AppleScript 代码适用于使用最新版本的 macOS Big Sur 的我。
(* Quits "System Preferences" If Running *)
if application "System Preferences" is running then ¬
do shell script "killall 'System Preferences'"
(* Makes Sure "System Preferences" Is Not Running
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat until application "System Preferences" is not running
delay 0.1
end repeat
tell application "System Preferences"
(* Launches "System Preferences" Without Bringing It
To The Foreground, Going Directly To The Window Where You Can
`click button "Hot Corners..."` *)
reveal anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect"
(* Makes Sure anchor "ScreenSaverPref_HotCorners" exists
Checking Every 1/10 Of A Second Before Moving On
To The Next Command*)
repeat while not (exists of anchor "ScreenSaverPref_HotCorners" of ¬
pane id "com.apple.preference.desktopscreeneffect")
delay 0.1
end repeat
(*.Makes application "System Preferences"
The Frontmost Visible App, Allowing You To Perform
Any Click Commands *)
activate
end tell
delay 0.1
tell application "System Events"
(* "Upper Left Dropdown Menu (pop up button 1) This Can Also Be Repeated
For `(pop up button 2) <-- Bottom Left, (pop up button 3) <-- Upper Right,
& (pop up button 4) <-- Bottom Right" *)
(*. By Now You Should Start Understanding The Purpose And Function
Of These Repeat Loops *)
repeat while not (exists of pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences")
delay 0.1
end repeat
click pop up button 1 of group 1 of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
delay 0.1
repeat while not (exists of menu item "Launchpad" of menu 1 of ¬
pop up button 1 of group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences")
delay 0.1
end repeat
-- Replace "Launchpad" Which Which Ever You Want
click menu item "Launchpad" of menu 1 of pop up button 1 of ¬
group 1 of sheet 1 of window "Desktop & Screen Saver" of ¬
application process "System Preferences"
delay 0.3
click UI element "OK" of sheet 1 of window ¬
"Desktop & Screen Saver" of application process "System Preferences"
end tell
delay 0.5
tell application "System Preferences" to quit