Applescript for Safari and Firefox

nealt

Registered
Anyone hove good knowledge of Applescript? I want to create a simple Applescript that involves transferring an active URL from Safari to Firefox and back.

I'd like to correspond with someone.
 
Going from Safari to Firefox is trivially easy:
Code:
tell application "Safari"
	set the_url to the URL of document 1
	tell application "Firefox"
		activate
		Get URL the_url
	end tell
end tell

Going from Firefox to Safari is not, because Firefox has really bad AppleScript support (which should be no surprise, since it's not very Mac-like in any regard). I hacked together a solution, but it involves using System Events (an invisible app which I think loads at log-in, at least by default) to simulate keypresses so you can copy the URL from Firefox's address bar.
Code:
tell application "Firefox"
	activate
	tell application "System Events"
		keystroke "l" using command down
		keystroke "c" using command down
	end tell
	tell application "Safari"
		activate
		set the URL of document 1 to (the clipboard) as text
	end tell
end tell

Keep in mind that this will OVERWRITE YOUR CLIPBOARD CONTENTS! I suppose you could adjust the script to save the contents beforehand and restore them when its done, but I'm not sure off the top of my head.

Hope this helps.
 
Thanks for the scripts. They work great. Here is why I wanted them. Sometimes a site will not render properly in one of the browsers. This script allows me to quickly switch browsers without having to type in the URL. You should publish them somewhere.
Neal
 
If you enable the Debug menu in Safari, there is an option (almost at the bottom) to open the page in any other browser you might have installed. No scripts needed.

You can also copy the url, activate Quicksilver, type . then paste the url, tab, select Open With, tab, select the browser of your choice, enter. Sounds like a lot of work, but it works and I did that faster than using Safari's Debug menu ...
 
If you have both browsers open, you could also just drag the URL icon to the other open browser window.
 
I just wrapped my two script snippets from before into a single script, if anyone's interested. If Safari is the active app, it'll take its URL and load it into Firefox. If Firefox is the active app, it'll do the reverse. I'm using it with iKey so I can easily go from one to another with the same shortcut.

Keep in mind that because of the hacky way I get Firefox's URL (by simulating command-L and command-C), if you have any modifer keys besides Command pressed when the script activates, it won't be able to get Firefox's URL. Kind of a bummer, but I don't know of any way to work around it. I personally have command-F1 set to trigger the script (when one of the browsers is in the front).

Here's the new script:
Code:
tell application "System Events"
	set the_list to the displayed name of every process whose frontmost is true
	
	if (the_list contains "Firefox") then
		tell me to firefox_to_safari(0)
	else if the_list contains "Safari" then
		tell me to safari_to_firefox(0)
	end if
end tell

on safari_to_firefox(x)
	tell application "Safari"
		set the_url to the URL of document 1
		tell application "Firefox"
			activate
			Get URL the_url
		end tell
	end tell
end safari_to_firefox

on firefox_to_safari(x)
	tell application "Firefox" to activate
	tell application "System Events"
		--WARNING: THIS OVERWRITES THE CLIPBOARD CONTENTS!
		keystroke "l" using command down
		keystroke "c" using command down
	end tell
	
	tell application "Safari"
		activate
		if the number of documents is 0 then make new document
		set the URL of document 1 to (the clipboard) as text
	end tell
end firefox_to_safari

I'm not quite sure why I need to have a parameter in my custom methods, but it gives me errors if I don't, so I'm passing in 0 to make it happy. *shrug*

Oh, and I also tried to make it save and restore the clipboard contents, like I mentioned before, but I couldn't get it working. It worked with plain text, but not styled text. So I decided not to bother. If it's not going to work for all clipboard contents, it's probably best that it doesn't work for any. Don't want to lull anyone into a false sense of security.

If anyone feels compelled to host and distribute this script, feel free.
 
For some reason I can't edit my last post, so I have to make a new post.

There's a bug with some versions of Firefox that makes it do weird things when you run my script when it's not yet loaded. To fix it, move the "activate" line after the "get url..." line in my above script. Then it works just fine. Well...at least for the versions I've tested. Firefox is so inconsistent....
 
Back
Top