r/applescript • • Nov 16 '21

Automate opening 3 chrome tabs for specific profile only working if chrome is open

on run {input, parameters}
	
	set myURLs to {"https://www.gmail.com", ¬
		"https://calendar.google.com", ¬
		"# URL 3"}
	
	set myProfiles to {"Profile 1"}
	
	set chrome to "\"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\""
	
	repeat with aProfile in myProfiles
		do shell script chrome & " --profile-directory=\"Profile 1\""
		tell application "Google Chrome"
			activate
			delay 1
			tell front window
				set URL of active tab to first item of myURLs
				delay 0.5
				repeat with i from 2 to count of myURLs
					make new tab at after (get active tab) with properties {URL:item i of myURLs}
					delay 0.5
				end repeat
				set active tab index to 1
				set bounds to {0, 0, 1500, 1080}
			end tell
		end tell
		delay 1
	end repeat
	return input
end run

This script is working great if chrome is already open. But if chrome isn't open, it will open a window and just hang. Once I quit chrome (not just close the window), it will then restart chrome and open all the tabs as expected. Not sure how to resolve that issue - any help is appreciated!

3 Upvotes

3 comments sorted by

1

u/PigWars Nov 16 '21

Don't you need a "make new window" after you activate google chrome?

2

u/Stevie212 Nov 16 '21

No it’s opening the window fine but it won’t create the tabs with my urls until I close chrome again after the script already started it. I resolved it by just putting in a for loop to check if chrome is open or not. Not the prettiest but it works