Home
Posted By: artie505 A cross-apps AppleScript? - 01/24/12 11:53 AM
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.
Posted By: dkmarsh Re: A cross-apps AppleScript? - 01/24/12 12:23 PM

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.
Posted By: dkmarsh Re: A cross-apps AppleScript? - 01/24/12 12:37 PM

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.
Posted By: dkmarsh Re: A cross-apps AppleScript? - 01/24/12 06:16 PM

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.
Posted By: artie505 Re: A cross-apps AppleScript? - 01/24/12 10:15 PM
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.
Posted By: dkmarsh Re: A cross-apps AppleScript? - 01/24/12 11:19 PM

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.]
Posted By: ganbustein Re: A cross-apps AppleScript? - 01/25/12 12:44 AM
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


Posted By: Kevin M. Dean Re: A cross-apps AppleScript? - 01/25/12 05:11 PM
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.
Posted By: alternaut Re: A cross-apps AppleScript? - 01/25/12 05:22 PM
Alternatives are good. Thanks for reminding us! laugh tongue
Posted By: dkmarsh Re: A cross-apps AppleScript? - 01/25/12 06:15 PM

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.
Posted By: dianne Re: A cross-apps AppleScript? - 01/26/12 12:24 AM
A branch of posts has been detached from this thread and now appears in Phantom Opera.
Posted By: artie505 Re: A cross-apps AppleScript? - 01/26/12 06:34 AM
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
Posted By: artie505 Re: A cross-apps AppleScript? - 01/26/12 06:42 AM
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.
Posted By: dkmarsh Re: A cross-apps AppleScript? - 01/26/12 10:52 AM

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.
Posted By: artie505 Re: A cross-apps AppleScript? - 01/26/12 11:39 AM
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.
Posted By: Virtual1 Re: A cross-apps AppleScript? - 01/26/12 10:20 PM
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.
© FineTunedMac