TweetFollow Us on Twitter

May 01 Mac OS X

Volume Number: 17 (2001)
Issue Number: 05
Column Tag: Mac OS X

Just Ship It!

by Andrew Stone

Building Electronic and CD Software distributions for Mac OS X

Now that you have written, debugged, beta tested and finished your online documentation for your whizzy new Mac OS X application, it's time to make it available to the world! Back in the old days of Mac OS 9 and earlier, developers had a selection of package making tools, including the almost de facto StuffIt™. For Mac OS X however, there is no need to rely on third party vendors to create your installation packages: Apple has provided some powerful disk image creation utilities that come with the base release of Mac OS X. This article will teach you how to use those tools by hand, give you an example of a totally automated packaging shell script, and go into the finer points, such as including a background image in your distribution folder. These same tools can create your master for CD replication as well.

The Macintosh Way

A quick review of some of the online developer discussion groups will reveal the huge amount of emotion developers have over the right way to distribute software packages. Open source and free ware folks swear by gnutar, but that is a problematic solution because Apple no longer includes gnutar in the base distribution. And, because of legal issues surrounding the GNU project's ‘Copyleft', Apple did not build in an unpacking mechanism for the industry standard gnutar ball. So, for now, forget simply packing up your distribution with gnutar czf, although you get the highest level of compression and smallest resulting downloads.

Apple has come up with a great packaging solution, although the resulting downloads are somewhat bulkier — disk images are optimized for decompression speed, not minimum compression size. The emphasis has become on how easy a distribution is to unpack and how to reduce the number of steps a user has to take to get the software installed. If you have visited the iTools site, and peeked into the Mac OS X software download folder, you'll see a bunch of APPNAME.dmg files — "disk image" files. The same technology that Apple pioneered for distributing firmware upgrades, the self-mounting disk image, .smi, has evolved to be a great way for developers to distribute their wares.

The user simply double-clicks the downloaded .dmg file, and the Copy Disk utility launches, and automatically mounts the download as a the disk image. During the mounting, Copy Disk verifies the integrity of the image by matching the checksum with the encoded checksum. The user then sees your distribution folder with your application in it. They can try out your app from the disk image if they want, or they can just copy it to their applications folder. And that is the entire installation process.

Because the OS X bundle architecture encourages developers to place all the related files of an application inside the application wrapper, your application can be anywhere on the user's computer and still function correctly. This is an ideal situation because we should never force users to do anything a certain way, especially not for some arcane technical reason. If your installation requires frameworks that must be installed in a certain location, you should consider either including them inside the appwrapper, or perhaps using scripts that install the software for the user in the correct location. Rule 1 is to make your installation so idiot proof that even adults can use it.

The Tools

You use the command line tools, hdiutil and hdid, to create, mount, eject and compress your disk image. A complete guide to the usage of these programs is included in the online manual pages. Type "man hdiutil" to see all the options. Let's walk over this process. I'll use APPNAME as a standin for whatever your application is called in the following set of instructions.

1. Launch Terminal.app from /Applications/Utilities
2. Become "root", aka super user

   sudo -s

then provide an adminstrator password, probably your own login password if you are set up to administer your machine.

You need to be root because you will be creating and mounting disk partitions which requires root access. A user cannot sudo to root if they are not a member of the admin group. To allow a user to become an administrator, choose the "Users" module from System Preference's "Show All" pane. Select the user, and click "Edit User". Make sure that the "Allow user to administer this machine" switch is checked, and click OK.

By default, Mac OS X ships without a root account for security reasons. The traditional way of becoming substitute user, su, will fail. To add a root user, launch NetInfoManager from /Applications/Utilities. Choose Domain->Security->Authenticate..., and provide the admin password for your computer. Choose Domain->Security->Enable Root User. You will be asked to provide a non-trivial password, don't forget it!

3. Change directory to where you want to build your disk image — preferabbly a disk with a lot of free space!

   cd /Volumes/BIG/distribution

