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 scro
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 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