1

I want to make an Automator app which closes any open tabs in Google Chrome and loads 3 specific tabs. It should load these 3 tabs regardless of whether Google Chrome was already in use or not.

To accomplish this my app runs the following Applescript:

# Close whatever is already open in Chrome
tell application "Google Chrome"
    close every window
end tell

Next, it uses the "Get Specified URLs" action to understand the list of URLs which I will want to load as tabs. Finally, these URLs are passed into the following Applescript for loading:

# input is the list of url's from the previous task
on run {input, parameters}

    # The below is an applescript loop
    repeat with theURL in input
        tell application "Google Chrome" to open location theURL
    end repeat

    # We must return something so we just return the input
    return input

 end run

When I actually run it, it sometimes works. Other times it does nothing (Chrome is running but has no window or tab loaded) or it opens the 3 new tabs along with whatever tabs were already open when the app was run.

At first I thought perhaps it was being affected by a browser preference which tells Chrome what you would like to have open on startup. I initially had Chrome set to open Gmail. I changed this preference to open only a blank new tab. This did not affect the results (except that when the app otherwise runs successfully now there is an additional blank new tab open, which isn't ideal but I don't care about much).

2
  • RE: "# We must return something so we just return the input" -- You only need to return something if there is another action in the workflow directly after that Run Shell Script action that requires input! That said, and without seeing your entire workflow, but from what you've shown so far, this could all be in a single Run AppleScript action. Commented Apr 21, 2021 at 19:03
  • @user3439894 Thanks for that. This is the entire app so far (or workflow, but technically I made it an app so I can run it outside of Automator). If making it into 1 AppleScript would help solve the problem I'd be very open to that.
    – Hack-R
    Commented Apr 21, 2021 at 19:10

2 Answers 2

1

If an application is what you want, then the following example AppleScript code can be saved as an AppleScript application in Script Editor:

set myURLs to {"https://www.example.com", ¬
    "https://www.apple.com", ¬
    "https://apple.stackexchange.com"}

tell application "Google Chrome"
    activate
    close windows
    set winID to id of (make new window)
    tell window id winID
        set URL of active tab to first item of myURLs
        repeat with i from 2 to (length of myURLs)
            make new tab at end of tabs ¬
                with properties {URL:item i of myURLs}
        end repeat
        set active tab index to 1
    end tell
end tell
5
  • This works fine for me, thank you very much. I was trying to use the Automator to create the app, but hey this does the trick! I assume I can also re-integrate this into the Automator to help me add additional actions later anyway if I want. I'm still a little curious what was causing the weird behavior I encountered, but it's not super important. This works great.
    – Hack-R
    Commented Apr 21, 2021 at 20:12
  • @Hack-R, Yes, the example AppleScript code can also be incorporated into a Run AppleScript action in an Automator workflow. As to the issue your were having, without me creating an Automator workflow for testing I can't say for sure, however I do not believe you will have the same issue with my example AppleScript code used an AppleScript application. Commented Apr 21, 2021 at 20:25
  • Sounds good, thanks again. You know that Automator can make applications as well as workflows, right? That's why I was using that term. Not important though. This is great.
    – Hack-R
    Commented Apr 21, 2021 at 20:29
  • @Hack-R, Yes I'm aware of the capabilities of Automator. I'm also aware that an AppleScript application typically runs faster than the equivalent saved as an Automator application and why when I only need to use AppleScript I go that way. Commented Apr 21, 2021 at 20:52
  • Right on! Makes sense
    – Hack-R
    Commented Apr 21, 2021 at 20:53
0

Even though another answer was already selected as the accepted answer, it never hurts to view alternate techniques, which return the same desired results.

property theURL : {"https://apple.stackexchange.com/questions/tagged/applescript", ¬
    "https://stackexchange.com/", "https://apple.stackexchange.com"}

tell application "Google Chrome"
    activate
    close every window
    tell its (make new window)
        set URL of tab 1 to theURL's item 1
        repeat with i from 2 to count of theURL
            set URL of (make new tab) to theURL's item i
        end repeat
        set active tab index to 1
    end tell
end tell
1
  • Thank you. I think this is the version I had in my question but if I'm wrong just let me know.
    – Hack-R
    Commented Apr 23, 2021 at 15:24

You must log in to answer this question.

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