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

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

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


当前回答

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

其他回答

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

我的应用,DropDMG,是一个简单的方法来创建磁盘图像的背景图片,图标布局,自定义卷图标,和软件许可协议。它可以通过“dropdmg”命令行工具或AppleScript从构建系统中控制。如果需要,图片和许可证RTF文件可以存储在您的版本控制系统下。

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

通过提供这个答案来更新这个问题。

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的创建者)

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