如何使用命令行工具为 Mac OS X 创建美观的 DMG?
我需要为 Mac 应用程序创建一个漂亮的安装程序。 我希望它是一个磁盘映像 (DMG),具有预定义的大小、布局和背景图像。
我需要在脚本中以编程方式执行此操作,以便集成到现有的构建系统中(实际上更像是一个包系统,因为它只创建安装程序。构建是单独完成的)。
我已经使用“hdiutil”完成了 DMG 创建,但我还没有找到如何制作图标布局并指定背景位图。
I need to create a nice installer for a Mac application. I want it to be a disk image (DMG), with a predefined size, layout and background image.
I need to do this programmatically in a script, to be integrated in an existing build system (more of a pack system really, since it only create installers. The builds are done separately).
I already have the DMG creation done using "hdiutil", what I haven't found out yet is how to make an icon layout and specify a background bitmap.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
经过大量研究,我得出了这个答案,特此将其放在这里作为我自己问题的答案,以供参考:
确保在系统中选中“启用辅助设备的访问”首选项>>通用访问。 AppleScript 需要它才能工作。 进行此更改后,您可能需要重新启动(否则在 Mac OS X Server 10.4 上不起作用)。
创建一个读/写 DMG。 它必须大于结果。 在此示例中,bash 变量“size”包含以 Kb 为单位的大小,“source”bash 变量中的文件夹内容将被复制到 DMG 中:
挂载磁盘映像,并存储设备名称(您执行此操作后可能需要使用睡眠几秒钟):
将背景图片(PNG 格式)存储在名为的文件夹中DMG 中的“.background”,并将其名称存储在“backgroundPictureName”变量中。
使用 AppleScript 设置视觉样式(.app 的名称必须位于 bash 变量“applicationName”中,根据需要使用其他属性的变量):
<前><代码>回显'
告诉应用程序“Finder”
告诉磁盘“'${title}'”
打开
将容器窗口的当前视图设置为图标视图
将容器窗口的工具栏可见设置为 false
将容器窗口的状态栏可见设置为 false
将容器窗口的边界设置为 {400, 100, 885, 430}
将ViewOptions设置为容器窗口的图标视图选项
将 ViewOptions 的排列设置为未排列
将 ViewOptions 的图标大小设置为 72
将ViewOptions的背景图片设置为文件“.background:'${backgroundPictureName}'”
在容器窗口中将新的别名文件创建为具有属性 {name:"Applications"} 的 POSIX 文件“/Applications”
将容器窗口的项目“'${applicationName}'”的位置设置为 {100, 100}
将容器窗口的“应用程序”项的位置设置为 {375, 100}
无需注册应用程序即可更新
延迟5
关闭
结束告诉
结束告诉
' | 奥萨脚本
通过正确设置权限来完成 DMG,压缩并释放它:
在 Snow Leopard 上,上面的 applescript 将无法正确设置图标位置 - 这似乎是 Snow Leopard 的错误。 一种解决方法是在设置图标后简单地调用关闭/打开,即:
After lots of research, I've come up with this answer, and I'm hereby putting it here as an answer for my own question, for reference:
Make sure that "Enable access for assistive devices" is checked in System Preferences>>Universal Access. It is required for the AppleScript to work. You may have to reboot after this change (it doesn't work otherwise on Mac OS X Server 10.4).
Create a R/W DMG. It must be larger than the result will be. In this example, the bash variable "size" contains the size in Kb and the contents of the folder in the "source" bash variable will be copied into the DMG:
Mount the disk image, and store the device name (you might want to use sleep for a few seconds after this operation):
Store the background picture (in PNG format) in a folder called ".background" in the DMG, and store its name in the "backgroundPictureName" variable.
Use AppleScript to set the visual styles (name of .app must be in bash variable "applicationName", use variables for the other properties as needed):
Finialize the DMG by setting permissions properly, compressing and releasing it:
On Snow Leopard, the above applescript will not set the icon position correctly - it seems to be a Snow Leopard bug. One workaround is to simply call close/open after setting the icons, i.e.:
有一个名为 create-dmg 的小 Bash 脚本,它可以构建具有自定义背景、自定义图标定位和卷名称。
很多年前,我为当时经营的公司建造了它; 从那时起,它就依靠其他人的贡献得以生存,并且据说运行良好。
还有 node-appdmg,它看起来像是基于 Node.js 的更现代、更积极的工作; 也检查一下。
There's a little Bash script called create-dmg that builds fancy DMGs with custom backgrounds, custom icon positioning and volume name.
I've built it many years ago for the company that I ran at the time; it survives on other people's contribution since then, and reportedly works well.
There's also node-appdmg which looks like a more modern and active effort based on Node.js; check it out as well.
别去那里。 作为一名长期的 Mac 开发人员,我可以向您保证,没有任何解决方案真正有效。 我尝试了很多解决方案,但都不是很好。 我认为问题在于苹果并没有真正记录必要数据的元数据格式。
以下是我很长一段时间以来非常成功的做法:
创建一个新的 DMG,可写(!),足够大以容纳预期的二进制文件和额外文件,如自述文件(稀疏可能有效)。
安装 DMG 并在 Finder 中或使用任何适合您执行此操作的工具手动为其指定布局。 背景图像通常是我们放入 DMG 上的隐藏文件夹(“.something”)中的图像。 将您的应用程序的副本放在那里(任何版本,甚至过时的版本都可以)。 再次复制您想要的其他文件(别名、自述文件等),过时的版本也可以。 确保图标具有正确的大小和位置(IOW,按照您想要的方式布局 DMG)。
再次卸载 DMG,所有设置现在应该已存储。
编写一个创建 DMG 脚本,其工作原理如下:
这种方法听起来可能不是最佳的,但相信我,它在实践中非常有效。 您甚至可以将原始 DMG(DMG 模板)置于版本控制(例如 SVN)之下,因此,如果您不小心更改/破坏了它,您可以返回到仍然可以的修订版。 您可以将 DMG 模板以及属于 DMG 的所有其他文件(自述文件、URL 文件、背景图像)添加到您的 Xcode 项目中,所有这些文件都在版本控制下,然后创建一个目标(例如名为“Create DMG”的外部目标)然后运行上面的 DMG 脚本并将旧的主目标添加为依赖目标。 您可以使用脚本中的 ${SRCROOT} 访问 Xcode 树中的文件(始终是产品的源根目录),并且可以使用 ${BUILT_PRODUCTS_DIR} 访问构建产品(始终是 Xcode 创建构建结果的目录) 。
结果:实际上Xcode可以在构建结束时生成DMG。 准备发布的 DMG。 您不仅可以通过这种方式非常轻松地创建发布 DMG,实际上还可以在自动化过程中(如果您愿意的话,在无头服务器上)使用 xcodebuild 从命令行执行此操作(例如,自动夜间构建)。
Don't go there. As a long term Mac developer, I can assure you, no solution is really working well. I tried so many solutions, but they are all not too good. I think the problem is that Apple does not really document the meta data format for the necessary data.
Here's how I'm doing it for a long time, very successfully:
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:
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).
Result: Actually Xcode can produce the DMG at the end of the build. A DMG that is ready to release. Not only you can create a release DMG pretty easy that way, you can actually do so in an automated process (on a headless server if you like), using xcodebuild from command line (automated nightly builds for example).
通过提供此答案来更新此问题。
appdmg
是一个简单、易于使用的开源命令行程序,可根据简单的 json 规范创建 dmg 文件。 看一下官网的readme:https://github.com/LinusU/node-appdmg
Quick example :
安装appdmg
编写json文件(
spec.json
)<前><代码>{
"title": "测试标题",
"背景": "背景.png",
“图标大小”:80,
“内容”: [
{ "x": 192, "y": 344, "类型": "文件", "路径": "TestApp.app" },
{“x”:448,“y”:344,“类型”:“链接”,“路径”:“/应用程序”}
]
}
运行程序
(免责声明。我是appdmg的创建者)
Bringing this question up to date by providing this answer.
appdmg
is a simple, easy-to-use, open-source command line program that creates dmg-files from a simple json specification. Take a look at the readme at the official website:https://github.com/LinusU/node-appdmg
Quick example:
Install appdmg
Write a json file (
spec.json
)Run program
(disclaimer. I'm the creator of appdmg)
对于那些对此主题感兴趣的人,我应该提到我如何创建 DMG
:
For those of you that are interested in this topic, I should mention how I create the DMG:
where
我的应用程序 DropDMG 是创建带有背景图片、图标布局、自定义卷的磁盘映像的简单方法图标和软件许可协议。 它可以通过“dropdmg”命令行工具或 AppleScript 从构建系统进行控制。 如果需要,图片和许可证 RTF 文件可以存储在您的版本控制系统下。
My app, DropDMG, is an easy way to create disk images with background pictures, icon layouts, custom volume icons, and software license agreements. It can be controlled from a build system via the "dropdmg" command-line tool or AppleScript. If desired, the picture and license RTF files can be stored under your version control system.
为了创建一个漂亮的 DMG,您现在可以使用一些编写良好的开源代码:
For creating a nice looking DMG, you can now just use some well written open sources:
我发现这个很棒的 mac 应用程序可以自动化该过程 - http://www.araelium.com/dmgcanvas/
如果您正在为您的 mac 应用程序创建 dmg 安装程序,您必须看看
I found this great mac app to automate the process - http://www.araelium.com/dmgcanvas/
you must have a look if you are creating dmg installer for your mac app
如果您想设置自定义音量图标,请使用以下命令
现在创建读/写 dmg
If you want to set custom volume icon then use below command
Now create read/write dmg
我终于在我自己的项目中得到了这个工作(恰好在 Xcode 中)。 将这 3 个脚本添加到您的构建阶段将自动为您的产品创建一个漂亮整洁的磁盘映像。 您所要做的就是构建您的项目,DMG 将在您的产品文件夹中等待。
脚本 1(创建临时磁盘映像):
脚本 2(设置窗口属性脚本):
由于我的背景图片和图标分辨率的大小,上述窗口测量适用于我的项目; 您可能需要为自己的项目修改这些值。
脚本 3(制作最终磁盘映像脚本):
确保您使用的映像文件位于 $PROJECT_DIR/$PROJECT_NAME/ 目录中!
I finally got this working in my own project (which happens to be in Xcode). Adding these 3 scripts to your build phase will automatically create a Disk Image for your product that is nice and neat. All you have to do is build your project and the DMG will be waiting in your products folder.
Script 1 (Create Temp Disk Image):
Script 2 (Set Window Properties Script):
The above measurement for the window work for my project specifically due to the size of my background pic and icon resolution; you may need to modify these values for your own project.
Script 3 (Make Final Disk Image Script):
Make sure the image files you are using are in the $PROJECT_DIR/$PROJECT_NAME/ directory!
.DS_Store 文件存储 Mac 中的 Windows 设置。 窗口设置包括图标布局、窗口背景、窗口大小等。在创建挂载图像的窗口时需要使用.DS_Store 文件来保留文件的排列和窗口背景。
创建 .DS_Store 文件后,您只需将其复制到您创建的安装程序 (DMG) 中即可。
.DS_Store files stores windows settings in Mac. Windows settings include the icons layout, the window background, the size of the window, etc. The .DS_Store file is needed in creating the window for mounted images to preserve the arrangement of files and the windows background.
Once you have .DS_Store file created, you can just copy it to your created installer (DMG).
我还需要使用命令行方法“以脚本方式”进行打包和 dmg 创建。 到目前为止,我找到的最佳答案来自 Adium 项目的发布构建框架(参见 R1)。 有一个自定义脚本(AdiumApplescriptRunner)可以让您避免 OSX WindowsServer GUI 交互。 “osascript applescript.scpt”方法要求您以构建者身份登录并从命令行 vt100 会话运行 dmg 创建。
与其他可以轻松系统地完成此任务的 Unixen 相比,OSX 包管理系统并不那么先进。
R1: http://hg.adium.im/adium-1.4/file/00d944a3ef16 /发布
I also in need of using command line approach to do the packaging and dmg creation "programmatically in a script". The best answer I found so far is from Adium project' Release building framework (See R1). There is a custom script(AdiumApplescriptRunner) to allow you avoid OSX WindowsServer GUI interaction. "osascript applescript.scpt" approach require you to login as builder and run the dmg creation from a command line vt100 session.
OSX package management system is not so advanced compared to other Unixen which can do this task easily and systematically.
R1: http://hg.adium.im/adium-1.4/file/00d944a3ef16/Release
我刚刚编写了一个新的(友好的)命令行实用程序来执行此操作。 它不依赖于 Finder/AppleScript,也不依赖于任何(已弃用的)Alias Manager API,并且易于配置和使用。
不管怎样,任何有兴趣的人都可以在 PyPi 上找到它; 该文档可通过阅读文档获取。
I've just written a new (friendly) command line utility to do this. It doesn’t rely on Finder/AppleScript, or on any of the (deprecated) Alias Manager APIs, and it’s easy to configure and use.
Anyway, anyone who is interested can find it on PyPi; the documentation is available on Read The Docs.
我使用了 dmgbuild。
pip3 install dmgbuild
宽度值是背景的宽度。
窗口栏的高度值应为背景高度 + 20。
在终端中:
dmgbuild -s settings.json "YOUR_VOLUME_NAME" output.dmg
I used dmgbuild.
pip3 install dmgbuild
The width value is the width of the background.
The height value should be the background height + 20 for the window bar.
In a terminal:
dmgbuild -s settings.json "YOUR_VOLUME_NAME" output.dmg
这些答案太复杂了,而且时代已经变了。 以下内容在 10.9 上运行得很好,权限正确并且看起来不错。
从目录创建只读 DMG
使用图标(.icns 类型)创建只读 DMG
如果需要发生其他情况,最简单的方法是创建 SRC_DIR 的临时副本并在创建 DMG 之前对其应用更改。
These answers are way too complicated and times have changed. The following works on 10.9 just fine, permissions are correct and it looks nice.
Create a read-only DMG from a directory
Create read-only DMG with an icon (.icns type)
If anything else needs to happen, these easiest thing is to make a temporary copy of the SRC_DIR and apply changes to that before creating a DMG.