我需要为Mac应用程序创建一个不错的安装程序。我希望它是具有预定义大小,布局和背景图像的磁盘映像(DMG)。
我需要在脚本中以编程方式进行此操作,以将其集成到现有的构建系统中(实际上更多的是打包系统,因为它仅创建安装程序。构建是单独完成的)。
我已经使用“ hdiutil”完成了DMG的创建,但我还没有发现如何进行图标布局和指定背景位图。
经过大量研究,我想出了这个答案,在此我将其作为我自己问题的答案,以供参考:
确保在系统偏好设置>>通用访问中选中“启用辅助设备访问”。AppleScript必须运行。进行此更改后,您可能必须重新引导(否则在Mac OS X Server 10.4上将不起作用)。
创建一个R / W DMG。它必须大于结果。在此示例中,bash变量“ size”包含以Kb为单位的大小,并且“ source” bash变量中文件夹的内容将被复制到DMG中:
hdiutil create -srcfolder "${source}" -volname "${title}" -fs HFS+ \
-fsargs "-c c=64,a=16,e=16" -format UDRW -size ${size}k pack.temp.dmg
挂载磁盘映像,并存储设备名称(此操作后,您可能要使用sleep几秒钟):
device=$(hdiutil attach -readwrite -noverify -noautoopen "pack.temp.dmg" | \
egrep '^/dev/' | sed 1q | awk '{print $1}')
将背景图片(以PNG格式)存储在DMG中名为“ .background”的文件夹中,并将其名称存储在“ backgroundPictureName”变量中。
使用AppleScript设置视觉样式(.app的名称必须在bash变量“ applicationName”中,根据需要将变量用于其他属性):
echo '
tell application "Finder"
tell disk "'${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 {400, 100, 885, 430}
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:'${backgroundPictureName}'"
make new alias file at container window to POSIX file "/Applications" with properties {name:"Applications"}
set position of item "'${applicationName}'" of container window to {100, 100}
set position of item "Applications" of container window to {375, 100}
update without registering applications
delay 5
close
end tell
end tell
' | osascript
通过正确设置权限,压缩和释放DMG,最终完成DMG:
chmod -Rf go-w /Volumes/"${title}"
sync
sync
hdiutil detach ${device}
hdiutil convert "/pack.temp.dmg" -format UDZO -imagekey zlib-level=9 -o "${finalDMGName}"
rm -f /pack.temp.dmg
在Snow Leopard上,上面的applescript无法正确设置图标的位置-似乎是Snow Leopard的错误。一种解决方法是在设置图标后简单地调用关闭/打开,即:
..
set position of item "'${applicationName}'" of container window to {100, 100}
set position of item "Applications" of container window to {375, 100}
close
open
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句