Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Open in Chrome
--
-- open currently open URL in Safari in Chrome
-- forked from https://gist.github.com/3151932
--
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
@prenagha

This comment has been minimized.

Show comment Hide comment
@prenagha

prenagha Jul 20, 2012

original idea from https://gist.github.com/3151932

Owner

prenagha commented Jul 20, 2012

original idea from https://gist.github.com/3151932

@Nigelw

This comment has been minimized.

Show comment Hide comment
@Nigelw

Nigelw Jul 22, 2012

Thanks - this is really useful. Only issue I have is that if Chrome is running without any open windows, the script doesn't work.

Nigelw commented Jul 22, 2012

Thanks - this is really useful. Only issue I have is that if Chrome is running without any open windows, the script doesn't work.

@prenagha

This comment has been minimized.

Show comment Hide comment
@Nigelw

This comment has been minimized.

Show comment Hide comment
@Nigelw

Nigelw Jul 23, 2012

Fix works like a charm. Thanks!

Nigelw commented Jul 23, 2012

Fix works like a charm. Thanks!

@timcamber

This comment has been minimized.

Show comment Hide comment
@timcamber

timcamber Jul 23, 2012

Another possible revision: if Chrome has a tab or two, but isn't running, then invoking the script doesn't add a tab but replaces the last-visible tab.

(I think it's extremely useful as it is, as evidenced by my version being less complete than yours, but I thought I'd point out this possible shortcoming :)

Another possible revision: if Chrome has a tab or two, but isn't running, then invoking the script doesn't add a tab but replaces the last-visible tab.

(I think it's extremely useful as it is, as evidenced by my version being less complete than yours, but I thought I'd point out this possible shortcoming :)

@prenagha

This comment has been minimized.

Show comment Hide comment
@prenagha

prenagha Jul 23, 2012

@TimArnold you must have chrome set to "continue where i left off". i have it set to always open blank. let me see if i can fix

Owner

prenagha commented Jul 23, 2012

@TimArnold you must have chrome set to "continue where i left off". i have it set to always open blank. let me see if i can fix

@prenagha

This comment has been minimized.

Show comment Hide comment
@prenagha

prenagha Jul 23, 2012

@TimArnold @Nigelw ok now even better than before. handles all cases I could come up with. let me know how it works.

Owner

prenagha commented Jul 23, 2012

@TimArnold @Nigelw ok now even better than before. handles all cases I could come up with. let me know how it works.

@Nigelw

This comment has been minimized.

Show comment Hide comment
@Nigelw

Nigelw Jul 24, 2012

This is great. Works for me in all cases I'm testing. Thanks for the updates!

Nigelw commented Jul 24, 2012

This is great. Works for me in all cases I'm testing. Thanks for the updates!

@donschaffner

This comment has been minimized.

Show comment Hide comment
@donschaffner

donschaffner Aug 21, 2012

When I launch the script it opens Chrome... but by first launching Parallels Desktop, and then Chrome for Windows [ugh]. Any thoughts on how to fix this?

When I launch the script it opens Chrome... but by first launching Parallels Desktop, and then Chrome for Windows [ugh]. Any thoughts on how to fix this?

@donschaffner

This comment has been minimized.

Show comment Hide comment
@donschaffner

donschaffner Aug 21, 2012

Ha! Renaming every instance of "Google Chrome" to "Google Chrome.app" fixed it.

Ha! Renaming every instance of "Google Chrome" to "Google Chrome.app" fixed it.

@danburbul

This comment has been minimized.

Show comment Hide comment
@danburbul

danburbul Aug 22, 2012

Great snippet, thanks.

Great snippet, thanks.

@donschaffner

This comment has been minimized.

Show comment Hide comment
@donschaffner

donschaffner Aug 23, 2012

And now it's back to doing the same thing again where it preferentially opens the page in Chrome for Windows.

And now it's back to doing the same thing again where it preferentially opens the page in Chrome for Windows.

@robjwells

This comment has been minimized.

Show comment Hide comment
@robjwells

robjwells Oct 26, 2012

Thanks for this @prenagha. I’ve just forked it, mostly to check if the current Chrome tab is empty (and if so to open the URL there).

@donschaffner My fork uses Chrome’s bundle id for everything except checking if it’s running, so that might solve your problem with Parallels & Chrome for Windows. (Unfortunately I can’t test this myself.)

Thanks for this @prenagha. I’ve just forked it, mostly to check if the current Chrome tab is empty (and if so to open the URL there).

@donschaffner My fork uses Chrome’s bundle id for everything except checking if it’s running, so that might solve your problem with Parallels & Chrome for Windows. (Unfortunately I can’t test this myself.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment