How to scroll the top of a window using applescript?

I want applescript to scroll the window completely.

I tried the page key, home key, and I tried to look for a way to scroll using the built-in window scrolling capabilities, but so far I have not been able to even move the scroll position at all.

+4
source share
3 answers

Basically, use the tell app "System Events" statement to send keystrokes and key codes. Theoretically, you can use the following:

 keystroke page up key keystroke page down key keystroke home key 

But for me it does not work. The good news is that you can use key codes instead. I suggest using the excellent free Full Key Codes reader app, although itโ€™s a bit difficult to make it read two keys pressed simultaneously.

The key codes for fn + arrow keys -combos are as follows:

Page up: fn + up key : key code 116

Page down: fn + down key : key code 121

Main: fn + left key : key code 115

End: fn + right key : key code 119

So, for example, if a long page was opened in Safari and you want to scroll to the 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 
+8
source

In browsers, you can 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 
+4
source

An alternative to sending keystrokes is to use GUI scripts .

Caveat . Although GUI scripts are more reliable than sending keystrokes for a given version of an application, changes to the layout of the application in future versions may code .

also:

  • For GUI scripts, you need to activate access for assistive devices ; Administrator privileges are required to enable:

    • Before 10.8, this can be done programmatically, system-wide by running the tell application "System Events" to set UI elements enabled to true (required administrator rights)
    • Unfortunately, at 10.9+ this does not work anymore, and the applications must be authorized manually, individually - the system will prompt you to execute first (administrator privileges are required)
    • however, in both scenarios the tell application "System Events" to get UI elements enabled will indicate whether access is enabled or not.
  • Finding the right target user interface elements can be nontrivial and tedious ; using the Accessibility Inspector utility that ships with Xcode . The class names specified by this utility correspond to the UI element classes contained in the System Events dictionary; for example, AXSplitGroup corresponds to a splitter group .

The following Safari 6.0.3 scrolling window is at 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 . Recall that this type of script works well for this version of the application, the code should have been 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 
+1
source

Source: https://habr.com/ru/post/1339306/


All Articles