setting view options on .dmg files

Dear Lazyweb,

How do I set the Finder view options on a .dmg file that I am creating?

Answer: <LJ-CUT text=" --More--( 5%) ">

  • Create a writable .dmg file, and set your Finder view options the way you like.
  • Unmount that .dmg.
  • Remount it.
  • Save a copy of its .DS_Store file.
    (You have to do this dance because Finder only flushes changes out to the .DS_Store files when unmounting, or when you log out.)

To build your final .dmg file:

  • Creating a "staging" directory and populate it with your files.
  • Copy your saved .DS_Store file into the staging directory.
  • hdiutil makehybrid -hfs -hfs-volume-name NAME -hfs-openfolder STAGING STAGING -o TMP.dmg
  • hdiutil convert -format UDZO TMP.dmg -o FINAL.dmg

You should be able to do that in one step, but that doesn't work. If you don't do "makehybrid" first, and then convert it to a compressed format, Finder will ignore some of your view settings (like icons/list/columns).

A useful tool is Mozilla's "pkg-dmg" script: it supports creation of .dmg files that show a license agreement when mounted, and a bunch of other things. To download it:

    cvs -d :pserver:anonymous@cvs-mirror.mozilla.org:/cvsroot co mozilla/build/package/mac_osx
Tags: , , ,

