如何使用 applescript 滚动到窗口顶部?
我想要 applescript 一直向上滚动窗口。
我尝试过向上翻页键、主页键,并且尝试寻找一种使用窗口内置滚动功能进行滚动的方法,但到目前为止我根本无法移动滚动位置。
I want applescript to scroll a window all the way up.
I've tried the page up key, the home key, and I've tried looking for a way to scroll using the built in scrolling capabilities of the window, but I've so far been unable to even move the scrolled position at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
基本上,使用
tell app "System Events"
语句来发送击键和键代码。理论上,您可以使用以下内容:
但对我来说这不起作用。好消息是您可以使用密钥代码。我建议使用优秀的免费完整密钥代码应用程序来读取它们,尽管它有点棘手让它读取同时按下的两个键。
fn+ 箭头键组合键代码如下:
向上翻页:fn+ 向上键 :
键代码 116
向下翻页:fn+ 向下键:
键代码 121
首页:fn< /kbd>+ 左键:
键码 115
End: fn+ 右键:
键码119
例如,如果您在 Safari 中打开了一个很长的页面,并且您想要滚动到其末尾,请使用
Basically, use a
tell app "System Events"
statement to send keystrokes and key codes.In theory, you could use the following:
But for me this doesn´t work. The good news is that you can use the key codes instead. I suggest using the excellent free Full Key Codes application to read them, though it is a bit tricky to let it read two keys pressed simultaneously.
The key codes for the fn+ arrow keys-combos are as following:
Page up: fn+ up key:
key code 116
Page down: fn+ down key:
key code 121
Home: fn+ left key:
key code 115
End: fn+ right key:
key code 119
So for example if you had a long page open in Safari, and you want to scroll to its end, use
通过浏览器,您还可以使用 JavaScript:
With browsers you could also use JavaScript:
发送击键的替代方法是使用 GUI 脚本。
警告:虽然 GUI 脚本比为应用程序的给定版本发送击键更强大,但应用程序的布局在未来会发生变化版本可能会破坏您的代码。
另外:
GUI 脚本需要启用对辅助设备的访问;启用需要管理员权限:
告诉应用程序“系统事件”将启用的 UI 元素设置为 true 来完成代码>(需要管理员权限)
告诉应用程序“系统事件”启用 UI 元素
将报告是否启用访问。确定正确的 UI 元素目标可能并不简单且乏味;使用
Xcode
附带的Accessibility Inspector
实用程序会有所帮助。该实用程序报告的类名对应于系统事件
字典中包含的UI元素
类;例如,AXSplitGroup
对应于splitter group
。以下内容将
Safari 6.0.3
的前窗口滚动到顶部(必须启用对辅助设备的访问):更新:提醒您这种类型的脚本运行良好对于给定版本的应用程序,必须针对
Safari 8.0.4
更改代码:The alternative to sending keystrokes is to use GUI scripting.
Caveat: While GUI scripting is more robust than sending keystrokes for a given version of an application, changes in the application's layout in future versions can break your code.
Also:
GUI scripting requires that access for assistive devices be enabled; enabling requires admin privileges:
tell application "System Events" to set UI elements enabled to true
(required admin privileges)tell application "System Events" to get UI elements enabled
will report whether access is enabled or not.Determining the right UI element targets can be non-trivial and tedious; using the
Accessibility Inspector
utility that comes withXcode
helps. The class names reported by this utility correspond to theUI element
classes contained in theSystem Events
dictionary; e.g.,AXSplitGroup
corresponds tosplitter group
.The following scrolls
Safari 6.0.3
's front window to the top (access for assistive devices must be enabled):Update: As a reminder that this type of scripting works well for a given version of an application, the code had to be changed for
Safari 8.0.4
: