Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

AppleScript to open a collection of Safari tabs Web Browsers
Here's a simple AppleScript that replaces the current Safari window with a collection of tabs, each open to a different URL. I've seen several scripts that attempt to do something similar, but this one improves on them in a couple of ways:
  • It closes all of the old tabs, rather than just adding new ones.
  • It opens the new tabs directly in AppleScript, which is much faster than using system events.
The URLs in the script are just an example; obviously you'll want to replace them with others.
tell application "Safari"
  activate
  -- close all but one tab of the front window
  try
    repeat
      close tab 2 of window 1
    end repeat
  end try
  -- open the URLs in separate tabs
  tell window 1
    set URL of tab 1 to "http://weather.yahoo.com/forecast/USMD0100.html"
    make new tab with properties {URL:"http://www.weather.com/weather/today/College+Park+MD+20740"}
    make new tab with properties {URL:"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=20742"}
    make new tab with properties {URL:"http://www.accuweather.com/us/md/college-park/20742/city-weather-forecast.asp"}
    make new tab with properties {URL:"http://weather.weatherbug.com/MD/College%20Park-weather.html?zcode=z6286&zip=20742"}
  end tell
end tell

[crarko adds: I tested this, and it works as described.]
    •    
  • Currently 4.00 / 5
  You rated: 5 / 5 (5 votes cast)
 
[8,798 views]  

AppleScript to open a collection of Safari tabs | 8 comments | Create New Account
Click here to return to the 'AppleScript to open a collection of Safari tabs' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
AppleScript to open a collection of Safari tabs
Authored by: musselrock on Apr 18, '11 09:11:43AM

I've used a script like this one for several years to do my daily reading. Here is how to open specific pages on specific days.

tell application "Safari"
launch
make new document
set bounds of window 1 to {20, 22, 1184, 1012}
end tell
set thisDate to current date
set thisDay to weekday of thisDate
if month of thisDate is in {December, January, February, March} then
openAsTab("file:///Users/diorite/Sites/SkiConditions.html")
end if
if thisDay is Monday then
openAsTab("http://www.myhistorycanbeatupyourpolitics.blogspot.com/")
end if
if thisDay is Tuesday then
openAsTab("http://www.tidbits.com/tb-issues/TidBITS-current.html")
end if
if thisDay is Thursday then
openAsTab("http://www.macosxtips.co.uk/")
end if
if thisDay is Saturday or thisDay is Sunday then
openAsTab("http://www.tuaw.com/")
openAsTab("http://www.macfixit.com/")
openAsTab("http://www.macintouch.com/")
end if
openAsTab("http://www.doonesbury.com/strip/dailydose/")
openAsTab("http://www.nytimes.com/")
openAsTab("http://www.nytimes.com/pages/opinion/index.html")
openAsTab("http://www.cnn.com/")
openAsTab("http://news.bbc.co.uk/")
openAsTab("http://www.macupdate.com/")
openAsTab("http://www.macosxhints.com/")
openAsTab("http://www.tuaw.com/")
openAsTab("http://www.macnn.com/")
tell application "Safari"
activate
end tell

on openAsTab(theURL)
delay 1
tell application "Safari"
make new tab at end of tabs of window 1 with properties {URL:theURL}
end tell
end openAsTab



