OSX 'open with' bash script
I've got a bash script, potentially many of them in fact, which I'd like to be able to open files in OSX's finder with.
It's a really, really simple concept but for some reason bash scripts are greyed out in the finder 'open with' dialog.
I gather that there are various ways of using applescript or packaging as an app... but I haven't been able to figure any of this out and I don't really want to have to pick up another language just for this trivial task - could someone spoon feed me how to do this?
Thanks!
It's not possible. Launch Services works with application identifiers, and bash scripts don't have them.
You need to create a wrapper application using Automator.
- Launch Automator
- Select Application
- Look for the Run Shell Script action and add it to the right.
- Pass input as arguments
- Put your script in there
- Save somewhere
Here's my version, using the Growl command line utility:
Result:
You can see the effect this change has on ~/Library/Preferences/com.apple.LaunchServices.plist
when you Change All:
("Test" is the name I gave my Automator application)
I followed Daniel Beck's instructions with a few modifications and got this to work for URL files which I wanted to open in Chrome (Firefox would also work) on a Mac.
The script I used is:
sed 's/^URL=/URL=/' "$1" | grep -m 1 '^URL=' | sed 's/^URL=//' | tr -d '\r' | xargs open -a "Google Chrome"
This parses out the URL= line from a typical URL file which looks like:
[InternetShortcut]
URL=http://www.docircuits.com/pricing
IDList=
HotKey=0
IconFile=O:\Apps\Firefox\Data\profile\shortcutCache\4t0JW4mY1qRPhiYz1fY3dw==.ico
IconIndex=0
Save the Automator script someplace like your ~/Library directory. I called mine OpenUrl.app.
Now go to a url file somewhere and open the "Get Info" popup. Change the Open With command to your OpenUrl.app script. Test with just one or click the "Change All..." The first time I clicked that it gave an error, but seemed to work the second time.
In developing this I did get the "Run Shell Script" errors, but that was because of actual errors in the script. I think the key issue with Daniel's script is that it doesn't handle spaces in the file name you try to open.