Relevant Music

Mac OS: How To Create Shell Script Launchers for Launchpad or Dock

Apps in the Launchpad created from shell scriptsMac OS X’s core component Darwin is a UNIX-compatible operating system, which may attract former Linux users to join the Mac fan community. Projects like MacPorts successfully try to thwart the lack of a built-in package manager, important for Linux residents to install new open-source software. Given these preconditions, many shell scripts you have created in the rest of the Unix world, run on Mac OS X.

Based on a built-in tool in Mac OS X

While both KDE and Gnome enable you to create launcher icons for your scripts, this is not an easy thing to do on the Mac OS X desktop. Since Mac OS X 10.4, a tool called Automator is shipped which allows you to automate some tasks on Mac OS X without programming skills. The interesting thing about Automator is that you can export your task flows to a Mac OS X application.

To launch a shell script from Automator, you may simply create a task starting Terminal with a shell script. Then export the shell script to a Mac OS X application, which will appear in your Launchpad.

Automating Automator

However, when using a lot of shell scripts like I do, it would be an annoying thing to always launch Automator, create a new workflow and export it to an app. So I have written another shell script, automatically creating such an Automator app which opens an existing shell script in the Terminal. The script basically creates an application directory, copies the application stubs from the Automator directory on your hard drive to the new directory, and includes a custom XML file telling Automator what to do.

Prerequisites

  • The script is made for people which have skills in shell scripting. Before launching the script, open it in a text editor and look for yourself what it is doing. Do not complain if you have used it in a wrong way.
  • The script was only tested on Mac OS X 10.7 (Lion)
  • Especially, check if the variable AutomatorApplStubRoot (set in line 8 ) is pointing to the correct automator application stub directory. In Lion, the path should be fine. Please post a comment if you have discovered a different directory, especially in other versions of Mac OS X.

Usage of the script

  1. Download the script and unzip it to a location of your choice.
  2. Ensure the shell script you want to launch (not the one you downloaded in 1.) ends with “.command”, not with “.sh” etc. Make sure it is executable, otherwise make it executable with chmod 755 <shell script name>
  3. Launch the script you downloaded in 1. with sh create-launcher-app.sh “[app-name]” [shellcommand] where [app-name] denotes your desired name for the application created, and [shellcommand] is the executed shell script from 2. To make the script work correctly, always use the absolute path, i.e. the path starting with the root directory ‘/
  4. The script will create an app placed in /Applications/Scripts/ You should also find the app in the Launchpad and may drag it to the dock.

Example

  1. You have a script /Users/me/scripts/unlock-exthd.sh that you want to launch from the dock in the terminal, since it creates a lot of interactive output. Download the creator script, unzip it and put it to /Users/me/scripts/create-launcher-app.sh
  2. Move to your scripts directory by doing ‘cd /Users/me/scripts’. Do ‘mv unlock-exthd.sh unlock-exthd.command’ and ‘chmod 755 unlock-exthd.command’
  3. You want to name the resulting application “Unlock external HD” Now launch the creator script with ‘sh create-launcher-app.sh “Unlock external HD”  /Users/me/scripts/unlock-exthd.command’
  4. The application will appear in ‘/Applications/Scripts/Unlock external HD.app’ and will appear in your Launchpad. Now the application can easily be dragged into the Dock.

By default, the Automator icon is used as the application icon. If you want to use another one, replace the corresponding file: /Applications/Scripts/[AppName].app/Contents/Resources/AutomatorApplet.icns

If you have found an easier solution, please post a comment.

Relevant Music © 2003-2006 Leo Nobach. Blog proudly powered by WordPress