[ Reply to This | # ]
AppleScript to open a collection of Safari tabs
Authored by: noyz11 on Apr 18, '11 10:43:59AM
Awesome!

That is sooo much faster then what I have been using in the past.

[ Reply to This | # ]
AppleScript to open a collection of Safari tabs
Authored by: NaOH-Lye on Apr 18, '11 01:06:04PM
Good to see different approaches like this hint provides. I take a slightly different approach. This script prompts you for the site you'd like to view and then opens the choice in a new tab. For me, this is helpful for the sites I might visit a few times a day.

tell application "System Events"
	activate
	set theList to {"Yahoo", "Weather.com", "Wunderground", "Accuweather", "Weatherbug"}
	choose from list theList with prompt "Choose a page:" default items {"Yahoo"}
	set listchoice to result as text
end tell

if listchoice is "Yahoo" then
	tell application "Safari"
		activate
		tell window 1
			open location "http://www.weather.com/weather/today/College+Park+MD+20740"
		end tell
	end tell
	
else if listchoice is "Weather.com" then
	tell application "Safari"
		activate
		tell window 1
			open location "http://www.weather.com/weather/today/College+Park+MD+20740"
		end tell
	end tell
	
else if listchoice is "Wunderground" then
	tell application "Safari"
		activate
		tell window 1
			open location "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=20742"
		end tell
	end tell
	
else if listchoice is "Accuweather" then
	tell application "Safari"
		activate
		tell window 1
			open location "http://www.accuweather.com/us/md/college-park/20742/city-weather-forecast.asp"
		end tell
	end tell
	
else if listchoice is "Weatherbug" then
	tell application "Safari"
		activate
		tell window 1
			open location "http://weather.weatherbug.com/MD/College%20Park-weather.html?zcode=z6286&zip=20742"
		end tell
	end tell
	
else
	tell application "Safari"
		activate
	end tell
	
end if
end


[ Reply to This | # ]
AppleScript to open a collection of Safari tabs
Authored by: Frederico on Apr 18, '11 02:34:10PM

LOL!

Man, I must be becoming seriously curmudgeonly in my old age, because *this* hint/alternate to my eye is a hypertrophy of a fifth wheel on a hellbox made of deadwood. How many keystrokes or mouse actions do you need to invoke that?

For me, the above is rendered in Safari as Command-T followed by Command-[1-9]. Easy peasy. If I was really so annoyed by two whole keystrokes, again, it can be macroed. Top Sites could be rolled into it for an even prettier transition.

Different strokes... (:

Best

F



[ Reply to This | # ]
AppleScript to open a collection of Safari tabs
Authored by: Frederico on Apr 18, '11 01:25:04PM

Sorry, but would the author please explain why this hint is superior to 'Open in Tabs' already provided in Safari?

A Collection of Bookmarks in a Safari Folder, stored anywhere from the Bookmarks Menu to the Bookmarks Toolbar gives the option to do *exactly* what the script above offers, and also offers the advantage of leaving the first new Tab with an active Back command that will completely close all the new tabs and return you to all the old tabs if you made a mistake.

If the advantage of this script is it can be keystroke activated from a macro, so can the Safari feature.

What am I missing here? Is there really some advantage to manually editing a (admittedly low-complexity) script if you need to make a change to URL or resulting Tab order?

The above commenters modified version allowing for different tabs for different days from a single source command is the only real single-keystroke-via-macro advantage I see offered; but Safari can also have Monday-Friday/Sat-Sun options easily built, as well.

Not trying to be pissy; honestly trying to understand the purpose of adding complexity to an existing Safari function.



[ Reply to This | # ]
AppleScript to open a collection of Safari tabs
Authored by: NaOH-Lye on Apr 18, '11 02:08:08PM

The built-in Safari/OS X method for assigning a keyboard shortcut via System Preferences > Keyboard will only work if the user has one folder of bookmarks. As it is, the system for customizing keyboard shortcuts in OS X has no accommodation for menu items of the same name which occur multiple times within an applications.

Just as an example, I have about 50 bookmark folders, some nested within others, some just in the Bookmark Bar. I don't believe there's a way using built-in options to assign a shortcut to the Open In Tabs item in a specific folder. If there is, I missed it and would be interested in seeing it.

Users can run into the same issue in Mail. Menu items are duplicated in Message > Move To and in Message > Copy To. Again, though, System Preferences > Keyboard doesn't provide a way to distinguish between these menu items.



[ Reply to This | # ]
AppleScript to open a collection of Safari tabs
Authored by: Frederico on Apr 18, '11 02:12:41PM

Yes, but this script cannot be keystroked from Keyboard Shortcuts, either, unless built as a service, which would be kinda odd, but doable.

My reference to Keystroking was suggesting that somehow this script, invoked from a third party macro, such as FastScripts or Keyboard Maestro, Butler, etc., might have some advantage not revealed in the script itself.

Again, what does this script offer that Safari does not?



[ Reply to This | # ]
AppleScript to open a collection of Safari tabs
Authored by: bglnelissen on Apr 19, '11 02:45:12AM

Safari has something similar already build in. A folder in the bookmark bar can be set to auto-click. If set to auto-click, all url's in that bookmark folder will open in different tabs when that folder is clicked.



[ Reply to This | # ]