An open community 
of Macintosh users,
for Macintosh users.

FineTunedMac Dashboard widget now available! Download Here

Previous Thread
Next Thread
Print Thread
A cross-apps AppleScript?
#20268 01/24/12 11:53 AM
Joined: Aug 2009
Likes: 15
OP Online

Joined: Aug 2009
Likes: 15
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
Re: A cross-apps AppleScript?
artie505 #20269 01/24/12 12:23 PM
Joined: Aug 2009
Likes: 3
Moderator
Offline
Moderator

Joined: Aug 2009
Likes: 3

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
Re: A cross-apps AppleScript?
dkmarsh #20270 01/24/12 12:37 PM
Joined: Aug 2009
Likes: 3
Moderator
Offline
Moderator

Joined: Aug 2009
Likes: 3

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.

Last edited by dkmarsh; 01/24/12 12:41 PM.


dkmarsh—member, FineTunedMac Co-op Board of Directors
Re: A cross-apps AppleScript?
dkmarsh #20272 01/24/12 06:16 PM
Joined: Aug 2009
Likes: 3
Moderator
Offline
Moderator

Joined: Aug 2009
Likes: 3

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
Re: A cross-apps AppleScript?
dkmarsh #20276 01/24/12 10:15 PM
Joined: Aug 2009
Likes: 15
OP Online

Joined: Aug 2009
Likes: 15
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.

Last edited by artie505; 01/24/12 10: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
Re: A cross-apps AppleScript?
artie505 #20277 01/24/12 11:19 PM
Joined: Aug 2009
Likes: 3
Moderator
Offline
Moderator

Joined: Aug 2009
Likes: 3

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
Re: A cross-apps AppleScript?
dkmarsh #20281 01/25/12 12:44 AM
Joined: Aug 2009
Offline

Joined: Aug 2009
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



Re: A cross-apps AppleScript?
artie505 #20287 01/25/12 05:11 PM
Joined: Aug 2009
Offline

Joined: Aug 2009
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
Re: A cross-apps AppleScript?
Kevin M. Dean #20288 01/25/12 05:22 PM
Joined: Aug 2009
Likes: 1
Moderator
Offline
Moderator

Joined: Aug 2009
Likes: 1
Alternatives are good. Thanks for reminding us! laugh tongue


alternaut moderator
Re: A cross-apps AppleScript?
ganbustein #20289 01/25/12 06:15 PM
Joined: Aug 2009
Likes: 3
Moderator
Offline
Moderator

Joined: Aug 2009
Likes: 3

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
Re: A cross-apps AppleScript?
artie505 #20303 01/26/12 12:24 AM
Joined: Aug 2009
Moderator
Offline
Moderator

Joined: Aug 2009
A branch of posts has been detached from this thread and now appears in Phantom Opera.


Back up everything you can't afford to lose: documents, mail, movies, music, photos, and other data and settings.
Re: A cross-apps AppleScript?
dkmarsh #20312 01/26/12 06:34 AM
Joined: Aug 2009
Likes: 15
OP Online

Joined: Aug 2009
Likes: 15
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
Re: A cross-apps AppleScript?
Kevin M. Dean #20313 01/26/12 06:42 AM
Joined: Aug 2009
Likes: 15
OP Online

Joined: Aug 2009
Likes: 15
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
Re: A cross-apps AppleScript?
artie505 #20319 01/26/12 10:52 AM
Joined: Aug 2009
Likes: 3
Moderator
Offline
Moderator

Joined: Aug 2009
Likes: 3

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
Re: A cross-apps AppleScript?
dkmarsh #20322 01/26/12 11:39 AM
Joined: Aug 2009
Likes: 15
OP Online

Joined: Aug 2009
Likes: 15
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
Re: A cross-apps AppleScript?
artie505 #20350 01/26/12 10:20 PM
Joined: Aug 2009
Offline

Joined: Aug 2009
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

Moderated by  alternaut, dianne, MacManiac 

Link Copied to Clipboard
Powered by UBB.threads™ PHP Forum Software 7.7.4
(Release build 20200307)
Responsive Width:

PHP: 7.4.33 Page Time: 0.035s Queries: 46 (0.022s) Memory: 0.6511 MB (Peak: 0.7716 MB) Data Comp: Zlib Server Time: 2024-03-28 10:27:52 UTC
Valid HTML 5 and Valid CSS