如何使用 applescript 滚动到窗口顶部?

发布于 2024-10-17 04:58:56 字数 99 浏览 4 评论 0原文

我想要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

怪异←思 2024-10-24 04:58:56

基本上,使用 tell app "System Events" 语句来发送击键和键代码。
理论上,您可以使用以下内容:

keystroke page up key
keystroke page down key
keystroke home key

但对我来说这不起作用。好消息是您可以使用密钥代码。我建议使用优秀的免费完整密钥代码应用程序来读取它们,尽管它有点棘手让它读取同时按下的两个键。

fn+ 箭头键组合键代码如下:

向上翻页:fn+ 向上键键代码 116

向下翻页:fn+ 向下键键代码 121

首页:fn< /kbd>+ 左键: 键码 115

End: fn+ 右键: 键码119

例如,如果您在 Safari 中打开了一个很长的页面,并且您想要滚动到其末尾,请使用

tell application "System Events"
tell application "Safari" to activate
    — to see the animation, we wait a moment:
    delay 0.5  

    key code 119

end tell

Basically, use a tell app "System Events" statement to send keystrokes and key codes.
In theory, you could use the following:

keystroke page up key
keystroke page down key
keystroke home key

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

tell application "System Events"
tell application "Safari" to activate
    — to see the animation, we wait a moment:
    delay 0.5  

    key code 119

end tell
心凉怎暖 2024-10-24 04:58:56

通过浏览器,您还可以使用 JavaScript:

tell application "Safari" to tell document 1
    do JavaScript "window.scroll(0,0)"
end tell

tell application "Google Chrome" to tell active tab of window 1
    execute javascript "window.scroll(0,0)"
end tell

With browsers you could also use JavaScript:

tell application "Safari" to tell document 1
    do JavaScript "window.scroll(0,0)"
end tell

tell application "Google Chrome" to tell active tab of window 1
    execute javascript "window.scroll(0,0)"
end tell
深海夜未眠 2024-10-24 04:58:56

发送击键的替代方法是使用 GUI 脚本

警告:虽然 GUI 脚本比为应用程序的给定版本发送击键更强大,但应用程序的布局在未来会发生变化版本可能会破坏您的代码

另外:

  • GUI 脚本需要启用对辅助设备的访问;启用需要管理员权限:

    • 直到 10.8,这可以以编程方式系统范围通过执行告诉应用程序“系统事件”将启用的 UI 元素设置为 true 来完成代码>(需要管理员权限)
    • 遗憾的是,在 10.9+ 上,这不再有效,并且应用必须单独手动授权 - 系统将在首次运行时提示您(需要管理员权限)
    • 但是,在这两种情况下,告诉应用程序“系统事件”启用 UI 元素报告是否启用访问。
  • 确定正确的 UI 元素目标可能并不简单且乏味;使用 Xcode 附带的 Accessibility Inspector 实用程序会有所帮助。该实用程序报告的类名对应于系统事件字典中包含的UI元素类;例如,AXSplitGroup 对应于splitter group

以下内容将 Safari 6.0.3 的前窗口滚动到顶部(必须启用对辅助设备的访问):

tell application "System Events"

    # Use Accessibility Inspector to find the desired target.
    tell front window of process "Safari"
        tell scroll bar 1 of scroll area 1 of group 1 of group 1 of last group
            set value of attribute "AXValue" to 0  # Scroll to top.
        end tell
    end tell

end tell

更新:提醒您这种类型的脚本运行良好对于给定版本的应用程序,必须针对 Safari 8.0.4 更改代码:

tell application "System Events"

    # Use Accessibility Inspector to find the desired target.
    tell front window of process "Safari"
        tell scroll bar 1 of scroll area 1 of group 1 of group 1 of group 2
            set value of attribute "AXValue" to 0  # Scroll to top.
        end tell
    end tell

end tell

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:

    • up to 10.8, this could be done programmatically, system-wide by executing tell application "System Events" to set UI elements enabled to true (required admin privileges)
    • Sadly, on 10.9+, this no longer works, and apps must be authorized manually, individually - the system will prompt you on first run (requires admin privileges)
    • however, in both scenarios 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 with Xcode helps. The class names reported by this utility correspond to the UI element classes contained in the System Events dictionary; e.g., AXSplitGroup corresponds to splitter group.

The following scrolls Safari 6.0.3's front window to the top (access for assistive devices must be enabled):

tell application "System Events"

    # Use Accessibility Inspector to find the desired target.
    tell front window of process "Safari"
        tell scroll bar 1 of scroll area 1 of group 1 of group 1 of last group
            set value of attribute "AXValue" to 0  # Scroll to top.
        end tell
    end tell

end tell

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:

tell application "System Events"

    # Use Accessibility Inspector to find the desired target.
    tell front window of process "Safari"
        tell scroll bar 1 of scroll area 1 of group 1 of group 1 of group 2
            set value of attribute "AXValue" to 0  # Scroll to top.
        end tell
    end tell

end tell
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文