Opening tabs in Safari with Automator and Applescript

I wasn’t sure whether to put this in the Automator forum or here so if it doesn’t belong here then please move it.

I made an automator script/application that basically just open 6 windows in Safari of pre-selected URL’s that I want open. What I want to do is, instead of opening separate windows for each URL, I want it to open the URL’s in tabs in one Safari window.

So with a little help from Apple’s applescript help site I put the following applescript together:

on run {input, parameters}
	tell application "Safari"
		try
			set the URL_list to the URL of every document
			repeat with i from the (count of the the URL_list) to 2 by -1
				set this_URL to item i of the URL_list
				my new_tab()
				set the URL of document 1 to this_URL
				close window i
			end repeat
		on error the error_message number the error_number
			display dialog the error_message buttons {"OK"} default button 1
		end try
	end tell
end run

on new_tab()
	tell application "Safari" to activate
	tell application "System Events"
		tell process "Safari"
			click menu item "New Tab" of menu "File" of menu bar 1
		end tell
	end tell
end new_tab
 

So basically, Automator gets the URL’s and opens each URL in its own window and then this script is run that is supposed to put the URL’s into tabs in one Safari window. For some reason I keep getting this error NSUnknowKeyScriptError with an Okay button to hit and it stops the script from finishing. The error comes up when it tries to put the second window/URL into tab#2 in Safari.

Any ideas, or is there an easier way to do this?

I’m running Tiger 10.4.1

AppleScript: 2.0
Browser: Firefox 1.0.4
Operating System: Mac OS X (10.4)

I generated this same workflow/script independently today and it worked fine but I did notice a few differences

  1. “on run…” was not present (not really sure if that would make a big diff or not)
  1. I think this “end” is throwing the script off

without those 2 elements my workflow ran with no issues.

If you don’t want to do this in Automator I also have an AS that does the same thing but forgos the opening of seperate windows and opens the URLs directly in a tab.


tell application "Safari"
	activate
end tell
tell application "System Events"
	tell process "Safari"
		tell menu bar 1
			click menu item "New Tab" of menu "File"
			open location "http://www.apple.com"
		end tell
		tell menu bar 1
			click menu item "New Tab" of menu "File"
			open location "http://www.mac.com"
		end tell
		tell menu bar 1
			click menu item "New Tab" of menu "File"
			open location "http://www.macscripter.net"
		end tell
	end tell
end tell

I suppose similar results might be achieved with:

tell application "Safari" to activate
repeat with loc in {"http://www.apple.com", "http://www.mac.com", "http://www.macscripter.net"}
	tell application "System Events" to keystroke "t" using command down
	tell application "Safari" to set front document's URL to loc
end repeat

On my machines, just using ‘open location’ automatically opens each page in a new Safari tab. Of course, Safari has to be the default browser for this and tabbed browsing has to be enabled in its preferences.

repeat with loc in {"http://www.apple.com" ,"http://www.mac.com" ,"http://www.macscripter.net" }
	open location loc 
end repeat

Strange… I tried that earlier, Nigel - and again just now. For some reason, it doesn’t work here. Each URL is opened in the first tab of a new window…

[Mac OS X Version 10.4.2 (8C46) Safari Version 2.0 (412.2)]

Ah. I know what it might be. At the bottom of the “General” panel in Safari’s preferences, there’s a choice of how to open links from applications. I’m set up to open links in a new tab in the current window. You’ve probably got the other setting: to open them in a new window.

Your script, kai, and chipmaker’s, work with either setting ” but have subtle differences. For me, they both create a blank tab if one of the URL’s is the “Home Page”. If I run them twice in succession, kai’s version duplicates the previously created new tabs, while chipmaker’s creates that number of blank tabs. My ‘open location’-only effort opens a Home Page (if specified) and doesn’t create extra tabs when run twice.

I can’t think why GUI scripting should affect the loading of a Home Page. (Maybe it’s just my Home Page.) ‘Open location’ apparently only acts when the URL’s not already loaded ” which would explain chipmaker’s blank tabs on the second run and my lack of extra tabs.

There doesn’t seem to be an entirely satisfactory way to impose new tabs regardless of the user’s settings. :expressionless:

Bingo! Thanks for clearing that up, Nigel. For some reason, that possibility hadn’t occurred to me. (I sometimes worry about myself, I really do!) :wink:

Apparently not. As far as I can see, there’s no elegant way to get the properties (name, URL, etc.) of existing tabs. I just had to resort to flicking through each tab to get a list of URLs - against which I could then compare the target URLs. So if a tab doesn’t already contain a desired location, a new tab/location could then be created. (Verrrry clunky - and way too nasty to post here!) :confused:

Please give some details on how to cross-check existing tab URL’s.
Apple again broken the Safari AS dictionary (and part of my heart)

Eelco Houwink