open item in a new tab

Is there any easier way to write this - methinks there is:


set my_URL to "http://macscripter.net/"
set URL_open to false
tell application "System Events" to set safari_open to (get name of every application process) contains "Safari"
tell application "Safari"
	set URL_list to get URL of every tab of every window
	set t to count of URL_list
	repeat t times
		set URL_open to item t of URL_list contains my_URL
	end repeat
	if safari_open then
		activate
		delay 0.3
		if not URL_open then tell application "System Events" to keystroke "t" using command down
	else
		activate
		close every window
	end if
	open location my_URL
end tell

Basically, I just have it get all URL’s of all tabs of all windows and it that list contains my_URL and Safari is open, then open my_URL in a new tab. If Safari isn’t open, simply open my_URL

maybe you can tweak this script…

tell application "Safari"
	activate
	my new_Tab()
	set URL of document 1 to "http://macscripter.net/"
	my new_Tab()
	set URL of document 1 to "http://204.122.16.85/"
	my new_Tab()
	set URL of document 1 to "http://www.geekculture.com/nitrozacpaintings/images/NP0032large.jpg"
	my new_Tab()
	set URL of document 1 to "http://www.slipperybrick.com/wp-content/uploads/2007/04/mac-osx-leopard.jpg"
	my new_Tab()
	set URL of document 1 to "http://www.apple.com/"
end tell
on new_Tab()
	tell application "Safari"
		tell application "System Events" to tell process "Safari"
			click menu item "New Tab" of menu "File" of menu bar 1
		end tell
	end tell
end new_Tab

You can use make to make a new tab in a window with a given URL:

tell application "Safari"
	set W to first item of (windows whose visible is true) -- item in list of items
	set T to tabs of W -- get the tabs if you want them
	tell W to make new tab with properties {URL:"http://google.com"} -- you have to tell the window to do it.
end tell

Like zo:

tell application "Safari"
	activate
	set W to first item of (windows whose visible is true) -- item in list of items
	tell W
		make new tab with properties {URL:"http://google.com"}
		make new tab with properties {URL:"http://macscripter.net"}
		make new tab with properties {URL:"http://apple.com"}
	end tell
end tell

or for a bunch of them:

set myURLs to {"http://macscripter.net", "http://scriptbuilders.net", "http://forums.macosxhints.com/"}

tell application "Safari"
	activate
	set W to first item of (windows whose visible is true) -- item in list of items
	tell W
		repeat with oneURL in myURLs
			make new tab with properties {URL:oneURL}
		end repeat
	end tell
end tell

To be a little more specific, is there a simple way to check to see if a URL is already open in any tab in any window?

try this


set a to "http://www.apple.com/"
tell application "Safari"
	set flag to false
	repeat with i in (get URL of tabs of windows whose visible is true)
		if a is in contents of i then
			set flag to true
			exit repeat
		end if
	end repeat
end tell
flag

Or building on Stefan’s script with some error checking:

set myURLs to {"http://macscripter.net/", "http://scriptbuilders.net/", "http://forums.macosxhints.com/"}

tell application "WebKit" -- works equally well in Safari
	activate
	try -- in case no windows are visible
		set W to first window whose visible is true -- item in list of items
	on error -- no window showing when app is active.
		set W to make new document
	end try
	tell W
		try -- in case there are no tabs
			set T to URL of tabs
		on error -- only one document, no tabs.
			set T to {}
		end try
		repeat with oneURL in myURLs
			if contents of oneURL is not in T then
				make new tab with properties {URL:oneURL}
			end if
		end repeat
	end tell
end tell