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.
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
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
:With browsers you could also use JavaScript: