# Applescript for Safari and Firefox



## nealt (Apr 1, 2005)

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.


----------



## Mikuro (Apr 2, 2005)

Going from Safari to Firefox is trivially easy:

```
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.

```
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.


----------



## nealt (Apr 3, 2005)

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


----------



## Cat (Apr 5, 2005)

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 ...


----------



## bobw (Apr 5, 2005)

If you have both browsers open, you could also just drag the URL icon to the other open browser window.


----------



## Mikuro (Apr 18, 2005)

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:

```
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.


----------



## Mikuro (Apr 25, 2005)

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....


----------



## camteddybear (Jul 27, 2008)

how do I disable debug in safari?


----------



## ElDiabloConCaca (Jul 27, 2008)

http://www.macosxhints.com/article.php?story=20030110063041629


----------

