Skip to content

Instantly share code, notes, and snippets.

@nanoant

nanoant/dmg.sh

Created Sep 16, 2014
Embed
What would you like to do?
DMG builder script
#!/bin/bash
# set -e
#
# Builds .dmg installer
#
# Copyright (C) 2013-2014 Adam Strzelecki
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
if [ $# -lt 3 ]; then
echo >&2 $0 path/to/some.app path/to/background.tif path/to/output.dmg
exit 1
fi
# Settings
tlx=400 # screen x position of installed
tly=100 # screen y position of installed
brx=$(($tlx+500)) # x+width
bry=$(($tly+365)) # y+height
appx=133 # x position of .app icon
Appx=378 # x position of Applications
appy=200 # y position of both icons
app=$1
background=$2
out=$3
name=${app##*/}
name=${name%.app}
temp=/tmp/$name.$$.dmg
size=$(du -sk "$app" | cut -f1)
bgsize=$(du -sk "$background" | cut -f1)
size=$(($size + $bgsize + 4096)) # +4MB for additional stuff
# shows command being run
run() { echo + "$@"; "$@"; }
# create rw dmg
run hdiutil create \
-skipunreadable \
-anyowners \
-nospotlight \
-layout NONE \
-ov \
-size "${size}k" \
-srcfolder /System/.localized \
-srcfolder "$app" \
-volname "$name" \
-fs HFS+ \
-fsargs "-c c=64,a=16,e=16" \
-format UDRW \
"$temp"
# NOTE: using two srcfolder makes dmg containing them as items rather than root,
# therefore /System/.localized is just dummy file triggering this behavior
# open dmg and check its path
while read line; do
if [[ "$line" =~ (/dev/disk[0-9]+)[^s] ]]; then
dev="${BASH_REMATCH[1]}"
fi
if [[ "$line" =~ (/Volumes/.*) ]]; then
vol="${BASH_REMATCH[1]}"
fi
done < <(
run hdiutil attach \
-readwrite \
-noverify \
-noautoopen \
"$temp"
)
[ -z "$vol" ] && exit 1
# fix settings
chmod -Rf go-w "$vol" 2>/dev/null
# make folder popup on open
bless --folder "$vol" --openfolder "/Volumes/$name"
# copy background
mkdir -p "$vol/.background"
cp -p "$background" "$vol/.background/background.tif"
ln -s /Applications "$vol"
osascript <<EOF
tell application "Finder"
tell disk "$name"
open
set current view of container window to icon view
set theViewOptions to the icon view options of container window
set background picture of theViewOptions to file ".background:background.tif"
set arrangement of theViewOptions to not arranged
set icon size of theViewOptions to 128
tell container window
set sidebar width to 0
set statusbar visible to false
set toolbar visible to false
set the bounds to { $tlx, $tly, $brx, $bry }
set position of item "$name" to { $appx, $appy }
set position of item "Applications" to { $Appx, $appy }
end tell
update without registering applications
close
eject
end tell
end tell
EOF
# hdiutil detach "$dev"
while [ -d "$vol" ]; do
sleep .1
done
# convert to bzipped ro
run hdiutil convert "$temp" \
-format UDBZ \
-ov \
-o "$out"
rm $temp
# open
run hdiutil attach \
-noverify \
"$out"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.