4. Create the scratch, uncompressed disk image to which you will copy your application. You should be liberal with the size you allocate with the -megabytes flag. Having too much extra free space will not affect the final compressed size, but having too little will annoy you when you cannot quite copy all of your application onto the image. Doubling the size that your application requires seems to always work. The -layout flag can be NONE for net-distributed application, but you must use the SPUD (Single Partition UDIF) option for a CD-R master. If you are planning on using the same image for Net distribution as CD distribution, then use SPUD here. The -zeroImage tells hdiutil to write zeros to all sectors in the image file, which seems like a good idea to me.

   hdiutil create ‘APPNAME-scratch.dmg' -megabytes 10 -layout NONE -zeroImage

5. Alert the operating system of the existence of this new disk using hdid — but don't mount the disk in Finder.

   hdid APPNAME-scratch.dmg -nomount

After entering this command, the system will reply with the allocated device now connected to the disk image:

   /dev/disk1

You'll now use this information in the next step when you initialize the disk image. It's always of the fom /dev/disk, starting with 1.

6. Create an HFS file system with the name you want Finder to display, using the standard 4K blocksize, -b 4096. If you used -layout NONE above:

   newfs_hfs -v APPNAME -w -b 4096 /dev/disk1

If you used -layout SPUD above, you need to initialize the second partition which contains the actual Apple_HFS disk, since the first one has the partition map on it:

   newfs_hfs -v APPNAME -w -b 4096 /dev/disk1s2

7. Eject the image, again replacing disk1 with whatever was returned in step 5. above.

   hdiutil eject disk1

8. Now, do a complete mount of the disk so that it appears in Finder:

   hdid APPNAME-scratch.dmg
     ->/dev/disk1

At this point, the disk image appears as the icon of a removable drive entitled the name you gave to the -v option in step 6. Now, copy your application and readme files to the new disk. Set the permissions as needed — later in this article, you'll learn how to change the folder's background image and set custom folder icons.

9. Eject the disk

hdiutil eject disk1

NOTE: If disk won't eject because "its busy", you may have to logout and login or even reboot.

10. Make the compressed, net distributable, disk image:

hdiutil convert ‘APPNAME-scratch.dmg' -format UDCO -o ‘APPNAME-2001-04-06'

The -format option is UDCO (UDIF compressed image) for net distributions, and UDTO (DVD/CD-R master image) for your CD master image. The correct file extension will be added by hdiutil. I think it is wise to include a date in the name of your distribution, so that users can quickly and correctly tell whether they need to download a newer version or not.

Automating The Process

Ken Case, cofounder of OmniGroup, www.OmniGroup.com, created PackageApplication. This handy script not only creates the diskimage, but preprocesses your application to remove extraneous files and symbols, makes a copy of the release, installs frameworks inside of the application, copies the app to the image, compresses the image, and creates an equivalent tarball for a more traditional (and somewhat smaller) UNIX distribution. This is a zsh script, so save it to PackageApplication and make the appropriate changes to PUBLIC path, and then make the file executable:

chmod 755 PackageApplication

Now, you can package an application with, e.g.:

sudo -u root PackageApplication APPNAME

Sudo allows you to execute commands as another user — you are prompted for a password — provide your own. See man sudo for more information on this handy utility.

Here's the script:

#!/bin/zsh -f
#
# Copyright 2000-2001 Omni Development, Inc. All rights reserved.
#
# $Header: /Network/Source/CVS/OmniGroup/Scripts/PackageApplication,v 1.27 2001/03/14 20:19:28 kc Exp $

PUBLIC=/Users/Shared

APPNAME=$1
if [ "$APPNAME" = "" ]; then
   echo "Usage: $0 app-name"
   exit 1
fi
if [ "$SUDO_USER" = "" ]; then
   SUDO_USER = "$USER"
fi
INSTALLEDPRODUCTS=$PUBLIC/$SUDO_USER/InstalledProducts
PACKAGEDIR=$PUBLIC/$SUDO_USER/Release/$APPNAME

cd /tmp

echo "Copying files from $INSTALLEDPRODUCTS to $PACKAGEDIR
"
echo "   (Note: this assumes that all of $INSTALLEDPRODUCTS
   is needed for $APPNAME. If not, remove and rebuild.)
