Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open link in Firefox via contextual menu using Applescript

I'm new to use Applescript to create services in Snow Leopard. I found myself often trying to open a link in Safari with Firefox. I know there are ways to open a page url with FF but I want to open any link inside a page with FF. I think using Applescript to create a service might be a good idea and so far I found this:

openFirefoxURL("http://www.apple.com/")

on openFirefoxURL(x)
   return do shell script "open -a Firefox" & space & quoted form of x
end openFirefoxURL

This creates a new tab in FF nicely. Any advice to adapt it to open a link on a page?

UPDATE: I found a solution through trial and error:

tell application "Safari"
    set myURL to (do JavaScript "(getSelection().anchorNode.parentNode.href)" in document 1)
end tell

do shell script "open -a Firefox" & space & myURL

Now that the script serves my purpose, I don't know if there are better ways to do it, e.g. getting the selection via Applescript rather than javascript. Let me know if you have better solutions. Thanks!

like image 891
lai Avatar asked May 04 '26 11:05

lai


1 Answers

There is another approach I just wrote after searching the web for a while an not finding a suitable solution.

It is a service in AppleScript that will open any URL in Firefox from any other application URL field. Just select the text in the URL of Chrome, for instance, and choose the service from the list.

To create the service:

  1. Choose service in the main Automator dialog.
  2. Choose "service receives selected" [URLs]
  3. Choose "in" [any application]
  4. Choose "input is" [only URLs]

  5. Then add the action "Run Shell Script" dragging it to the script sequence.

  6. Choose "Shell" [/bin/bash]
  7. Choose "Pass input" [as arguments]
  8. And paste the following script:
for f in "$@"
do
    echo "$f"
    if [ ${f:0:4} = "http" ]; then
        open -a Firefox "$f"
    else
        prefix="http://"
        prefix+=$f
        open -a Firefox "$prefix"
    fi
done

The script checks if the "http" prefix is added, as Chrome does not pass it by default, and opens Firefox with the OSX 'open' command.

Save this Service with the name "Open URL in Firefox" and you are done. The service will be available at the services list menu after a right click over any URL field selection.

Done.

The service is saved at ~/Library/Services. This directory is hidden by default in Lion, to see it just issue this command at terminal:

# chflags nohidden ~/Library/
like image 114
myktux Avatar answered May 07 '26 06:05

myktux



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!