AppleScript to open current Safari page in Google Chrome via LaunchBar or TextExpander

Jul 24, '12 07:30:00AM

Contributed by: kirkmc

We ran a hint last month about opening the current Safari page in Chrome, but Padraic Renaghan came up with a nice AppleScript that he uses to open the page in Chrome via a LaunchBar action.

property theURL : ""
tell application "Safari"
	set theURL to URL of current tab of window 1
end tell

if appIsRunning("Google Chrome") then
	tell application "Google Chrome"
		if (count of (every window where visible is true)) is greater than 0 then
			-- running with a visible window, ready for new tab
		else
			-- running but no visible window, so create one
			make new window
		end if
	end tell
else
	tell application "Google Chrome"
		-- chrome app not running, so start it
		do shell script "open -a \"Google Chrome\""
	end tell
end if

-- now that we have made sure chrome is running and has a visible
-- window create a new tab in that window
-- and activate it to bring to the front
tell application "Google Chrome"
	tell front window
		make new tab with properties {URL:theURL}
	end tell
	activate
end tell

on appIsRunning(appName)
	tell application "System Events" to (name of processes) contains appName
end appIsRunning
He got the original idea from another AppleScript to open Chrome via TextExpander , created by Tim Arnold. Here's his script:
property theURL : ""
tell application "Safari"
	set theURL to URL of current tab of window 1
end tell
if appIsRunning("Google Chrome") then
	tell application "Google Chrome"
		make new window
		set URL of active tab of window 0 to theURL
		activate
	end tell
else
	tell application "Google Chrome"
		do shell script "open -a \"Google Chrome\""
		set URL of active tab of window 0 to theURL
		activate
	end tell
end if

on appIsRunning(appName)
	tell application "System Events" to (name of processes) contains appName
end appIsRunning
With the former, you save the script with any name you want, and type an abbreviation in LaunchBar to activate it. With the second script, you set a TextExpander abbreviation to run the script, and just type the abbreviation. Both of these scripts could be used in other applications: other launchers like LaunchBar, and other snippet expanders like TextExpander, as long as they support AppleScripts. Both are nice ways to quickly open a Safari page in Chrome.

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20120724050254162