1

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.

  • Is that the actual URL? If so, where on that page are you trying to set to e.g. just under the address bar? Is the bounds of the window always the same or would you consider setting the bounds so it's a constant? When you use the term "page" are you referring to the entire length of the html document as a single page or number of pages if printed or whats visible within the area of the page that presently displayed within the bounds of the window, etc. Is this something your trying to do for just this particular page? – user3439894 Mar 2 '17 at 2:42
  • @user3439894 I would like the top of the page to be right between the time (which currently reads "9:48 pm EST") and the "TIME DESCRIPTION TEMP FEELS PRECIP HUMIDITY WIND" column headers. But I use Adblock; I don't need to scroll past the large ads at the top of the page. The bounds of the Google Chrome window are always the same (full width and full height, from the menu bar to the Dock). When I refer to the "page", I mean the entire page as it exists in the browser window as one continuous page - not the print view or only what is visible. – rubik's sphere Mar 2 '17 at 3:04
  • Yes, right now, this is something that I am trying to implement just for this specific webpage. Down the line, I will adapt the solution for different web addresses. – rubik's sphere Mar 2 '17 at 3:05
  • Could you provide what resolution your screen is set to and the bounds of the Google Chrome window? I'm asking so as to test some code that might help get you what you want. You can use, tell application "Finder" to get bounds of window of desktop and tell application "Google Chrome" to get bounds of front window when the target URL is the front window. – user3439894 Mar 2 '17 at 3:24
  • @user3439894 Screen size: 15.4-inch Retina. Resolution: "Default for display." "(2880 x 1800)". Bounds of Chrome window (the bounds of my Chrome windows are always this size): "{0, 23, 1440, 850}". Chrome zoom setting (for the specific webpage in question): 100%. Chrome display options: I do not have the "Bookmarks Bar" or any extension-specific toolbar shown at the top of the window; only the address bar and the tabs. – rubik's sphere Mar 2 '17 at 5:01
2

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.

  • Very nice! I keep learning new things from your answers. +1 – user3439894 Mar 2 '17 at 9:00
  • Both the percentage-based solution and table-based solution work perfectly and take less time than the keystroke-down-arrow method. The second solution is amazing because it sets the scrollbar exactly where I want it. Thanks jackjr300! – rubik's sphere Mar 2 '17 at 18:56
2

Based on the additional information you provided in the comments, let me offer the following example code of how I'd do it. (At least until I learned a better way.)

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

tell application "Google Chrome"
    set bounds of front window to {0, 23, 1440, 850}
    tell front window to make new tab at after (get active tab) with properties {URL:myURL}
    repeat while (loading of active tab of front window is true)
        delay 1
    end repeat
    activate
    tell application "System Events"
        repeat 9 times
            key code 125 -- # Down Arrow
            delay 0.1
        end repeat
    end tell
end tell

Notes:

  • The value of the delay command in the repeat while ... loop can be adjusted to something more or less as you see fit based on how fast that page actually loads, you'd want to minimize the number of times the event triggers just so you don't unnecessarily over trigger the event. It's necessary for the page to finish loading, otherwise the next repeat loop will not scroll the appropriate amount. (I'm obviously using the term scroll figuratively.)

  • In the tell application "System Events" statement/block, you might need to adjust the number of time the repeat loop triggers. The delay command in this repeat loop is necessary otherwise the key code event, in this use case, does not register on the page properly.

  • Wow, that's funny. Your solution is almost the same as my original attempt, verbatim. I couldn't get it to work with a delay 1, so I incorrectly assumed that my method was bad. When I change your delay 1 to delay 3, the code now works properly on my computer. Your solution accomplishes what I desire, but I am very surprised that AppleScript does not offer a more elegant method of scrolling. – rubik's sphere Mar 2 '17 at 6:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .