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!
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:
Choose "input is" [only URLs]
Then add the action "Run Shell Script" dragging it to the script sequence.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With