Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I scroll to the top of a window using 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.

like image 274
Roman Zabicki Avatar asked Feb 11 '11 04:02

Roman Zabicki


1 Answers

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
like image 194
Asmus Avatar answered Sep 29 '22 07:09

Asmus