Skip to content

Instantly share code, notes, and snippets.

@robjwells
Forked from prenagha/Open In Chrome.scpt
Last active March 24, 2020 02:32
Show Gist options
  • Save robjwells/3958402 to your computer and use it in GitHub Desktop.
Save robjwells/3958402 to your computer and use it in GitHub Desktop.
Open current Safari URL in Chrome
-- Open current Safari URL in Chrome
--
-- Forked from https://gist.github.com/3153606
-- which was forked in turn from https://gist.github.com/3151932
tell application "System Events"
-- Check if Chrome is running
set chromeRunning to ((name of processes) contains "Google Chrome")
end tell
tell application "Safari"
-- Get the current Safari URL
set theURL to the URL of the current tab of window 1
end tell
if chromeRunning is true then
-- Bundle id used to try to avoid a bug with Parallels & Chrome for Windows
tell application id "com.google.Chrome"
if (count of (every window where visible is true)) is 0 then
-- Make a new window if there are no visible ones
make new window
end if
end tell
else
launch application id "com.google.Chrome"
end if
tell application id "com.google.Chrome"
-- Check if the current tab is empty
if the URL of the active tab of window 1 is "chrome://newtab/" then
-- If it is, open theURL in it
set the URL of the active tab of window 1 to theURL
else
-- Otherwise make a new tab and open theURL
tell window 1 to make new tab with properties {URL:theURL}
end if
activate
end tell
@robjwells
Copy link
Author

All the hard work for this done by @prenagha. This fork has two main differences:

  1. If the current Chrome tab is empty it opens the URL there.
  2. It uses Chrome’s bundle id to try to get round a bug with Parallels & Chrome for Windows.

The latter’s mostly a hunch, and I can’t test it so I don’t know if it works. (It’s a response to @donschaffner’s comments on the original gist.)

Generally the code looks a bit different, and that’s because I rewrote it line-by-line so I knew exactly what was going on, and also to match my own idiosyncrasies (like replacing the single-use handler with a simple tell block).

Usage: I’d recommend activating it from LaunchBar or FastScripts. Personally, I have it saved to ~/Library/Scripts/Applications/Safari and call it through FastScripts with Control-Option-Command-C (⌃⌥⌘C).

PS. Most of the revisions since my first are minor fiddling with meta stuff, such as changing the file extension to force GitHub’s syntax highlighting.

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