To ensure a disk image retains folder visual settings (such as background color/image, icon positions, and window size) when deploying an application on macOS, you need to carefully follow a specific process. Here's a detailed step-by-step guide to create a properly configured DMG (Disk Image) with persistent visual attributes:
This is the folder that will contain your app and any customizations.
mkdir -p dmg-staging/MyApp
cp -R /path/to/MyApp.app dmg-staging/MyApp/-
Open the
dmg-stagingfolder in Finder. -
Customize:
- Set the folder view to Icon View (Command + 1).
- Set icon sizes and arrange them as needed.
- Add a background image or color via View > Show View Options (Command + J).
- Adjust the window size and position.
-
VERY IMPORTANT: Make sure the
“.DS_Store”file is generated and saved. This file holds the visual configuration. -
Close the Finder window to ensure the
.DS_Storefile is written.
hdiutil create -srcfolder dmg-staging \
-volname "MyApp Installer" \
-fs HFS+ \
-format UDRW \
MyApp-temp.dmgThis creates a read/write image (UDRW) that you can modify.
Mount the image:
hdiutil attach MyApp-temp.dmgLet’s say it mounts at /Volumes/MyApp Installer.
- Open
/Volumes/MyApp Installerin Finder. - Do final adjustments to icon positions, background, etc., as needed.
- Again, close the window to save
.DS_Store.
Inside the mounted volume:
cd /Volumes/MyApp\ Installer
chmod -Rf go-w .This makes sure no unexpected modifications can be made.
Unmount the image:
hdiutil detach /Volumes/MyApp\ InstallerConvert it to a final compressed read-only image (UDZO):
hdiutil convert MyApp-temp.dmg -format UDZO -imagekey zlib-level=9 -o MyApp.dmgYou can now distribute MyApp.dmg and it will retain:
- Folder layout and icon positions
- Background color or image
- Custom window size
- Always make the
.DS_Storechanges within the disk image, not just the staging folder. - Use HFS+ (not APFS) for the DMG format to ensure Finder layout features work (backgrounds, icon positions, etc.).
- Don’t use third-party tools to modify images after converting them — this can break
.DS_Store.