1

So a great guy helped me (Automator: Applescript - Open a specific Google Chrome profile and use extension). Now, I want to implement the next step into this.

I can now run a specific Chrome profile and use a specific extension function that basically opens a Chrome setup that I saved (various websites opening with specific dimensions on my monitors and with specific placements)

This is the script:

set chrome to "\"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\""
do shell script chrome & " --profile-directory=\"Profile 6\"" & " > /dev/null 2>&1 &"

tell application "Google Chrome" to activate
delay 0.25
tell application "System Events"
    click ¬
        pop up button "Multi Layout Manager" of ¬
        toolbar 1 of group 1 of window 1 of ¬
        application process "Chrome"
    delay 0.25
    repeat 3 times
        key code 48 -- # tab key
        delay 0.2
    end repeat
    key code 36 -- # enter key
end tell

What I want now, is for some of these websites to change their scrollbars so that I can see the part of the website that interests me in front of me.

I've found this script that works but only by itself.

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

I tried, but my knowledge is limited and can't make the two of them work together.

On its simple form, I want the script to pick one of the sites that the first script opens and change its scrollbar.

Any help would be appreciated.

UPDATE

I managed to make it work using this script:

delay 3
tell application "Google Chrome" to tell active tab of window 1
    execute javascript "window.scroll(0,270)"
end tell

The issue now is that it takes the active tab and applies the scroll function. I want it to find a different tab and apply it.

The tab is in a different window though.

Any way around it?

1
  • I've rolled back your last edit as it answers your question and that is not how things are done here. You can self-answer your question by posting an answer of how you resolved your issue. Thanks. Sep 3, 2021 at 13:33

2 Answers 2

0

If you want to open a Safari url in a new tab rather than in a new window you can do

tell application "Safari"
    tell window 1
        set current tab to (make new tab with properties {URL:"$1"})
    end tell
end tell

If you want to switch to a tab with a specific url

    tell application "Safari"
    set desired_tab to null
    set tablist to every tab of window 1
    repeat with t in tablist
        if URL of t is "https://www.google.com/" then
            set desired_tab to t
        end if
    end repeat
    
    if desired_tab is not equal to null then
        set current tab of window 1 to desired_tab
    end if
end tell

Be careful though the url here needs to be an exact match

Get it in the javascript console if you need to with

window.location.href

This example shows selecting a [https://stackoverflow.com/questions/30242098/applescript-find-open-tab-in-safari-by-name-and-open-it][safari tab by name in applescript]

1
  • thank you! I managed to make it work but this could be useful for a future reference. Sep 6, 2021 at 14:28
0

The fix was pretty simple. I just had to put window 2.

delay 3
tell application "Google Chrome" to tell active tab of window 2
    execute javascript "window.scroll(0,270)"
end tell
1
  • 1
    Please add further details to expand on your answer, such as working code or documentation citations.
    – Community Bot
    Sep 6, 2021 at 14:57

You must log in to answer this question.

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