如何shell脚本生成dmg包
时间: 2023-06-30 13:27:22 浏览: 137
dmgify:创建一个自定义dmg
您可以使用以下shell脚本来生成dmg包:
```shell
#!/bin/bash
APP_NAME="YourAppName"
APP_SRC="path/to/your/app"
APP_ICON="path/to/your/icon.icns"
DMG_BACKGROUND="path/to/background.jpg"
DMG_NAME="$APP_NAME.dmg"
VOL_NAME="$APP_NAME"
# Create a temporary working directory
WORK_DIR=`mktemp -d /tmp/${APP_NAME}-dmg.XXXXXX` || exit 1
# Copy the application to the working directory
cp -R "$APP_SRC" "$WORK_DIR/$APP_NAME.app"
# Set the icon of the application
sips -i "$APP_ICON" --out "$WORK_DIR/$APP_NAME.app/Contents/Resources/app.icns"
# Create a symlink to the Applications folder
ln -s /Applications "$WORK_DIR"
# Create the DMG
hdiutil create -srcfolder "$WORK_DIR" -volname "$VOL_NAME" -fs HFS+ -fsargs "-c c=64,a=16,e=16" -format UDRW -size 100m "$DMG_NAME"
# Mount the DMG
MOUNT_DIR=`mktemp -d /tmp/${APP_NAME}-mount.XXXXXX` || exit 1
hdiutil attach -nobrowse -mountpoint "$MOUNT_DIR" "$DMG_NAME"
# Add the background image
cp "$DMG_BACKGROUND" "$MOUNT_DIR/.background.jpg"
ln -s /Applications "$MOUNT_DIR/Applications"
# Set the icon size of the DMG window
WINDOW_SIZE="400 400"
echo "
tell application \"Finder\"
tell disk \"$VOL_NAME\"
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 {0, 0, $WINDOW_SIZE}
set theViewOptions to the icon view options of container window
set arrangement of theViewOptions to not arranged
set icon size of theViewOptions to 72
set background picture of theViewOptions to file \".background.jpg\"
close
open
update without registering applications
delay 2
set statusbar visible of container window to false
set toolbar visible of container window to false
set the bounds of container window to {0, 0, $WINDOW_SIZE}
end tell
end tell
" | osascript
# Unmount the DMG
hdiutil detach "$MOUNT_DIR"
rm -rf "$MOUNT_DIR"
# Convert the DMG to a compressed read-only format
hdiutil convert "$DMG_NAME" -format UDZO -imagekey zlib-level=9 -o "$APP_NAME-Compressed.dmg"
# Clean up the working directory
rm -rf "$WORK_DIR"
# Open the DMG
open "$APP_NAME-Compressed.dmg"
```
在脚本中,您需要将以下变量替换为您的实际值:
- APP_NAME:您的应用程序名称
- APP_SRC:您的应用程序源代码的路径
- APP_ICON:您的应用程序图标的路径
- DMG_BACKGROUND:DMG包的背景图像的路径
- DMG_NAME:生成的DMG文件的名称
- VOL_NAME:DMG的卷名称
运行脚本后,它将创建一个临时工作目录,并将应用程序复制到该目录中。然后,它将设置应用程序的图标,创建一个指向应用程序的符号链接,并创建一个DMG文件。接下来,它将挂载DMG文件,并将背景图像添加到DMG文件中。最后,它将设置DMG窗口中的图标大小和背景图像,并将DMG转换为压缩的只读格式。最终,它将删除工作目录并打开DMG文件。
阅读全文