Monday, November 16, 2009

Getting background pictures on disk images to “stick”

As a part of working on an installer for TeachTown, I ran into a problem where we were trying to make an automated build process for the installer and making a nice disk image for the installer: the process of assigning a background image via AppleScript would work in the moment, but after you hdituil detached the image, it would vanish. e.g.,
osascript -e 'tell app "Finder" to set background picture of icon view options of container window of disk "disk_name" to POSIX file "full_path_to_image_file_on_disk_image"'
hdituil detach /Volumes/disk_name

We found that running sync wouldn’t help, but that if we waited a while (e.g., sleep 5), it would usually “take.” Well, Apple replied with the Magic™: The Finder doesn’t commit the changes made to Finder metadata immediately to disk, and whereas if you told the Finder to eject the disk, it would know better, write its metadata and then eject, but for whatever reason hdiutil detach would just unmount the disk image out from under the Finder and it wouldn’t necessarily have known to write the metadata first.

Workaround and/or fix: Use the update disk verb to force the Finder to write out the metadata. e.g.,
osascript -e 'tell app "Finder" to update disk "disk_name"'

That should make sure that background image and icon size/placement settings that you made get committed before the detach.

No comments: