我需要为Mac应用程序创建一个很好的安装程序。我希望它是一个磁盘映像(DMG),具有预定义的大小、布局和背景图像。

我需要在一个脚本中以编程方式做到这一点,将其集成到现有的构建系统中(实际上更多的是一个包系统,因为它只创建安装程序。构建是单独完成的)。

我已经用“hdiutil”完成了DMG的创建,我还没有发现的是如何制作图标布局和指定背景位图。


当前回答

我使用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"输出

其他回答

我找到了这个很棒的mac应用程序来自动化这个过程- http://www.araelium.com/dmgcanvas/ 如果你正在为你的MAC应用程序创建DMG安装程序,你必须看看

这些答案太复杂了,而且时代已经变了。下面的工作在10.9上很好,权限是正确的,它看起来不错。

从目录中创建只读DMG

#!/bin/sh
# create_dmg Frobulator Frobulator.dmg path/to/frobulator/dir [ 'Your Code Sign Identity' ]
set -e

VOLNAME="$1"
DMG="$2"
SRC_DIR="$3"
CODESIGN_IDENTITY="$4"

hdiutil create -srcfolder "$SRC_DIR" \
  -volname "$VOLNAME" \
  -fs HFS+ -fsargs "-c c=64,a=16,e=16" \
  -format UDZO -imagekey zlib-level=9 "$DMG"

if [ -n "$CODESIGN_IDENTITY" ]; then
  codesign -s "$CODESIGN_IDENTITY" -v "$DMG"
fi

创建带有图标的只读DMG。icn类型)

#!/bin/sh
# create_dmg_with_icon Frobulator Frobulator.dmg path/to/frobulator/dir path/to/someicon.icns [ 'Your Code Sign Identity' ]
set -e
VOLNAME="$1"
DMG="$2"
SRC_DIR="$3"
ICON_FILE="$4"
CODESIGN_IDENTITY="$5"

TMP_DMG="$(mktemp -u -t XXXXXXX)"
trap 'RESULT=$?; rm -f "$TMP_DMG"; exit $RESULT' INT QUIT TERM EXIT
hdiutil create -srcfolder "$SRC_DIR" -volname "$VOLNAME" -fs HFS+ \
               -fsargs "-c c=64,a=16,e=16" -format UDRW "$TMP_DMG"
TMP_DMG="${TMP_DMG}.dmg" # because OSX appends .dmg
DEVICE="$(hdiutil attach -readwrite -noautoopen "$TMP_DMG" | awk 'NR==1{print$1}')"
VOLUME="$(mount | grep "$DEVICE" | sed 's/^[^ ]* on //;s/ ([^)]*)$//')"
# start of DMG changes
cp "$ICON_FILE" "$VOLUME/.VolumeIcon.icns"
SetFile -c icnC "$VOLUME/.VolumeIcon.icns"
SetFile -a C "$VOLUME"
# end of DMG changes
hdiutil detach "$DEVICE"
hdiutil convert "$TMP_DMG" -format UDZO -imagekey zlib-level=9 -o "$DMG"
if [ -n "$CODESIGN_IDENTITY" ]; then
  codesign -s "$CODESIGN_IDENTITY" -v "$DMG"
fi

如果需要执行其他操作,最简单的方法是创建SRC_DIR的临时副本,并在创建DMG之前对其应用更改。

不要去那里。作为一个长期的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(例如自动化的夜间构建)。

对于那些对这个话题感兴趣的人,我应该提到我是如何创建DMG的:

hdiutil create XXX.dmg -volname "YYY" -fs HFS+ -srcfolder "ZZZ"

在哪里

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

如果你想设置自定义卷图标,那么使用下面的命令

/*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