Friday, July 25, 2014

How to associate a file name extension with a darwin /linux/unix command-line executable on osx/macintosh.

One of the things one can do with self-serving (a multi-platform command-line executable for making simple tools that employ the user's browser as the UI and javascript for the logic), is to bundle a bunch of resources into a file with the ".slfsrv" extension; for example "morningMenu.slfsrv".  On my macintosh I could execute this from the command line "self-serving morningMenu.slfsrv" or on the Windows command line with "self-serving.exe morningMenu.slfsrv".

I wanted to associate the ".slfsrv" extension with the executable, so that one could run a .slfsrv program simply by double-clicking on the file in OSX's Finder or Windows' explorer. This was very easy in Windows but on the Mac it took me quite a while to figure out. It turns out you can't associate a file extension with a simple executable, it has to be a  Mac App.

How to mac a Mac App from a darwin executable

To make a Mac App from a darwin executable

  1. Launch the Automator application
  2. Click "New Document"
  3. For document type select Application
  4. From the Actions library on the left select select "Run Shell Script" and drag that to the workplace on the right
  5. Change the "Pass input" option to "as arguments"
  6. Edit the shell script for what to be something like one of these:

    /path/to/my/exec/myexecutable "$@"

    or

    for f in "$@"
    do
        /path/to/my/exec/myexecutable "$f"
    done
  7. Select File/Save and create a name and location for your ".app" file. In my case I called this "SelfServing.app" and put it in my "~/bin" folder

You next want to associate a file extension with the .app you just created, as follows:

  1. In the finder open a folder containing a file of that type. In my case I opened the folder containing the file morninMenu.slfsrv
  2. Right click on the file and select "Get Info"
  3. Under "Open With" select "Other..." and locate the .app file you created from Automator (you probably want to have all files with that extension opened with your app).
Using it

That's it. Now you can run the command-line program on all files with that executable simply by double clicking that type of file in Finder, or from the command-line typing "open myfile.myextension"