An open community 
of Macintosh users,
for Macintosh users.

FineTunedMac Dashboard widget now available! Download Here

Topic Options
#20268 - 01/24/12 03:53 AM A cross-apps AppleScript?
artie505 Online


Registered: 08/04/09
Sometimes I visit a Web site in Safari, discover that it doesn't support Safari, and want to revisit it in Firefox, so I'm wondering whether it would be possible to cobble up an AppleScript that would grab the site's URL from Safari, launch Firefox, bring it to the front, enter the URL in the address bar of the page to which Firefox opens (an empty page would presumably be best), and click on "enter," and whether some kind soul would cobble it up if it is possible. (I am totally daunted by even the thought of learning AppleScript.)

Seems to me that there'd be a market for it.

Thanks.
_________________________
The new Great Equalizer is the SEND button.

In Memory Of Harv: Those who can make you believe absurdities can make you commit atrocities. ~Voltaire

Top
#20269 - 01/24/12 04:23 AM Re: A cross-apps AppleScript? [Re: artie505]
dkmarsh Offline
Moderator

Registered: 08/04/09

Firefox isn't AppleScriptable, unfortunately (one of the many reasons I generally don't use it). GUI scripting (i.e., scripting the actual interface actions—button pushes, keyboard strokes, etc., rather than the Apple Events which such actions are the normal front-end to) might be a possibility, but the specifics would depend on how various of your Firefox prefs are configured. Unless, of course, you're willing to make Firefox your default browser, in which case

tell application "Safari"
activate
set theURL to URL of document 1
end tell
open location theURL

should work. However, since this means that clicking links in other apps, which currently open pages in Safari, will now open pages in Firefox instead, I doubt you want to go that route.

As for "a market for it," I suspect that all but the most rabidly mouse-phobic would prefer to do what I do, which is simply to drag and drop the Safari page's favicon on FIrefox's Dock icon.
_________________________

dkmarsh • member, FineTunedMac Co-op Board of Directors

Top
#20270 - 01/24/12 04:37 AM Re: A cross-apps AppleScript? [Re: dkmarsh]
dkmarsh Offline
Moderator

Registered: 08/04/09

Or give this a try:

tell application "Safari"
activate
set theURL to URL of document 1
end tell
do shell script "open -a Firefox.app " & theURL

Edit: note that this script doesn't require you to have made Firefox your default browser.


Edited by dkmarsh (01/24/12 04:41 AM)
_________________________

dkmarsh • member, FineTunedMac Co-op Board of Directors

Top
#20272 - 01/24/12 10:16 AM Re: A cross-apps AppleScript? [Re: dkmarsh]
dkmarsh Offline
Moderator

Registered: 08/04/09

BTW, the second script may not work as expected if Firefox isn't currently running. In my case, anyhow, that situation results in Firefox launching but only displaying my home page.

If I modify the script thusly

tell application "Safari"
activate
set theURL to URL of document 1
end tell
tell application "Firefox" to activate
do shell script "open -a Firefox.app " & theURL

Firefox launches, opening my home page, then opens a second window to the desired URL.
_________________________

dkmarsh • member, FineTunedMac Co-op Board of Directors

Top
#20276 - 01/24/12 02:15 PM Re: A cross-apps AppleScript? [Re: dkmarsh]
artie505 Online


Registered: 08/04/09
Many, many thanks! smile

I'm now running four of your AppleScripts, and they make my life considerably easier.

The second script is working just as I hoped, regardless of whether or not Firefox is running when it is invoked, first with new pages set to open to Vanguard.com and now with them set to open to empty. (Edit: When Vanguard was my home page the script over-rode it and opened only the page I wanted open.)

> As for "a market for it," I suspect that all but the most rabidly mouse-phobic would prefer to do what I do, which is simply to drag and drop the Safari page's favicon on FIrefox's Dock icon.

"Mouse-phobia" may not be as much a factor today as trackpad-phobia, what with the shift from towers to portables; I find my trackpad more difficult to use than I found my mouse when I had to use one.


Edited by artie505 (01/24/12 02:17 PM)
_________________________
The new Great Equalizer is the SEND button.

In Memory Of Harv: Those who can make you believe absurdities can make you commit atrocities. ~Voltaire

Top
#20277 - 01/24/12 03:19 PM Re: A cross-apps AppleScript? [Re: artie505]
dkmarsh Offline
Moderator

Registered: 08/04/09

An additional modification is necessary. Because URLs may contain ampersands and other special characters which have particular meanings to the shell, we need to "escape" such characters by telling the shell to treat a URL as a string literal; to accomplish this, we wrap the URL in quotes when passing it to the shell, as follows:

tell application "Safari"
activate
set theURL to URL of document 1
end tell
do shell script "open -a Firefox.app '" & theURL & "'"