21 Responses:

  1. AFAIK, this is impossible. The binary format for storing backgrounds & such has not been reverse-engineered[1], and apple isn't sharing.

    [1] Except by this one company, who sells a .dmg utility, whose name I can't for the life of me remember.

  2. tarzxf says:

    Reading up at http://www.personal.kent.edu/~blukens/viewoptions.html says that HOW the windows is opened controls the use of the view options:

    Toolbar visibility is a little unique. While the setting is actually stored in the same .DS_Store as icon positions (and so follows the same rules for modifying), the Finder will often ignore it. When you open a folder in the Finder by double-clicking its icon in its parent folder, toolbar visibility remains in the same state as it was in the previously open window. If, however, you open the folder from the Dock, the Finder's "Go" menu, a desktop alias, another application, or through spring-loaded dragging then the window's toolbar setting will be respected.

  3. ac78 says:

    These two links might help you, the first is a solution if you want to throw money at the problem but I think the second will work just as well for you.

    http://c-command.com/dropdmg/
    http://el-tramo.be/guides/fancy-dmg/

    • jwz says:

      Hmm, I don't think that second link describes anything different than what I just described...

      • ac78 says:

        The method described in the second link doesn't copy the .DS_Store file like you're doing, instead it creates a template disk image formatted the way you prefer. Then duplicates that disk image, populates it with your files and converts this new image to a compressed read-only DMG.

        It may still not work for you, but it is different than what you described in that the method I suggested uses the DMG file as a template rather than trying to move the DS_Store file.

  4. blasdelf says:

    I spent about 10 hours on Wednesday with my friend Ryan figuring this out. We are also finishing up on porting a Unix app to have a native Cocoa Preference Pane UI.

    The secret is to make the .dmg READ ONLY, with a customized .DS_Store file in it. The Finder treats read-only images differently - it opens them in a new finder window, and generally respects the options set in it's .DS_Store file.

    The Procedure:

    • Make a normal .dmg with your shit in it
    • Funk up it's .DS_Store - give it a nice background image, 128px icon view, and a small non-metal window
    • Use Disk Utility to convert it to read-only
    • blasdelf says:

      The Finder's behavior when opening read-only disk images may be Tiger-specific, I don't know for sure. We didn't have to test for 10.3 (our app uses launchd), so we didn't check.

      I think the Liscense Agreement pop-up stuff you see sometimes (Guess it's for those times when you want a meaningless click through EULA, but don't want an unnecessary installer) is done with some sort of undocumented trick that Michael Tsai's Drop DMG implements, among other doo-dad features.

      • Actually, the license agreement stuff is documented- Apple has a technote or something on it. You use hdiutil to "unflatten" the image so it has a resource fork, then add resources containing the agreement in an ancient crufty 1980's styled-text format, then re-flatten the image so that it doesn't have a resource fork any more. Bizarrely, the resources get converted into some sort of XML blob in the dmg, but that is undocumented...

    • blasdelf says:

      Oh yeah, one more thing -

      The reason .DS_Store settings aren't always respected with Read-Write disk images is because the Finder usually opens them in-context, as if they were a normal folder. There may also be an issue with the Finder writing different settings (like your default ones) to the .dmg's .DS_Store file, simply because it can.

      Basically, use read-only images for distribution.

    • jwz says:

      It's definitely read-only, since I'm using -format UDBZ.

      • blasdelf says:

        You're using a command line utility to create it as read-only to start with? Never tried that.

        Try:

        1. Making a normal read-write disk image with disk utility
        2. Mounting the fucker in the Finder
        3. Put your shit in it the way you like it
        4. Dismount it, making sure the .DS_Store is written
        5. Then open it with Disk Utility and convert it to read-only

        That should work.

        I just did this wednesday, It took my buddy and I a while to figure it out (though we spent most of that time making a shiny Web 2.0 background image for the .dmg)

        We found a bunch of information in the archives @ Cocoa Builder, a generally pretty useful repository of OS X development minutae.

        I'm pretty sure DropDMG would make it even easier.

        • jwz says:

          If I can't do this shit from a Makefile, it is of zero use to me.

          This is crazy: I've got "template.dmg", which was made like this:

            mkdir build/empty
            cp -p bindist-DS_Store build/empty/.DS_Store
            hdiutil create -size 1gb -format UDRW -volname XScreenSaver -srcfolder build/empty template.dmg

          When I open that, it shows up with my view settings, and in "list" mode. Then I do this:

            hdiutil convert template.dmg -format UDRO -o out.dmg

          and when I open "out.dmg", it shows up in "columns" mode! Arrgh!!

          • jwz says:

            And it does the same thing if I use the "convert" command in Disk Utility.

          • blasdelf says:

            Can you get it to work at all, using the method I described? As in, make an empty disk image, put shit in it, make it look pretty, dismount, convert to read only?

            Try getting it to work at all first, then try to automate it.

            It doesn't take more than a few minutes to do once you figure it out, and you only have to do it for stuff you're gonna distribute.

  5. danomite55 says:

    I create a sparse disk image once, with the layout and background image as I like. I copy the background image into a folder called "invisible" on the disk image, and then use the Folder View Options to set the background image, ensuring that I'm selecting the image from the mounted disk image and not my local drive. Then using SetFile, I make that folder invisible:

    /Developer/Tools/SetFile -a V "/Volumes/My Volume/invisible/"

    Now I have the disk image setup the way I want, with the icons whatever size and position I want, and the background image to also be custom.

    In Xcode, I have a shell script target (New Target, Special Targets, Shell Script Target), that creates my distribution dmg file. My script mounts my sparse image, uses rsync to copy the .app and .saver folders, and then converts the image to the dmg file. It looks something like:

    SRCROOT=$PROJECT_DIR

    hdiutil attach "$SRCROOT/Distribution/DigitalAlarmClockReadWriteImage.sparseimage"

    rsync -avE "$BUILT_PRODUCTS_DIR/Digital Alarm Clock.app/" "/Volumes/Digital Alarm Clock 2.6/Digital Alarm Clock.app/"
    rsync -avE "$BUILT_PRODUCTS_DIR/Digital Alarm Clock Screen Saver.saver/" "/Volumes/Digital Alarm Clock 2.6/Extras/Digital Alarm Clock Screen Saver.saver/"

    hdiutil detach "/Volumes/Digital Alarm Clock 2.6"

    hdiutil compact "$SRCROOT/Distribution/DigitalAlarmClockReadWriteImage.sparseimage"
    hdiutil convert -ov -format UDZO -o "$SRCROOT/Distribution/DigitalAlarmClockDistributionImage" "$SRCROOT/Distribution/DigitalAlarmClockReadWriteImage.sparseimage"

    I have my "Input Files" set to the Release builds of my project so that it only rebuilds the final DMG if one of these input files has changed.

  6. brianenigma says:

    I was able to do something like this using information from a knowledgebase article on Roxio's (the makers of Toast) website. As far as I can tell, it's chicken bone voodoo magic, but it worked. You have to create the DMG, mount it, adjust it exactly as you want it to look (including icon placement, background image, hiding files, etc.), then log out of the system. Yes, log out of the system(?!) and it magically remembers all of those settings in the DMG that it wouldn't remember before.

    • riffraff says:

      this is because the finder, in apple's infinite wisdom, doesn't save those settings (window position, state, etc) until it quits.

      wtf? don't ask me... this is yet another of the finder's problems since it took on the dual job of spatial finder of yore and also column browser/spotlight bitch.

      • jwz says:

        Yeah, something I read said that Finder also flushes out that info when a volume is unmounted (and this appears to be true); so if the .DS_Store you're talking about is inside a .dmg, you can just unmount/remount rather than having to quit finder, which is why I did that dance above.

  7. kalperin says:

    jwz, thanks for this information, it was incredibly helpful. For fellow lazyweb travelers, here's another disk image recipe: http://www.heliumfoot.com/blog/19 .

    Cheers,
    Keith Alperin