image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old April 1st, 2005, 03:50 PM
Registered User
 
Join Date: Apr 2005
Posts: 113
Thanks: 0
Thanked 6 Times in 6 Posts
nealt is on a distinguished road
Applescript for Safari and Firefox

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.
Reply With Quote
  #2  
Old April 2nd, 2005, 09:35 PM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,506
Thanks: 4
Thanked 15 Times in 14 Posts
Mikuro is on a distinguished road
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.
Reply With Quote
  #3  
Old April 3rd, 2005, 08:46 PM
Registered User
 
Join Date: Apr 2005
Posts: 113
Thanks: 0
Thanked 6 Times in 6 Posts
nealt is on a distinguished road
Wink

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
Reply With Quote
  #4  
Old April 5th, 2005, 02:55 PM
Cat's Avatar
Cat Cat is offline
Registered User
 
Join Date: Jan 2003
Location: @ my Mac
Posts: 1,972
Thanks: 0
Thanked 0 Times in 0 Posts
Cat is on a distinguished road
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 ...
__________________
This is not a signature (but I could be wrong).
15" MacBook Pro C2D@2.4 GHz | 2 GB RAM | Mac OS 10.5.4 |
Website | LinkedIn | Publications
GP/O d-(+)@ s: a->? C++(+++) U* P+ L+>++ !E---- W+++ N o? K? w--- O? M++ V? PS+++ PE-- Y+ PGP t 5? X- R !tv b++++ DI+(++)@ D+(++) G++(+++) e+++$>++++$$ h--->---- r+++ y++++@
Reply With Quote
  #5  
Old April 5th, 2005, 03:01 PM
bobw's Avatar
The Late: SuperMacMod
 
Join Date: Mar 2001
Location: Phila,PA
Posts: 8,835
Thanks: 0
Thanked 5 Times in 1 Post
bobw has a spectacular aura aboutbobw has a spectacular aura about
If you have both browsers open, you could also just drag the URL icon to the other open browser window.
__________________

Reply With Quote
  #6  
Old April 18th, 2005, 02:36 PM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,506
Thanks: 4
Thanked 15 Times in 14 Posts
Mikuro is on a distinguished road
Post Updated Safari <-> Firefox script

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.
Reply With Quote
  #7  
Old April 25th, 2005, 09:58 PM
Mikuro's Avatar
Crotchety UI Nitpicker
 
Join Date: Mar 2005
Posts: 2,506
Thanks: 4
Thanked 15 Times in 14 Posts
Mikuro is on a distinguished road
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....
Reply With Quote
  #8  
Old July 27th, 2008, 12:07 PM
Registered User
 
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
camteddybear is on a distinguished road
how do I disable debug in safari?
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Switch from safari to firefox sum_ifx_i Mac OS X System & Mac Software 37 August 1st, 2005 08:35 AM
Should Apple Ride the FireFox Wave? jeb1138 Apple News, Rumors & Discussion 18 March 2nd, 2005 04:32 PM
different result in safari and firefox j2603 Design & Media 1 November 19th, 2004 03:31 PM
Firefox Security Update bobw Mac OS X System & Mac Software 2 October 2nd, 2004 07:10 PM


All times are GMT -5. The time now is 11:31 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.