【问题标题】:How to click a button on System Preferences using applescript?如何使用applescript单击系统偏好设置上的按钮?
【发布时间】:2021-05-28 02:23:15
【问题描述】:

我正在尝试编写一个 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"。我会很感激有人解释这里出了什么问题,还有没有办法获取窗格的属性(例如可用按钮)?

【问题讨论】:

    标签: applescript system-preferences


    【解决方案1】:

    如果您在问题中提到要选择热门角落中的哪个下拉菜单……我可以在我的解决方案中发布该附加代码。

    此 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
    

    【讨论】:

    • 如果您能对代码提供一些解释,我将不胜感激
    • @Arman 我在代码中添加了一些 cmets,应该更详细地解释发生了什么。我还添加了一个动画,它应该有助于可视化代码的每个步骤。
    • 非常感谢。这让我知道我应该做什么
    【解决方案2】:

    变化:

    click button "Hot Corners..."
    

    收件人:

    click button "Hot Corners…" of tab group 1 of window 1
    

    注意… 的使用,一个省略号 与... 三个点,以及缺少的UI 脚本 层次结构元素。


    在 代码 的 click 行之后,为了获取您需要与之交互的各种 UI 元素,请使用以下 示例 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 有什么问题,但是另一个答案中的方法是到达 Hot 的方法角落。


    更新地址评论…

    “UI 脚本分层元素”是什么意思

    来自 系统事件的 AppleScript 字典:

    UI 元素 n:进程用户界面的一部分

    这意味着构成用户界面的元素有一个层次结构,例如,如代码行所示:

    button "Hot Corners…" of tab group 1 of window 1
    

    如 Accessibility Inspector(Xcode 的一部分)的以下屏幕截图的顶部所示:

    这是我设置的一个热门角落之一,用于展示桌面:

    【讨论】:

    • 非常感谢。但“UI 脚本分层元素”是什么意思?
    • @Arman,RE:“'UI Scripting 分层元素是什么意思'” - 请参阅我的答案的更新以解决评论...部分。
    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 2011-10-02
    • 1970-01-01
    • 2021-08-26
    • 1970-01-01
    • 2022-01-18
    相关资源
    最近更新 更多