Search code examples
bashiconsmacos-sierradmg

how to programmatically (preferably bash) add icon to dmg?


I have a bash script that creates a .dmg for my node.js app. However, the icon for the .dmg that appears in Finder is the standard disk icon. I need it to be the same icon as the app.

I know I can cmd+I on my .app in Finder, copy the icon, and cmd+I on my .dmg and paste. But I want my build process to be fully automated. I prefer not to use a third-party tool for this, since I already have code signing and localization working in my bash script.

I have tried copying my icns as .VolumeIcon.icns into the root of my dmg. It has no effect.

I have also tried SetFile -a C "path/to/dmg/.VolumeIcon.icns". That doesn't have any effect either, and man SetFile says it is deprecated anyway. I'm building on macOS Sierra 10.12.6.

Since a simple copy and paste works, there must be a way to do this programmatically! But how???

Update

This partially works:

hdiutil create -srcfolder "my/build/dir" -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -volname MyAppName -ov -attach "my/output/dir/MyAppName.uncompressed.dmg"
SetFile -c icnC "/Volumes/MyAppName/.VolumeIcon.icns"
SetFile -a C "/Volumes/MyAppName"

It does not associate the icon with the .dmg file itself, but if I double-click the .dmg file, the Finder window that is displayed has my icon.

The primary need, though, is for the .dmg file itself. When someone downloads my .dmg, I need the .dmg file sitting in the Downloads folder to have my icon instead of the standard disk icon. As noted above, that's easy manually, but how can I do it from within a bash script or AppleScript?


Solution

  • Let's say you have image like this

    enter image description here

    and it's saved as: cc.png. Do following

    cp cc.png cc_copy.png
    sips -i cc_copy.png
    DeRez -only icns cc_copy.png > icns.rsrc
    mkdir some_installer
    hdiutil create -volname installer -srcfolder ./some_installer -ov -format UDZO my_app.dmg
    Rez -append icns.rsrc -o my_app.dmg
    SetFile -a C my_app.dmg
    

    and you will end up with:

    enter image description here

    Is that what you are looking for?

    Update:

    Should work for icns as well

    cp /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/macOS.icns .
    cp macOS.icns macOS_copy.icns
    sips -i macOS_copy.icns
    DeRez -only icns macOS_copy.icns > icns.rsrc
    hdiutil create -volname installer -srcfolder ./some_installer -ov -format UDZO my_app.dmg
    Rez -append icns.rsrc -o my_app.dmg
    SetFile -a C my_app.dmg
    

    enter image description here