Files
2022-05-22 22:22:12 +03:00

135 lines
3.9 KiB
Bash
Executable File

#!/usr/bin/env bash
#Create DMG file
# 1 - Working folder
# 2 - Result full dmg path
# 3 - Application.app path
# 4 - Application name
# 5 - Title
# 6 - Background image
# 7 - Disk icon
# 8 - Application folder image
packageDMG() {
WORK_FOLDER=$1
RESULT_DMG_PATH=$2
APPLICATION_PATH=$3
APPLICATION_NAME=$4
APPLICATION_TITLE=$5
BACKGROUND_IMAGE=$6
ICON_IMAGE=$7
APPLICATION_FOLDER_IMAGE=$8
# Check the background image DPI and convert it if it isn't 144 x 144
_BACKGROUND_IMAGE_DPI_H=`sips -g dpiHeight ${BACKGROUND_IMAGE} | grep -Eo '[0-9]+\.[0-9]+'`
_BACKGROUND_IMAGE_DPI_W=`sips -g dpiWidth ${BACKGROUND_IMAGE} | grep -Eo '[0-9]+\.[0-9]+'`
if [ $(echo " $_BACKGROUND_IMAGE_DPI_H != 144.0 " | bc) -eq 1 -o $(echo " $_BACKGROUND_IMAGE_DPI_W != 144.0 " | bc) -eq 1 ]; then
echo "WARNING: The background image's DPI is not 144. This will result in distorted backgrounds on Mac OS X 10.7+."
echo " I will convert it to 144 DPI for you."
_DMG_BACKGROUND_TMP="${BACKGROUND_IMAGE%.*}"_dpifix."${BACKGROUND_IMAGE##*.}"
sips -s dpiWidth 144 -s dpiHeight 144 ${BACKGROUND_IMAGE} --out ${_BACKGROUND_IMAGE_TMP}
BACKGROUND_IMAGE="${_BACKGROUND_IMAGE_TMP}"
fi
# Clear previous folders
DMG_TMP=tmp.dmg
rm -rf "$RESULT_DMG_PATH"
rm -rf "${WORK_FOLDER}"
rm -rf $DMG_TMP
# Create Working folder
mkdir -p $WORK_FOLDER
# Copy Application to folder -rpf?
cp -R $APPLICATION_PATH $WORK_FOLDER
#Create link to Application folder
ln -s /Applications $WORK_FOLDER/Applications
# Change Application folder image
#sips -i "$APPLICATION_FOLDER_IMAGE"
#DeRez -only icns "$APPLICATION_FOLDER_IMAGE" > tmpicns.rsrc
#Rez -append tmpicns.rsrc -o $WORK_FOLDER/Applications
#SetFile $WORK_FOLDER/Applications
#rm tmpicns.rsrc
# add a background image
mkdir "${WORK_FOLDER}"/.background
cp "$BACKGROUND_IMAGE" "${WORK_FOLDER}"/.background/
# Calculate size
SIZE=`du -sh "${WORK_FOLDER}" | sed 's/\([0-9]*\)[KM]\(.*\)/\1/'`
SIZE=`echo "${SIZE} + 1.0" | bc | awk '{print int($1+5.5)}'`
if [ $? -ne 0 ]; then
echo "Error: Cannot compute size of staging dir"
return 1
fi
#hdiutil makehybrid -hfs -hfs-volume-name "$APPLICATION_TITLE" -hfs-openfolder "${WORK_FOLDER}" "${WORK_FOLDER}" -o $DMG_TMP
hdiutil create -srcfolder "${WORK_FOLDER}" -volname "${APPLICATION_TITLE}" -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -size ${SIZE}M $DMG_TMP
# mount it and save the device
DEVICE=$(hdiutil attach -readwrite -noverify "${DMG_TMP}" | egrep '^/dev/' | sed 1q | awk '{print $1}')
# Wait before mounted!
sleep 2
BACKGROUND_FILE=$(basename $BACKGROUND_IMAGE)
# tell the Finder to resize the window, set the background,
# change the icon size, place the icons in the right position, etc.
echo '
tell application "Finder"
tell disk "'${APPLICATION_TITLE}'"
open
set current view of container window to icon view
set toolbar visible of container window to false
set statusbar visible of container window to false
set the bounds of container window to {200, 200, 720, 540}
set viewOptions to the icon view options of container window
set arrangement of viewOptions to not arranged
set icon size of viewOptions to 72
set text size of viewOptions to 10
set background picture of viewOptions to file ".background:'${BACKGROUND_FILE}'"
set position of item "'${APPLICATION_NAME}'.app" of container window to {136, 172}
set position of item "Applications" of container window to {380, 172}
close
open
update without registering applications
delay 3
close
end tell
end tell
' | osascript
sync
# Detach device
hdiutil detach "${DEVICE}"
# Lock image
hdiutil convert "${DMG_TMP}" -format UDZO -imagekey zlib-level=9 -o "${RESULT_DMG_PATH}"
# Set icon
RSRC=icns.rsrc
sips -i "${ICON_IMAGE}"
DeRez -only icns "${ICON_IMAGE}" > $RSRC
Rez -append $RSRC -o "${RESULT_DMG_PATH}"
SetFile -a C "${RESULT_DMG_PATH}"
rm -rf $RSRC
rm -rf ${DMG_TMP}
#rm $ICON_TMP
}