Lud*_*rin 194
经过大量的研究,我得出了这个答案,我现在把它放在这里作为我自己问题的答案,供参考:
确保在"系统偏好设置">>"通用访问"中选中"启用辅助设备访问".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
Run Code Online (Sandbox Code Playgroud)挂载磁盘映像,并存储设备名称(此操作后您可能希望使用sleep几秒钟):
device=$(hdiutil attach -readwrite -noverify -noautoopen "pack.temp.dmg" | \
egrep '^/dev/' | sed 1q | awk '{print $1}')
Run Code Online (Sandbox Code Playgroud)将背景图片(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
Run Code Online (Sandbox Code Playgroud)通过正确设置权限,压缩和释放它来完成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
Run Code Online (Sandbox Code Playgroud)在Snow Leopard上,上面的AppleScript不会正确设置图标位置 - 它似乎是一个Snow Leopard bug.一种解决方法是在设置图标后简单地调用close/open,即:
..
set position of item "'${applicationName}'" of container window to {100, 100}
set position of item "Applications" of container window to {375, 100}
close
open
Run Code Online (Sandbox Code Playgroud)
And*_*sov 55
有一个名为create-dmg的小Bash脚本可以构建具有自定义背景,自定义图标定位和卷名的花式DMG.
我很多年前为我当时经营的公司建造了它; 它从那时起就依赖于其他人的贡献,据报道效果很好.
还有node-appdmg,它看起来像是基于Node.js的更现代和更积极的努力; 检查一下.
Mec*_*cki 37
不要去那里.作为一名长期的Mac开发人员,我可以向您保证,没有任何解决方案能够正常运行.我尝试了很多解决方案,但它们都不太好.我认为问题在于Apple并没有真正记录必要数据的元数据格式.
这是我很长时间以来非常成功的方式:
创建一个新的DMG,可写(!),大到足以容纳预期的二进制文件和额外的文件,如readme(稀疏可能工作).
安装DMG并在Finder中手动布局或使用适合您的任何工具(请参阅底部的FileStorm链接以获得一个好工具).背景图像通常是我们放入DMG上隐藏文件夹(".something")的图像.在那里放一个你的应用程序的副本(任何版本,甚至过时的版本都可以).复制你想要的其他文件(别名,自述文件等),再次,过时的版本会做得很好.确保图标具有正确的尺寸和位置(IOW,按照您希望的方式布置DMG).
再次卸载DMG,现在应该存储所有设置.
编写一个create DMG脚本,其工作方式如下:
这种方法可能听起来不是最佳,但相信我,它在实践中非常有效.您可以将原始DMG(DMG模板)置于版本控制之下(例如SVN),因此如果您不小心更改/销毁它,您可以返回到仍然可以的版本.您可以将DMG模板与属于DMG的所有其他文件(自述文件,URL文件,背景图像)一起添加到Xcode项目中,所有文件都在版本控制下,然后创建目标(例如,名为"创建DMG"的外部目标)然后运行上面的DMG脚本并将旧的主目标添加为依赖目标.您可以使用脚本中的$ {SRCROOT}访问Xcode树中的文件(始终是产品的源根目录),并且可以使用$ {BUILT_PRODUCTS_DIR}访问构建产品(始终是Xcode创建构建结果的目录) .
结果:实际上Xcode可以在构建结束时生成DMG.准备发布的DMG.您不仅可以通过这种方式轻松创建重新设置DMG,实际上您可以在自动化过程中(如果您愿意,在无头服务器上),使用命令行中的xcodebuild(例如,自动夜间构建).
关于模板的初始布局,FileStorm是一个很好的工具.它是商业的,但非常强大且易于使用.正常版本不到20美元,所以它真的很实惠.也许可以自动化FileStorm来创建DMG(例如通过AppleScript),从未尝试过,但是一旦找到完美的模板DMG,就可以很容易地为每个版本更新它.
Lin*_*äck 25
通过提供这个答案,使这个问题更新.
appdmg是一个简单易用的开源命令行程序,它从简单的json规范创建dmg文件.看看官方网站上的自述文件:
https://github.com/LinusU/node-appdmg
快速举例:
安装appdmg
npm install -g appdmg
Run Code Online (Sandbox Code Playgroud)写一个json文件(spec.json)
{
"title": "Test Title",
"background": "background.png",
"icon-size": 80,
"contents": [
{ "x": 192, "y": 344, "type": "file", "path": "TestApp.app" },
{ "x": 448, "y": 344, "type": "link", "path": "/Applications" }
]
}
Run Code Online (Sandbox Code Playgroud)运行程序
appdmg spec.json test.dmg
Run Code Online (Sandbox Code Playgroud)(免责声明.我是appdmg的创造者)
Lud*_*rin 22
对于那些对此主题感兴趣的人,我应该提一下我是如何创建DMG的:
hdiutil create XXX.dmg -volname "YYY" -fs HFS+ -srcfolder "ZZZ"
Run Code Online (Sandbox Code Playgroud)
哪里
XXX == disk image file name (duh!)
YYY == window title displayed when DMG is opened
ZZZ == Path to a folder containing the files that will be copied into the DMG
Run Code Online (Sandbox Code Playgroud)
如果要设置自定义音量图标,请使用以下命令
/*Add a drive icon*/
cp "/Volumes/customIcon.icns" "/Volumes/dmgName/.VolumeIcon.icns"
/*SetFile -c icnC will change the creator of the file to icnC*/
SetFile -c icnC /<your path>/.VolumeIcon.icns
Run Code Online (Sandbox Code Playgroud)
现在创建读/写 dmg
/*to set custom icon attribute*/
SetFile -a C /Volumes/dmgName
Run Code Online (Sandbox Code Playgroud)
我终于在我自己的项目中得到了这个工作(恰好在 Xcode 中)。将这 3 个脚本添加到您的构建阶段将自动为您的产品创建一个漂亮整洁的磁盘映像。您所要做的就是构建您的项目,DMG 将在您的产品文件夹中等待。
脚本 1(创建临时磁盘映像):
#!/bin/bash
#Create a R/W DMG
dir="$TEMP_FILES_DIR/disk"
dmg="$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.temp.dmg"
rm -rf "$dir"
mkdir "$dir"
cp -R "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.app" "$dir"
ln -s "/Applications" "$dir/Applications"
mkdir "$dir/.background"
cp "$PROJECT_DIR/$PROJECT_NAME/some_image.png" "$dir/.background"
rm -f "$dmg"
hdiutil create "$dmg" -srcfolder "$dir" -volname "$PRODUCT_NAME" -format UDRW
#Mount the disk image, and store the device name
hdiutil attach "$dmg" -noverify -noautoopen -readwrite
Run Code Online (Sandbox Code Playgroud)
脚本2(设置窗口属性脚本):
#!/usr/bin/osascript
#get the dimensions of the main window using a bash script
set {width, height, scale} to words of (do shell script "system_profiler SPDisplaysDataType | awk '/Main Display: Yes/{found=1} /Resolution/{width=$2; height=$4} /Retina/{scale=($2 == \"Yes\" ? 2 : 1)} /^ {8}[^ ]+/{if(found) {exit}; scale=1} END{printf \"%d %d %d\\n\", width, height, scale}'")
set x to ((width / 2) / scale)
set y to ((height / 2) / scale)
#get the product name using a bash script
set {product_name} to words of (do shell script "printf \"%s\", $PRODUCT_NAME")
set background to alias ("Volumes:"&product_name&":.background:some_image.png")
tell application "Finder"
tell disk product_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 {x, y, (x + 479), (y + 383)}
set theViewOptions to the icon view options of container window
set arrangement of theViewOptions to not arranged
set icon size of theViewOptions to 128
set background picture of theViewOptions to background
set position of item (product_name & ".app") of container window to {100, 225}
set position of item "Applications" of container window to {375, 225}
update without registering applications
close
end tell
end tell
Run Code Online (Sandbox Code Playgroud)
上述窗口测量适用于我的项目,特别是由于我的背景图片和图标分辨率的大小;您可能需要为自己的项目修改这些值。
脚本 3(制作最终磁盘映像脚本):
#!/bin/bash
dir="$TEMP_FILES_DIR/disk"
cp "$PROJECT_DIR/$PROJECT_NAME/some_other_image.png" "$dir/"
#unmount the temp image file, then convert it to final image file
sync
sync
hdiutil detach /Volumes/$PRODUCT_NAME
rm -f "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.dmg"
hdiutil convert "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.temp.dmg" -format UDZO -imagekey zlib-level=9 -o "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.dmg"
rm -f "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.temp.dmg"
#Change the icon of the image file
sips -i "$dir/some_other_image.png"
DeRez -only icns "$dir/some_other_image.png" > "$dir/tmpicns.rsrc"
Rez -append "$dir/tmpicns.rsrc" -o "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.dmg"
SetFile -a C "$BUILT_PRODUCTS_DIR/$PRODUCT_NAME.dmg"
rm -rf "$dir"
Run Code Online (Sandbox Code Playgroud)
确保您使用的图像文件位于 $PROJECT_DIR/$PROJECT_NAME/ 目录中!