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

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

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


当前回答

我终于得到了这个工作在我自己的项目(这恰好是在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/目录!

其他回答

不要去那里。作为一个长期的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创建“在脚本中编程”。到目前为止,我找到的最好的答案是Adium项目的发布构建框架(见R1)。有一个自定义脚本(AdiumApplescriptRunner)可以让你避免OSX WindowsServer GUI交互。“osascript applescript。Scpt”方法要求您以构建器身份登录,并从命令行vt100会话运行DMG创建。

OSX的包管理系统相对于其他的Unixen来说还不太先进,可以轻松系统地完成这一任务。

R1: http://hg.adium.im/adium - 1.4 - / - file/00d944a3ef16/release

我刚刚编写了一个新的(友好的)命令行实用程序来完成这项工作。它不依赖于Finder/AppleScript,或任何(已弃用的)别名管理器api,并且易于配置和使用。

无论如何,任何感兴趣的人都可以在PyPi上找到它;文档可以在阅读文档中找到。

对于那些对这个话题感兴趣的人,我应该提到我是如何创建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

有一个叫做create-dmg的Bash小脚本,它可以构建具有自定义背景、自定义图标定位和卷名的花哨dmg。

这是我多年前为我当时经营的公司建造的;从那时起,它就依靠其他人的贡献而生存,据报道,它运行良好。

还有node-appdmg,它看起来更现代,更活跃,基于Node.js;也来看看吧。