我需要为Mac应用程序创建一个很好的安装程序。我希望它是一个磁盘映像(DMG),具有预定义的大小、布局和背景图像。
我需要在一个脚本中以编程方式做到这一点,将其集成到现有的构建系统中(实际上更多的是一个包系统,因为它只创建安装程序。构建是单独完成的)。
我已经用“hdiutil”完成了DMG的创建,我还没有发现的是如何制作图标布局和指定背景位图。
我需要为Mac应用程序创建一个很好的安装程序。我希望它是一个磁盘映像(DMG),具有预定义的大小、布局和背景图像。
我需要在一个脚本中以编程方式做到这一点,将其集成到现有的构建系统中(实际上更多的是一个包系统,因为它只创建安装程序。构建是单独完成的)。
我已经用“hdiutil”完成了DMG的创建,我还没有发现的是如何制作图标布局和指定背景位图。
当前回答
如果你想设置自定义卷图标,那么使用下面的命令
/*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
现在创建读/写dmg
/*to set custom icon attribute*/
SetFile -a C /Volumes/dmgName
其他回答
如果你想设置自定义卷图标,那么使用下面的命令
/*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
现在创建读/写dmg
/*to set custom icon attribute*/
SetFile -a C /Volumes/dmgName
不要去那里。作为一个长期的Mac开发人员,我可以向你保证,没有什么解决方案是真正有效的。我试了很多办法,但都不太好。我认为问题在于苹果并没有真正记录必要数据的元数据格式。
以下是我长期以来非常成功地做这件事的方法:
Create a new DMG, writeable(!), big enough to hold the expected binary and extra files like readme (sparse might work). Mount the DMG and give it a layout manually in Finder or with whatever tools suits you for doing that. The background image is usually an image we put into a hidden folder (".something") on the DMG. Put a copy of your app there (any version, even outdated one will do). Copy other files (aliases, readme, etc.) you want there, again, outdated versions will do just fine. Make sure icons have the right sizes and positions (IOW, layout the DMG the way you want it to be). Unmount the DMG again, all settings should be stored by now. Write a create DMG script, that works as follows:
It copies the DMG, so the original one is never touched again. It mounts the copy. It replaces all files with the most up to date ones (e.g. latest app after build). You can simply use mv or ditto for that on command line. Note, when you replace a file like that, the icon will stay the same, the position will stay the same, everything but the file (or directory) content stays the same (at least with ditto, which we usually use for that task). You can of course also replace the background image with another one (just make sure it has the same dimensions). After replacing the files, make the script unmount the DMG copy again. Finally call hdiutil to convert the writable, to a compressed (and such not writable) DMG.
This method may not sound optimal, but trust me, it works really well in practice. You can put the original DMG (DMG template) even under version control (e.g. SVN), so if you ever accidentally change/destroy it, you can just go back to a revision where it was still okay. You can add the DMG template to your Xcode project, together with all other files that belong onto the DMG (readme, URL file, background image), all under version control and then create a target (e.g. external target named "Create DMG") and there run the DMG script of above and add your old main target as dependent target. You can access files in the Xcode tree using ${SRCROOT} in the script (is always the source root of your product) and you can access build products by using ${BUILT_PRODUCTS_DIR} (is always the directory where Xcode creates the build results).
结果:实际上Xcode可以在构建结束时生成DMG。准备释放的DMG。这样你不仅可以很容易地创建一个发布版DMG,实际上你可以在一个自动化的过程中(如果你喜欢的话,在一个无头服务器上),从命令行使用xcodebuild(例如自动化的夜间构建)。
我终于得到了这个工作在我自己的项目(这恰好是在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
脚本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
上述测量的窗口工作为我的项目,特别是由于我的背景图片和图标分辨率的大小;您可能需要为自己的项目修改这些值。
脚本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"
确保您正在使用的图像文件在$PROJECT_DIR/$PROJECT_NAME/目录!
通过提供这个答案来更新这个问题。
Appdmg是一个简单、易用的开源命令行程序,可以根据简单的json规范创建dmg文件。看看官方网站的自述:
https://github.com/LinusU/node-appdmg
简单的例子:
安装appdmg 安装-g appdmg 编写一个json文件(spec.json) { "title": "测试标题", “背景”:“background.png”, “icon-size”:80年, “内容”:[ {“x”:192年,“y”:344年,“类型”:“文件”、“路径”:“TestApp。应用"}, {“x”:448年,“y”:344年,“类型”:“链接”,“路径”:“/应用程序”} ] } 运行程序 Appdmg spec.json test.dmg
(免责声明。我是appdmg的创建者)
我使用dmgbuild。
安装:pip3 install dmgbuild 挂载您的卷 创建一个设置文件:
{
"title": "NAME",
"background": "YOUR_BACKGROUND.png",
"format": "UDZO",
"compression-level": 9,
"window": { "position": { "x": 100, "y": 100 },
"size": { "width": 640, "height": 300 } },
"contents": [
{ "x": 140, "y": 165, "type": "file", "path": "/Volumes/YOUR_VOLUME_NAME/YOUR_APP.app" },
{ "x": 480, "y": 165, "type": "link", "path": "/Applications" }
]
}
width值是背景的宽度。 高度值应该是窗口栏的背景高度+ 20。 在终端中:dmgbuild -s settings。. json "YOUR_VOLUME_NAME"输出