MacOS – How to change scrollbar position in AppleScript

applescriptautomationgoogle-chromemacosscrolling

I have an AppleScript .scpt file that is assigned to a keyboard shortcut via the third-party application, FastScripts.

The script opens a specific, fixed, pre-determined URL in a new Google Chrome tab. This new tab is inserted immediately after the currently open Google Chrome tab (if Google Chrome is currently open).

Here is this code:

activate application "Google Chrome"

set myURL to "https://weather.com/weather/hourbyhour/l/12345"

tell application "Google Chrome"
    activate
    tell front window to make new tab at after (get active tab) with properties {URL:myURL}
end tell

An additional operation that I would like to implement in this script is to have the scrollbar of the opened webpage in Chrome automatically move downwards a bit. Just like the URL variable, the scroll amount is specific, fixed, and pre-determined.


I don't know the proper way to articulate a scroll amount to you in writing (and it of course varies based on one's screen resolution, monitor size, window size, and the zoom setting set in Chrome for that given page), but I can say that, for my unique environment, it is around 15% down the page. To clarify the meaning of this description, "100% down the page" would be at the lowest scrollbar position (the scrollbar can be set to this position by pressing ⌘ command + ).

The exact scroll amount that I desire is probably not particularly relevant in the context of my question (considering the many aforementioned variables that may affect the scroll amount). It is simply important to note that the scroll amount is a custom figure, as opposed to being at the very top or very bottom of the page (since one can use the respective keyboard shortcut via key code in AppleScript to easily accomplish scrolling to these two locations).

I am hoping that whatever solution is suggested offers me the ability to fine-tune and adjust the desired scrollbar location, so that I can get it as close as possible to where I would scroll manually.

Best Answer

You can use the JavaScript to set the scroll amount.

To adjust the scroll with a defined percentage:

set myURL to "https://weather.com/weather/hourbyhour/l/12345"
set scrollAmount to "16" --- % down the page

tell application "Google Chrome"
    activate
    tell front window to set curTab to make new tab at after (get active tab) with properties {URL:myURL}
    tell curTab
        repeat while (loading)
            delay 1
        end repeat
        -- set the vertical scroll 
        execute javascript "h=document.documentElement.scrollHeight-document.documentElement.clientHeight; window.scrollTo(0,h*" & scrollAmount & "/100)"
    end tell
end tell

To adjust the vertical scroll at the top of an element in the page, the script can get the element with document.getElementById('twc-scrollabe') twc-scrollabe is the identifier of the table.

After that, the JavaScript use a loop to get the top of the table in the page

set myURL to "https://weather.com/weather/hourbyhour/l/12345"
tell application "Google Chrome"
    activate
    tell front window to set curTab to make new tab at after (get active tab) with properties {URL:myURL}
    tell curTab
        repeat while (loading)
            delay 1
        end repeat
        -- scroll to the top of the table
        execute javascript "e=document.getElementById('twc-scrollabe');tTop=0; do {tTop +=e.offsetTop || 0; e=e.offsetParent} while(e); window.scrollTo(0,tTop);"
    end tell
end tell

The problem with this JavaScript (on another URL), you must find the identifier in the source of the page and change it in the script, if the element does not have an identifier, you must use other methods to get the element, the JavasScript code will be different depending on the page of the site.