Use AppleScript to open current Safari URL in Google Chrome
I've been using John Gruber's suggestions from Going Flash-Free on Mac OS X, and How to Cheat When You Need It to avoid installing Adobe Flash by using Google Chrome (which includes its own version of Flash) whenever I run into a page that has Flash I want to see.
To make this easier, John suggested turning on the "Develop menu" in Safari's "Advanced" preferences, which includes a sub-menu to "Open Page With" and a sub-sub-menu that shows all of your installed browsers. John suggested using System Preferences to create a keyboard shortcut for "Google Chrome" or "Google Chrome.app" depending on which one you saw in the menu.
Unfortunately, this failed for me quite often. Every time I launched Safari, the keyboard shortcut would not work until I had opened that menu manually using the mouse. I hate using the mouse. After opening the menu, the keyboard shortcut would work until I quit Safari again. That was mildly annoying, but things recently took a turn for the worse.
Here's what the menu looks like for me now:

Notice that the browser listings now include version numbers. This means that a keyboard shortcut would have to include the version number, which means it would break whenever the browser is updated.
I asked a few folks, and it appears this changed in Safari 5.0.4. I haven't been able to find a way to revert to the old behavior, so I started looking for another way.
Enter AppleScript
What I needed was a way to open the current Safari page to Google Chrome. After my own attempts at cobbling together an AppleScript solution failed, John Welch was kind enough to provide the the answer via Twitter:
property theURL : ""
tell application "Safari"
set theURL to URL of current tab of window 1
end tell
tell application "Google Chrome"
set URL of active tab of window 1 to theURL
activate
end tell
(I added the "activate" line to make Google Chrome active, which is what I wanted.)
You can download the .scpt file here or copy the above code into AppleScript Editor.app.
I saved the script as an AppleScript file in ~/Library/Scripts/Applications/Safari/Open-Safari-URL-in-Chrome.scpt and then used FastScripts to assign a keyboard shortcut (F2, but you can choose anything you want) to the script.
FastScripts is a great program that runs in your menu bar and gives you easy access to AppleScripts, either with the mouse or by letting you assign keyboard shortcuts to them. You can assign up to 10 keyboard shortcuts for free. If you need more than that, a license is $15. It even makes it easy to put certain scripts where they will only be seen by certain applications.
It may be possible to do something similar via Apptivate, LaunchBar, Alfred, Quicksilver or your favorite launcher. If someone comes up with a workable solution for one of them, please let us know in the comments. For me, FastScripts was the easiest and best tool in my toolbox.
What we really need is for someone to write a Safari extension, like ClickToFlash, that will send the current page to Chrome. Free name suggestion: "ClickToChrome." Until then, this works even better than the original solution, as the keyboard shortcut now works much more reliably.
Troubleshooting
A few times I have seen this error:
Error Number:Google Chrome got an error: Can't get window 1. Invalid index.
-1719
But it seems to go away if I restart Chrome and/or Safari. I cobbled together this alternative based on a post at Stackoverflow.com:
set backupClipboard to the clipboard
tell application "System Events"
keystroke "lc" using command down
end tell
delay 0.2 -- to make sure keystroke will hit cmd+l & cmd+c
tell application "Google Chrome"
open location (the clipboard)
activate
end tell
set the clipboard to backupClipboard
It seems less elegant and more "hacky" (it is simulating keystrokes to copy the URL from the address bar to the clipboard and then pastes it into Chrome), but if you want to use this instead you can download it here.
Update: In the comments below, Rob suggests that the first script fails when Chrome is running but doesn't have a window open, and suggests a third alternative, which you can download here.
Share
I've been using John Gruber's suggestions from Going Flash-Free on Mac OS X, and How to Cheat When You Need It to avoid installing Adobe...
Add a Comment
For Quicksilver, assign this to a Trigger:
Open-Safari-URL-in-Chrome.scpt>Run
http://dl.dropbox.com/u/157506/Script%20Run%20Action.png
(Excuse the custom script icons.) Make sure the script is in QS's catalog first.
Oh, this just gets better.
The following code works with ANY browser that happens to be open when you invoke it. It does not rely on the "activate" command for the source browser.
I have it assigned to ctrl-C. When I'm in any browser and see a Flash-festooned page I want to fully load, I just hit that key combination, and up pops Chrome with the page loaded. Bam.
So now it's totally painless to remove Flash from your system. Just have Chrome installed, and it'll do the job for you with a single keystroke.
property theURL : ""
on run {input, parameters}
tell application "System Events"
keystroke "l" using {command down} -- Highlight the URL field.
keystroke "c" using {command down}
end tell
delay 0.1
tell application "Google Chrome"
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of front window to the clipboard
activate
end tell
return input
end run
BTW, the code in #38 also works for Safari, if you substitute its name for Firefox's. It appears that cmd-L to highlight the URL works in any browser. Cool.
March 17 2011 at 7:07 PM Report abuse Permalink rate up rate down ReplyReplying to my own post... the following works for Firefox!
property theURL : ""
tell application "Firefox" to activate
tell application "System Events"
keystroke "l" using {command down} -- Highlight the URL field.
keystroke "c" using {command down}
end tell
delay 0.5
tell application "Google Chrome"
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of front window to the clipboard
activate
end tell
The various scripts provided by you helpful and knowledgeable readers work great here. EXCEPT I use Firefox for most browsing, rather than Safari. Replacing "Safari" with "Firefox" in this script does not work:
property theURL : ""
tell application "Safari"
set theURL to URL of current tab of window 1
end tell
tell application "Google Chrome"
if (count of (every window where visible is true)) is greater than 0 then
tell front window
make new tab
end tell
else
make new window
end if
set URL of active tab of front window to theURL
activate
end tell
...Attempting to run this in the AppleScript Editor results in "Syntax Error: Expected end of line but found property" ...with the word "tab" highlighted by the Editor.
Is there any way to make this work with Firefox? Many thanks in advance.
A bit off topic, but after one becomes essentially Flash free in browsers, is there something to deal with those of us that read most stuff in an RSS app (like NetNewsWire, which I'm typing in now)?
March 15 2011 at 4:08 PM Report abuse Permalink rate up rate down ReplyI would like to modify this script for cross browser testing. I normally use Chrome and would like the script to open the current chrome page in.. Safari & Firefox.
I tried switching the programs and I get an error...
property theURL : ""
tell application "Google Chrome"
set theURL to URL of current tab of window 1
end tell
tell application "Safari"
set URL of active tab of window 1 to theURL
activate
end tell
Whats the problem?
Switching only the programs doesn't work. You have to switch "current tab" and "active tab" as well. The following script should work:
property theURL : ""
tell application "Google Chrome"
set theURL to URL of active tab of window 1
end tell
tell application "Safari"
set URL of current tab of window 1 to theURL
activate
end tell
Look up, I already posted your answer yesterday :)
set currentURL to missing value
tell application "Google Chrome"
if (exists window 1) then
if (URL of active tab of window 1 does not start with "chrome://") then
set currentURL to URL of active tab of window 1
end if
end if
end tell
if currentURL is not equal to missing value then
tell application "Safari"
activate
open location currentURL
end tell
end if
You can replace "Safari" with "Firefox" and it will also work. Or you can duplicate that last tell block, making one for each of Safari and Firefox, to have the script simultaneously open the location in *both*. (If you're doing that, you might want to remove the "activate" commands so they'll open the URL in the background)
You can use an Automator service workflow to contain this sort of thing without using something like Fastscripts. OS X then lets you assign a keyboard shortcut to the workflow (I used cmd-option-C), and it appears in the Services menu.
I created a Service Workflow with 2 actions:
1) Get Current webpage from Safari
2) Run Applescript. Here's the Applescript that I used.
on run {input, parameters}
--display dialog (input as text)
tell application "Google Chrome"
activate
try
get properties of window 1
on error
make new window
end try
tell window 1
make new tab with properties {URL:(input as text)}
end tell
end tell
end run
The Safari dictionary needs an entry to allow access to the User Agent list. This would be PERFECT if there was a companion script to tell Safari to report as the iPad user agent. Sadly, the dictionary appears to be bereft. Any ideas?
@ geekboy, My Safari's Develop -> User Agent menu has "Safari iOS 3.2.2 â iPad" as an option. Is that not what you're looking for?
@ Joe, thanks for the extension! Trying it out now.
Yes, I know the option is there, I want a way to call it via Applescript.
Are you sure about that extension? Doesn't install like a normal Safari extension ...
Ah - THAT is the tab strategy. Perfect. HATE new windows (except sometimes) love love LOVE new tabs.
Rob gets one (1) vote for Awesome Guy O' The Week.
Hot Apps on TUAW
Deals of the Day
more deals- Used Apple iPhone 3G 8GB for AT&T for $108 + $5 s&h
- Apple Mac Pro Xeon 6-Core 3.3GHz Desktop w/ 12GB RAM for $3,899 + $28 s&h
- Apple MacBook Pro Core i7 Quad 2.2GHz 15" SSD Laptop for $2,447 + $13 s&h
- Apple Earphones with Remote and Mic for $6 + $2 s&h
- PC Micro Store sale: Up to 50 off
- USB MP3 Player FM Transmitter with remote for $6 + free shipping
Software Updates
more updates- EFI Firmware Update brings Lion Internet Recovery to 2010-model Macs
- OS X Lion 10.7.3 released with Safari 5.1.3, Wi-Fi bug fix
- Aperture updated to 3.2.2, addresses Photo Stream issue
- Apple updates Keynote to address Lion issues
- Google Search app gets new look on iPad
- Apple releases Apple TV Software Update 4.4.3



42 Comments