[Single quotation marks in red and double quotation marks in blue for clarity, so you can know what you're copying & pasting.]
_________________________

dkmarsh • member, FineTunedMac Co-op Board of Directors

Top
#20281 - 01/24/12 04:44 PM Re: A cross-apps AppleScript? [Re: dkmarsh]
ganbustein Offline


Registered: 08/04/09
That doesn't work if the URL contains single quotes. It's generally unwise to try to "roll your own" when it comes to quoting. (Most SQL injection attacks exploit the ineptness of roll-your-owners. Proper quoting is not as easy as it looks.)

Code:
tell application "Safari"
    activate
    set theURL to URL of document 1
end tell
do shell script "open -a Firefox.app " & quoted form of theURL



Top
#20287 - 01/25/12 09:11 AM Re: A cross-apps AppleScript? [Re: artie505]
Kevin M. Dean Offline


Registered: 08/04/09
Loc: Florida
You don't even need applescript.

In Safari's Preferences, go to the Advanced tab and check "Show Develop menu in menubar"

Then when you're on a web page, go to Develop -> Open Page With -> Firefox

...and any other browser you may have installed.
_________________________
iMac 2.7 GHz Core i5, 12 GB RAM, OS X 10.9, Int SATA 1 TB, Ext Fire 2 TB / 1 TB / 1 TB / 500 GB / 300 GB
Former MacFixIt Forums member since 11/17/99
www.rhubarbproductions.com

Top
#20288 - 01/25/12 09:22 AM Re: A cross-apps AppleScript? [Re: Kevin M. Dean]
alternaut Offline

Moderator

Registered: 08/04/09
Alternatives are good. Thanks for reminding us! laugh tongue
_________________________
alternaut moderator

Top
#20289 - 01/25/12 10:15 AM Re: A cross-apps AppleScript? [Re: ganbustein]
dkmarsh Offline
Moderator

Registered: 08/04/09

Quote:
Proper quoting is not as easy as it looks.

It doesn't look particularly easy to me, but I take your point. Thanks for the debugging.
_________________________

dkmarsh • member, FineTunedMac Co-op Board of Directors

Top
#20303 - 01/25/12 04:24 PM Re: A cross-apps AppleScript? [Re: artie505]
dianne Offline

Moderator

Registered: 08/04/09
A branch of posts has been detached from this thread and now appears in Phantom Opera.
_________________________
dianne • moderator

Back up everything you can't afford to lose – documents, mail, movies, music, photos, and other data and settings.

Top
#20312 - 01/25/12 10:34 PM Re: A cross-apps AppleScript? [Re: dkmarsh]
artie505 Online


Registered: 08/04/09
Thanks to both you and ganbustein for your contributions; aside from their usefulness, they've taught me a teensy, tiny bit about AppleScript and enabled me to write my very first one, i.e.

Code:
do shell script "rm -rf /Users/artie/Library/Caches/Firefox "

tongue
_________________________
The new Great Equalizer is the SEND button.

In Memory Of Harv: Those who can make you believe absurdities can make you commit atrocities. ~Voltaire

Top
#20313 - 01/25/12 10:42 PM Re: A cross-apps AppleScript? [Re: Kevin M. Dean]
artie505 Online


Registered: 08/04/09
Originally Posted By: Kevin M. Dean
You don't even need applescript.

In Safari's Preferences, go to the Advanced tab and check "Show Develop menu in menubar"

Then when you're on a web page, go to Develop -> Open Page With -> Firefox

...and any other browser you may have installed.

Many thanks for that, Kevin.

I've got no idea why, but your method surpasses the AppleScript method in that it takes me to a more pertinent page.

By way of example, if I use the AppleScript I created (dk's second suggestion) on this page I'm taken to an FTM log-in page, whereas if I use the "Develop" method I'm taken to this page, although (obviously) not as a logged-in user. Similarly, the different methods take me to different "start" pages on the Staples Web site.
_________________________
The new Great Equalizer is the SEND button.

In Memory Of Harv: Those who can make you believe absurdities can make you commit atrocities. ~Voltaire

Top
#20319 - 01/26/12 02:52 AM Re: A cross-apps AppleScript? [Re: artie505]
dkmarsh Offline
Moderator

Registered: 08/04/09

Did you try ganbustein's version (or my last version, which ganbustein's "universalizes")? My realization that quoting the URL is necessary was precipitated by exactly the difference you describe.
_________________________

dkmarsh • member, FineTunedMac Co-op Board of Directors

Top
#20322 - 01/26/12 03:39 AM Re: A cross-apps AppleScript? [Re: dkmarsh]
artie505 Online


Registered: 08/04/09
Originally Posted By: dkmarsh

Did you try ganbustein's version (or my last version, which ganbustein's "universalizes")? My realization that quoting the URL is necessary was precipitated by exactly the difference you describe.

I just tried your final script (which I should have thought of earlier blush ), and it worked with FTM, although it still took me to a different home page at staples.com, but, on the other hand, so does "Develop."

So it's back to AppleScript (ganbustein's version, which also takes me to a different home page at staples.com), which is easier to access (Butler hot key) than is the "Develop" option.

Thanks for the kick.
_________________________
The new Great Equalizer is the SEND button.

In Memory Of Harv: Those who can make you believe absurdities can make you commit atrocities. ~Voltaire

Top
#20350 - 01/26/12 02:20 PM Re: A cross-apps AppleScript? [Re: artie505]
Virtual1 Offline


Registered: 08/04/09
Loc: Iowa
Safari is surprisingly scriptable in some regards, and less so in others.

I wrote an app to interact with a web page and do print-to-pdfs for me in an automated way, but I unpleasantly discovered that once you get into the print dialog, you are on your own, telling safari to "press button 4 of tab 3 of control 5" sorts of commands, and those can be a PAIN to figure out. Also, the control indexes can change from build to build of safari, meaning if a machine accidentally gets a safari update, the applescripting for it can blow up and force you to put back on the gloves to figure out the new indexes.
_________________________
I work for the Department of Redundancy Department

Top

Moderator:  alternaut, dianne, MacManiac