"
(cd $INSTALLEDPRODUCTS; chmod -R u+w .; tar cf — .) | (rm -rf $PACKAGEDIR; mkdir -p $PACKAGEDIR ; cd 
$PACKAGEDIR; tar xpf — |& egrep -v ‘tar: Unable to set file uid/gid of .* (No such file or directory)')

cd $PACKAGEDIR
echo "Cleaning up release"
chmod -R u+w,go-w,a+rX .
chown -R root.admin . 2>/dev/null
# Get rid of those CVS leftovers...
find . -name CVS -type d -exec rm -rf {} \; -prune
find . -name ‘.#*' -type f -exec rm {} \; -prune
# Users don't need framework documentation and headers, and developers can get those by downloading 
them separately

find . -name ‘*.minx' -exec rm {} \; -prune
find . -name ‘Documentation' -exec rm -rf {} \; -prune
find . -name ‘Headers' -exec rm -rf {} \; -prune
find . -name ‘PrivateHeaders' -exec rm -rf {} \; -prune
echo "Stripping debugger symbol table entries from Mach-O files"
find . -type f -print | xargs file | grep Mach-O | awk -F: ‘{print $1}' | while read file
do
   echo "  $file"
   strip -S $file
   chmod a+x $file
done
if [ "$APPNAME" = "OmniWeb" ]; then
   echo "Moving plugins into $APPNAME plugins"
   mv *.plugin $APPNAME.app/Contents/PlugIns
fi
echo "Moving frameworks into $APPNAME frameworks"
mkdir $APPNAME.app/Contents/Frameworks
mv *.framework $APPNAME.app/Contents/Frameworks
if [ -d OmniGroupCrashCatcher.app ]; then
   echo "Moving OmniGroupCrashCatcher into $APPNAME resources"
   mv OmniGroupCrashCatcher.app $APPNAME.app/Contents/Resources
fi
echo "Setting up launcher"
mv $APPNAME.app/Contents/MacOS/{,.}$APPNAME
mv Launcher $APPNAME.app/Contents/MacOS/$APPNAME
echo "Total size of $PWD"
du -s *

echo "Building tar file"
PACKAGEDATE=`date "+%Y-%m-%d"`
TARBALL_NAME="$APPNAME-$PACKAGEDATE.tar.gz"
TARBALL="$PUBLIC/$TARBALL_NAME"
tar czf $TARBALL $APPNAME.app
echo "Building Disk Copy image"
LABELNAME="$APPNAME-$PACKAGEDATE"
SCRATCHIMAGE="$PUBLIC/${LABELNAME} Temp-$$.dmg"
DISKCOPYIMAGE="$TARBALL:r:r.dmg"
# Create the image
echo "  Creating scratch image..."
rm -f $SCRATCHIMAGE
hdiutil create $SCRATCHIMAGE -megabytes 10 -layout NONE -zeroImage
# Create a /dev/disk device from the image
drive=`hdid -nomount $SCRATCHIMAGE`
# Create a new filesystem on the disk device
newfs_hfs -v "${APPNAME}" -b 4096 /dev/r${drive:t}
# Remount the disk
echo "  Image formatted, ejecting ${drive}..."
hdiutil eject ${drive}
echo "  Mounting $SCRATCHIMAGE..."
drive=`hdid $SCRATCHIMAGE`
echo "  Searching for ${drive}..."
while [ "$MOUNTPOINT" = "" ]
do
   MOUNTPOINT=`df -l | grep $drive | awk ‘{print $6}'`
done
echo "  Found $drive at $MOUNTPOINT"
# Unpack the tarball into the mounted filesystem
echo "  Copying application..."
(cd $MOUNTPOINT && gnutar xzpf $TARBALL)
# Eject the disk
echo "  Ejecting..."
hdiutil eject ${drive}
# Convert the image to a UDIF compressed image 
echo "  Compressing..."
hdiutil convert -format UDCO $SCRATCHIMAGE -o ${DISKCOPYIMAGE:r}
gzip -9 < $DISKCOPYIMAGE > $DISKCOPYIMAGE.gz
# Remove the temporary image
echo "  Removing scratch image"
rm -f $SCRATCHIMAGE
#
# All done! List the resulting packages to the user.
#
ls -lg $TARBALL $DISKCOPYIMAGE $DISKCOPYIMAGE.gz

Getting Fancy

The automated process is great if you are happy with a standard folder with your application in it. You can use the utility ditto to copy files that have resource forks with them — or you can just drag and drop them onto the disk image. Since Finder lets you associate images with folders, let's take advantage! I have not yet figured out how to automate the process of adding a background folder image — because the image MUST point to within the new file hierarchy on your disk image — and this has to be done with Finder. If you preset it on a folder which you then copy to your disk, the copied folder will point back to the original image, which will not be on the end user's disk!

The Finder stores information about a folder in a file named .DS_Store — normally you cannot see files that begin with a ‘.'. Use the -a option to ls to see this file:

ls -la 
-rw-rw-rw-  1 andrew admin    22604 Mar 26 17:28 .DS_Store

To set the background image of a folder:

1. In Finder, select the folder, and set the view to Icon mode

2. Choose View -> Show View Options... to bring up the View Options window.

3. Select the "Window" pane. You must have the Finder window in "Icon" mode to access this feature.

4. Uncheck the "Use Global View Preferences" switch

5. Select the "Picture" option, and click "Select..."

6. Navigate to an image included within your new disk image's file hierarchy and click "Choose"


Figure 1. For a professional touch, include an instructive image in the top level folder.

You can also set a custom icon for any folder — these do copy fine via Finder or ditto, since they are stored in the folder. Our artist created them in Mac OS 9 with IconFactory, a plugin for PhotoShop http://www.iconfactory.com. In Mac OS X, you can copy/paste folder icons like this:

1. In Finder, select the folder which has the icon you like

2. Choose File -> Show Info

3. Click on the icon in the upper left hand corner, a box will show that it is selected

4. Choose Edit -> Copy

5. Navigate to the destination folder on which you want the icon to appear.

6. Click on the Info window's icon

7. Choose Edit -> Paste


Figure 2. Folders can have their own special icon.

You're Toast

Making a CD master image is very similar to making a net distribution, with a few changes in the creation of the disk partition, in the initializing of the new file system, and in the compression phase (steps 4., 6. and 10. from The Tools above). When we create the disk image, we'll use the SPUD layout and make it large enough to hold all those quicktime movies and demo files!

hdiutil create ‘APPNAME-scratch.dmg' -megabytes 160 -layout SPUD -zeroImage

The process continues the same until we initialize the new disk — we want to intialize the second partition:

newfs_hfs -v APPNAME -w -b 4096 /dev/disk1s2

The process continues the same until we get to the very end when we make a raw disk image for use by Toast or other cd recording software:

hdiutil convert ‘APPNAME-scratch.dmg' -format UDTO -o ‘APPNAME_CD_2000-04-06'

You are ready to burn your golden master and you'll be shipping CDs within days.

Conclusion

The disk image format is a simple and surefire way to distribute your software electronically, and the same tools let you create CD master images.

Thanks to Jim Kateley and Eric Peyton of Apple Computer, Inc., who assisted me in understanding these tools, Byron Han, principle engineer on the DiskImages project, and Ken Case for PackageApplication.


Andrew Stone, CEO of Stone Design, www.stone.com, has been coding in Cocoa as an independent software developer for over 13 years.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Fresh From the Land Down Under – The Tou...
After a two week hiatus, we are back with another episode of The TouchArcade Show. Eli is fresh off his trip to Australia, which according to him is very similar to America but more upside down. Also kangaroos all over. Other topics this week... | Read more »
TouchArcade Game of the Week: ‘Dungeon T...
I’m a little conflicted on this week’s pick. Pretty much everyone knows the legend of Dungeon Raid, the match-3 RPG hybrid that took the world by storm way back in 2011. Everyone at the time was obsessed with it, but for whatever reason the... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for July 19th, 2024. In today’s article, we finish up the week with the unusual appearance of a review. I’ve spent my time with Hot Lap Racing, and I’m ready to give my verdict. After... | Read more »
Draknek Interview: Alan Hazelden on Thin...
Ever since I played my first release from Draknek & Friends years ago, I knew I wanted to sit down with Alan Hazelden and chat about the team, puzzle games, and much more. | Read more »
The Latest ‘Marvel Snap’ OTA Update Buff...
I don’t know about all of you, my fellow Marvel Snap (Free) players, but these days when I see a balance update I find myself clenching my… teeth and bracing for the impact to my decks. They’ve been pretty spicy of late, after all. How will the... | Read more »
‘Honkai Star Rail’ Version 2.4 “Finest D...
HoYoverse just announced the Honkai Star Rail (Free) version 2.4 “Finest Duel Under the Pristine Blue" update alongside a surprising collaboration. Honkai Star Rail 2.4 follows the 2.3 “Farewell, Penacony" update. Read about that here. | Read more »
‘Vampire Survivors+’ on Apple Arcade Wil...
Earlier this month, Apple revealed that poncle’s excellent Vampire Survivors+ () would be heading to Apple Arcade as a new App Store Great. I reached out to poncle to check in on the DLC for Vampire Survivors+ because only the first two DLCs were... | Read more »
Homerun Clash 2: Legends Derby opens for...
Since launching in 2018, Homerun Clash has performed admirably for HAEGIN, racking up 12 million players all eager to prove they could be the next baseball champions. Well, the title will soon be up for grabs again, as Homerun Clash 2: Legends... | Read more »
‘Neverness to Everness’ Is a Free To Pla...
Perfect World Games and Hotta Studio (Tower of Fantasy) announced a new free to play open world RPG in the form of Neverness to Everness a few days ago (via Gematsu). Neverness to Everness has an urban setting, and the two reveal trailers for it... | Read more »
Meditative Puzzler ‘Ouros’ Coming to iOS...
Ouros is a mediative puzzle game from developer Michael Kamm that launched on PC just a couple of months back, and today it has been revealed that the title is now heading to iOS and Android devices next month. Which is good news I say because this... | Read more »

Price Scanner via MacPrices.net

Amazon is still selling 16-inch MacBook Pros...
Prime Day in July is over, but Amazon is still selling 16-inch Apple MacBook Pros for $500-$600 off MSRP. Shipping is free. These are the lowest prices available this weekend for new 16″ Apple... Read more
Walmart continues to sell clearance 13-inch M...
Walmart continues to offer clearance, but new, Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBooks... Read more
Apple is offering steep discounts, up to $600...
Apple has standard-configuration 16″ M3 Max MacBook Pros available, Certified Refurbished, starting at $2969 and ranging up to $600 off MSRP. Each model features a new outer case, shipping is free,... Read more
Save up to $480 with these 14-inch M3 Pro/M3...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
Amazon has clearance 9th-generation WiFi iPad...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Apple is offering a $50 discount on 2nd-gener...
Apple has Certified Refurbished White and Midnight HomePods available for $249, Certified Refurbished. That’s $50 off MSRP and the lowest price currently available for a full-size Apple HomePod today... Read more
The latest MacBook Pro sale at Amazon: 16-inc...
Amazon is offering instant discounts on 16″ M3 Pro and 16″ M3 Max MacBook Pros ranging up to $400 off MSRP as part of their early July 4th sale. Shipping is free. These are the lowest prices... Read more
14-inch M3 Pro MacBook Pros with 36GB of RAM...
B&H Photo has 14″ M3 Pro MacBook Pros with 36GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 Pro MacBook Pro (... Read more
14-inch M3 MacBook Pros with 16GB of RAM on s...
B&H Photo has 14″ M3 MacBook Pros with 16GB of RAM and 512GB or 1TB SSDs in stock today and on sale for $150-$200 off Apple’s MSRP, each including free 1-2 day shipping: – 14″ M3 MacBook Pro (... Read more
Amazon is offering $170-$200 discounts on new...
Amazon is offering a $170-$200 discount on every configuration and color of Apple’s M3-powered 15″ MacBook Airs. Prices start at $1129 for models with 8GB of RAM and 256GB of storage: – 15″ M3... Read more

Jobs Board

*Apple* Systems Engineer - Chenega Corporati...
…LLC,** a **Chenega Professional Services** ' company, is looking for a ** Apple Systems Engineer** to support the Information Technology Operations and Maintenance Read more
Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
*Apple* / Mac Administrator - JAMF Pro - Ame...
Amentum is seeking an ** Apple / Mac Administrator - JAMF Pro** to provide support with the Apple Ecosystem to include hardware and software to join our team and Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.