Installation Q & A
Q
I want to add the application I'm installing to the Dock of the current user. How can I do that?
A
This can be achieved through a shell script that will be run after the application has been installed. The shell script will run a command line tool that will create the alias.

This article will describe how to do this with Packages.


Adding an application to the Dock from the command line

There are no tools provided with the default Mac OS X installation that will let you add an application to the Dock from the command line. So we have to provide our own tool to do that. It just happens that such a tool is available, its name is emmett.

emmett lets you add an application, a file or a folder to the Dock of the current user.

You can get the source code for emmett here (112 KB). It also includes a Universal binary of the executable.

Let's say you want to add the Dictionary application to your Dock, here is how you can do this from a Terminal session:

$ /absolute/path/for/emmett add /Applications/Dictionary.app

This will add the Dictionary app to the Dock, if it's not already there, and relaunch the Dock to reflect the change.

Calling emmett after the application has been installed

It is possible to run a shell script after a bundle application within the payload has been installed. Such a script is called a post-installation script.

In order to run emmett from a post-installation script, you need to know 2 file paths:

  • the path that should be used to call emmett.
  • the path where the application will be installed.

Since emmett is not part of a standard installation, you will need to also include it in the extra resources of the package. During the installation process, extra resources are extracted at the same level as the post-installation script.

When it comes to the installation path of the application to pass to the emmett tool, you do not want to use a hard coded path. The application may not always be installed where you think it will be:

  • you may have allowed the user to install your package on a volume other than the startup disk. In this case, you have no ways to know the name of the destination volume.
  • you may have attached a locator rule to the application bundle in the payload. In this case, if a previous version of the application is found in another location on the destination volume, the installer will upgrade the previous version with the version in the package.

Fortunately, when the installer runs a post-installation script attached to an application bundle, it provides you with the final installation location for the application as a parameter. For a shell script, this is the $2 parameter.

Knowing all this, we can now write our shell script. Assuming the name of the application to install is MyApplication, here is what the script will look like:

#!/bin/sh

# We only install the application in the Dock for the current user and when the installation is not run from the command line.

if [ "$COMMAND_LINE_INSTALL" = "" ]; then

    /usr/bin/su $USER -c "./emmett add \"$2\""

fi

exit 0


Setting the post-installation script for the application

As we mentionned earlier, we will attach the post-installation script to the application - and not set it to be the post-installation script for the package -. Here is how to do that:

  1. Select the application in the Files hierarchy list.
  2. Click on the Scripts tab in the inspector.
  3. Click on the Set… button for the post-installation script.
  4. Select the script and click Set.

Including the emmett tool in the package

Finally, we will add the emmett tool as an extra resource of the package. Here is how to do that:

  1. Click the Scripts tab.
  2. Click the + button.
  3. Select the emmett tool and click Add…
  4. Click Add.

You can now build your package and test it.


Sample Project
04/06/12: sample code and article updated to add quotation marks around the $2 argument.

Site Map

Copyright 2012 Stéphane Sudre. All rights reserved.