Create DMG file

MobX picture MobX · Aug 6, 2009 · Viewed 17k times · Source

I am developing an application in Cocoa. I need to create a DMG file to install my application like Adium (which provides a nice UI to drag the app file to Application folder). Is there a tool for this?

Answer

Quinn Taylor picture Quinn Taylor · Aug 6, 2009

If you have a single file that the user would probably copy to the Applications directory, an "internet-enabled" disk image is a nice alternative. When you download one using Safari (I know, not everyone uses Safari), it automatically mounts it, copies the contents to the download location, then unmounts the DMG and moves it to the trash. The application doesn't automatically go to Applications, but it's on the user's HD and they don't have to worry about fiddling with the disk image.

Disk images can be internet-enabled with a single Terminal command:

hdiutil internet-enable -yes "MyDiskImage.dmg"

This should work on any DMG file. If you want users to see your pretty background, you might not want to do this. However, for simple payloads it's a nice way to go. (Of course, some people would prefer to create a ZIP file for this sort of deployment, anyway.)


I find that manually creating a DMG can be a pain. You can actually automate creation of a simple DMG in your Xcode project. Just create a Shell Script Target (Project → New Target...) and use hdiutil in the script. The command would look something like this (substituting the proper names and directories):

hdiutil create -fs HFS+ -volname "MyApp 1.0" -srcfolder \
"/directory/with/contents/to/package/" "~/Desktop/MyApp-1.0.dmg"`

You'll need to put all the stuff you want to include in the disk image in a single directory, but if you're not afraid of using cp in Terminal, this is easy to do.

If you would like to see an example, I use this approach in CHDataStructures.framework myself. If you check out the code and open in Xcode, the second script in the Deployment target creates the DMG.