OSX 'open with' bash script

8

6

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!

rich

Posted 2011-01-29T10:31:23.653

Reputation: 1 071

Answers

11

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.

  1. Launch Automator
  2. Select Application
  3. Look for the Run Shell Script action and add it to the right.
  4. Pass input as arguments
  5. Put your script in there
  6. Save somewhere

Here's my version, using the Growl command line utility:

enter image description here

Result:

enter image description here


You can see the effect this change has on ~/Library/Preferences/com.apple.LaunchServices.plist when you Change All:

enter image description here

("Test" is the name I gave my Automator application)

Daniel Beck

Posted 2011-01-29T10:31:23.653

Reputation: 98 421

Thanks. I gave it a whirl but I try and open with the app created by Automator it just says The action "Run Shell Script" encountered an error. Check the action's properties and try running the workflow again. Which is about the least helpful error message I've seen for some time! – rich – 2011-01-29T21:45:08.347

@Rich Does it work with the default script in the run shell script action? Or not even then? Did you remember to change the pass input preference? Do you try double-clicking your application, dragging files onto your application, or only opening the associated file? What happens if you start it the other ways? – Daniel Beck – 2011-01-29T21:49:01.377

Thanks. Not sure how you'd see output from the default one as it just calls cat, but it seems that didn't error. Rather than have the app call my script from a non-standard location I created a script in /usr/local/bin and then discovered that if I tried to run it it complained that there were too many levels of symbolic links (1?!) So I've just copied the script there instead and it now seems to work. Thanks! – rich – 2011-01-29T22:01:19.110

1

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.

Keith Olson

Posted 2011-01-29T10:31:23.653

Reputation: 11

-1

Use reattach-to-user-namespace, either from the repo or with brew install reattach-to-user-namespace. This works in Sierra, I'm not sure about other OSes.

Once installed, use it in the bash script like so:

reattach-to-user-namespace open a_cool_file.png

wickedchicken

Posted 2011-01-29T10:31:23.653

Reputation: 99

This doesn't seem to answer the question. The OP wants to use the Finder's "Open With" function to open a file with a Bash script. Providing clipboard support under Tmux and Screen doesn't help with that, especially since there's no indication that Tmux or Screen are even involved. – 8bittree – 2017-03-16T19:12:35.230