BLOG

Building Fancy DMG Images on Mac OS X

Remko Tronçon ·

On Mac OS X, applications are usually distributed using disk images (DMGs). These images are mounted as a separate volume, and it often suffices to drag the application out of the image into your Applications folder. A nice extra feature of these disk images is that their look can be customized, just as every other folder under Mac OS X.

This post explains how such DMG files with a customized look can be built. Moreover, a Makefile is provided such that, once a template look is created for a DMG, the process of building a DMG file from a set of files can be fully automated.

Creating the template

First, we’ll start by specifying how our disk image should look when it is mounted (and opened). If you don’t need a fancy look, you can skip this section; the Makefile provided at the end of the page will automatically create a default template if you don’t create one yourself.

  1. Using Disk Utility (which comes with Mac OS X), create a new disk image, and call it template.dmg. Select a size which is more than enough to store the contents you plan to put in the final DMG. The Encryption and Format settings (None and r/w respectively) should be left untouched. Then, create your image by clicking Create.
  2. Open the newly created image.
  3. From the View menu of Finder, choose Show View Options You can now customize the way your folder will look. Be careful, don’t forget to select This window only, such that your changes will only apply to the current folder. Using the View menu, you can change the looks of the window even further. Also note that, if you choose a background picture, the picture file must reside in the image itself. It is common to create a (hidden) directory .background in the root dir of the image, dropping the background picture there, and then selecting it as a background. Use Cmd-Shift-G in combination with the full path (e.g. /Volumes/MyDisk/.background) to open the hidden directory in Finder and copy your picture there (or, alternatively, use Terminal). The Select button in the View menu seems to open the .background dir by default if it exists. If not, also use Cmd-Shift-G.
  4. Drag all the files you want in your image into the volume, and place them exactly where you want to. Assign icons to the files by control- clicking on the icons, and using the Show Info dialog. Note that the actual contents of the files in the template does not matter if you are planning to use the automatic build way; they will be overwritten anyway. Hence, you will get the most compact template if all your files are empty.
  5. Customize the icon of the disk image in the same way as the other icons, only this time by control-clicking on the disk image icon on the desktop.
  6. Eject the disk image.
  7. Since the template.dmg file will be quite big, you probably want to compress it using bzip2. This is not strictly necessary, but if you created an initial disk size of 40M, using bzip2 you can reduce this to only 1M.

If all went well, you should now have a template.dmg(.bz2) which, when you open it, has a custom icon on the desktop, and looks exactly how you would like the final disk image to be. Now all we need to do is fill it up with the right files.

Manually building the final DMG

The easiest (but least efficient) way to create a final DMG is by doing it by hand. If you want to do this manually, I assume that you dragged the exact files you want the image to contain in the previous section. All you have to do then is:

  1. Open Disk Utility
  2. Select the image you created in the previous step
  3. Select Convert… from the Images menu
  4. Enter the name you want your final image to get
  5. Select Compressed as your Image Format
  6. Click Save

That’s it, your final DMG should be ready for distribution.

Automatically building the final DMG

Using the template we just created, we can now automatically build a DMG with the actual files. All you need to do is download this Makefile, modify the values on the top of the file, and run make. Alternatively, you can include the Makefile from within another Makefile after setting the proper values, as is illustrated in the example of the fancy-dmg package (also available from the Git repository)

The values that can be modified are:

  • NAME: The name of your application. This name will appear as the volume name of your disk image.
  • VERSION: The version number of your package. This is used in the filename of the final disk image.
  • SOURCE_DIR: The directory where the files you want in your disk image reside.
  • SOURCE_FILES: The names of the files (without the directory prefix) you want in your source image.
  • TEMPLATE_DMG: The name of the template DMG file (without the .bz2 extension if it was compressed)

Note that you don’t really need a template.dmg. If the Makefile can’t find one, it will create its own, default disk image.