首页 > 解决方案 > 如何使用 AppleScript 在 Safari 网络浏览器中单击此层次结构的选项卡

问题描述

我不知道如何访问这个层次结构,因为它似乎真的嵌套在代码中。有谁知道我如何通过 Applescript 单击“开始”选项卡?

等级制度

标签: applescripthierarchy

解决方案


The easiest way I find to obtain a reference to a UI element from an object hierarchy is to use System Events' click at command, e.g.

tell application "System Events" to click at {300, 400}

If you run this command in Script Editor, it returns an object reference to whatever it finds at the coordinates you specify, in this case {300, 400}.

Therefore, what you can do is to hover the mouse over your "Start" object and obtain the mouse's coordinates. This can be done by starting a screen capture using ⌘</kbd>⇧</kbd>4, which brings up a set of crosshairs from which you can read the coordinates of the mouse cursor. Then press ESC to cancel the screen capture, and use those coordinates to run a click at command (make sure the Script Editor window doesn't obscure the "Start" tab when you run the command).

Then, once you run it, the object reference (if it has one, which it ought to if it appears in the Accessibility Inspector) will appear in the results pane at the bottom of the Script Editor window, which you can copy and paste. Here's an example:

Screenshot on macOS

Here, my mouse cursor is hovering over the tab in Safari in which I'm typing this answer. I obtained the coordinates as being {500, 73}, then ran the command you see in Script Editor. At the bottom is the object reference to the tab. Note that the mouse doesn't need to remain hovering over the object in question as the coordinates specified will be used to issue the click and obtain a reference; I just kept my mouse there for illustrative purposes.


推荐阅读