如何使用 Apple 脚本单击“确定”?

Jon*_*002 3 applescript

我尝试在弹出窗口中选择“确定”,以使用 Apple 脚本缩放屏幕。

有谁知道我可以向此脚本添加什么以允许我单击“确定”?

苹果脚本:

tell application "System Preferences"
    reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
    click radio button "Scaled" of radio group 1 of tab group 1
    click radio button 1 of radio group 1 of group 1 of tab group 1

    (* Trying to click the ok button here *)
    delay 1
    click ((pop up buttons of sheet 1 of window 1) whose description is "OK")

end tell

quit application "System Preferences"
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

或者更确切地说,是否有人知道一个可信的应用程序,当我在屏幕上访问它们时,它可以以图形方式打印出 UI 的标签,这样我就知道当我说:“告诉UIName ”时要使用什么?

谢谢。

CJK*_*CJK 7

解决方案一:按Return

\n

由于您无论如何都在使用系统事件,并且默认情况下会选择有问题的按钮(以蓝色突出显示),因此只需确保首选项窗格处于焦点状态并使用系统事件来按下return按键即可:

\n
    tell application "System Events" to keystroke return\n
Run Code Online (Sandbox Code Playgroud)\n

它快速、简单,并且省去了在层次结构中识别 UI 元素的麻烦。缺点是首选项窗格具有焦点,如果它在收到击键之前失去焦点,则脚本的其余部分将失败。

\n

解决方案 2:您要求的解决方案

\n

关于 GUI 对象层次结构中 UI 元素的识别,您得到的按钮的类及其描述都是错误的。

\n

“确定”按钮的引用方式如下:

\n
    tell application "System Events" to tell process "System Preferences" \xc2\xac\n        to get button "OK" of sheet 1 of window "Built-in Retina Display"\n
Run Code Online (Sandbox Code Playgroud)\n

window 1也很好)。您可以使用whose过滤器来定位它,具体操作如下(通过系统事件系统首选项进程):

\n
    get buttons of sheet 1 of window 1 whose name is "OK"\n
Run Code Online (Sandbox Code Playgroud)\n

但这所做的只是要求 AppleScript 搜索一个我们知道名称的按钮,然后烦人地将结果作为列表返回给我们(可以通过请求 来展平列表结构)first button of sheet 1...

\n

但是,我们确实知道它的名称,而且只有一个,因此我们可以直接通过名称引用它。

\n

作为侧边栏,如果您快速运行此命令:

\n
    get the properties of button "OK" of sheet 1 of window "Built-in Retina Display"\n
Run Code Online (Sandbox Code Playgroud)\n

你会看到它的描述只是“按钮”,这不是你所希望的。现在运行这个命令:

\n
    get the actions of button "OK" of sheet 1 of window "Built-in Retina Display"\n
Run Code Online (Sandbox Code Playgroud)\n

这表明它有一个可用的操作AXPress(这相当于单击鼠标)。

\n

因此,最后,比按回车键更令人满意的方式单击按钮的方法如下所示:

\n
    tell application "System Events" to tell process "System Preferences" \xc2\xac\n        to tell button "OK" of sheet 1 of window "Built-in Retina Display" \xc2\xac\n            to perform action "AXPress"\n
Run Code Online (Sandbox Code Playgroud)\n
\n

为了探索屏幕上的 GUI 对象,我偶尔会使用Apple XCode附带的辅助功能检查器。它有点有用,尽管它引用对象的名称与 AppleScript 执行的名称之间的一些差异令人不方便(它们也是非常微妙的差异,但足以阻止您的脚本工作并让您陷入疯狂)数周)。

\n

所以,实际上,我只是自己在脚本编辑器中以编程方式探索它中以编程方式探索它。

\n

我为解决您的问题所做的就是调出有问题的窗格和对话框,然后系统地告诉系统首选项进程get UI elements:然后get buttons of UI elements; 然后get buttons of UI elements of UI elements,当我看到返回的一对按钮“确定”和“取消”时停止。这可能很痛苦,但它会给你正确的参考。

\n

还有其他方法,但我会超出这个问题的范围。

\n