Command-Line Tutorial (Part I): Native Mac Apps for Command-Line Tools
Many times science users have tools designed for classic UNIX or Linux and intended to be run on the command-line. On Mac OS X, however, it's sometimes nicer to have a real, native Mac app that can be launched in the Finder, accepts files dragged to it, and everything which goes along with the Mac experience.
Launching and Dropping
In Tiger, Apple made it possible to double-click on UNIX binaries to launch them. This was a nice step, but often doesn't solve our problems. This is the equivalent of running a command from Terminal.app with a full path name. The program starts in some arbitrary directory and you cannot easily set environment variables or other options before running. For example, many UNIX command-line tools set options via flags.
So this tutorial series will look at a variety of options to make native applications for command-line programs, shell scripts, Perl, Python, Ruby, and everything else you might run from the terminal.
This week's installment looks at simple drag-and-drop launchers.
Sometimes you just want to launch a program, make sure you're in the right directory, set a few environment tools, and maybe accept drag-and-drop files.
Long ago, there was a tool called DropScript which would let you turn an arbitrary set of BSD commands into a Mac application (http://www.wsanchez.net/papers/DropScript/). It's still available, but hasn't been updated.
The replacement, in my mind, is Platypus (http://www.sveinbjorn.org/platypus) which solves a large range of problems in launching scripts of all types, including simple shell scripts.
Come Out of Your Shell
For today's exercise, we'll stick with creating a simple drop application with a shell script. Feel free to explore, Platypus has a huge range of options.
By default the "Shell" script type is chosen, but we don't have a shell script in mind. So go ahead and click the "New" button under the "Script Path" field. A simple editor will pop up as a sheet:
Notice that this creates a temporary file (/tmp/PlatypusScript.1804) -- you can reveal this in the Finder if you want to save it. (Not a bad idea.)
Here I'm creating a launcher which needs a few environment variables set. Then it runs the command "shift" -- by default Platypus will pass the path to the application as the first shell parameter $1. Any files dropped on the application would take later spots, e.g. $2, $3, ... In my case, I want to pass those files on to the command-line program, so I run "shift" which moves $3 to $2, $2 to $1, etc.
Then I run my command. That's it to my script!
Click "save" in the script editor sheet.
Now I don't actually have to set environment variables in the shell script. I could use a separate option. Under the "advanced" options section of the Platypus window, there's a small button to "Edit Environment" which lets you set environment variables. I'm trying to illustrate that any sort of script command can be put into a Platypus script. For example, you can change directories to be sure you run the command from a certain directory, redirect output, literally anything in a regular UNIX script.
One last thing for this tutorial. We want to allow users to drop any sort of file on the application. Make sure to check the box for "Is droppable" under the advanced options. For me, anything is fine, the command-line tool will ignore files it doesn't understand. If you want to restrict the file types, click on the "Edit Types" button which appears after the box is checked.
Once you're done setting options, you can create your application. Click the "Create" button. You'll get a sheet to save your new application. Feel free to explore, Platypus has many options for making a fully native Mac application. Next time, we'll look at cases where you might want to set options before running the application.