我需要为Mac应用程序创建一个很好的安装程序。我希望它是一个磁盘映像(DMG),具有预定义的大小、布局和背景图像。
我需要在一个脚本中以编程方式做到这一点,将其集成到现有的构建系统中(实际上更多的是一个包系统,因为它只创建安装程序。构建是单独完成的)。
我已经用“hdiutil”完成了DMG的创建,我还没有发现的是如何制作图标布局和指定背景位图。
我需要为Mac应用程序创建一个很好的安装程序。我希望它是一个磁盘映像(DMG),具有预定义的大小、布局和背景图像。
我需要在一个脚本中以编程方式做到这一点,将其集成到现有的构建系统中(实际上更多的是一个包系统,因为它只创建安装程序。构建是单独完成的)。
我已经用“hdiutil”完成了DMG的创建,我还没有发现的是如何制作图标布局和指定背景位图。
当前回答
有一个叫做create-dmg的Bash小脚本,它可以构建具有自定义背景、自定义图标定位和卷名的花哨dmg。
这是我多年前为我当时经营的公司建造的;从那时起,它就依靠其他人的贡献而生存,据报道,它运行良好。
还有node-appdmg,它看起来更现代,更活跃,基于Node.js;也来看看吧。
其他回答
对于那些对这个话题感兴趣的人,我应该提到我是如何创建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
经过大量的研究,我得出了这个答案,我在这里把它作为我自己的问题的答案,供参考:
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: hdiutil create -srcfolder "${source}" -volname "${title}" -fs HFS+ \ -fsargs "-c c=64,a=16,e=16" -format UDRW -size ${size}k pack.temp.dmg Mount the disk image, and store the device name (you might want to use sleep for a few seconds after this operation): device=$(hdiutil attach -readwrite -noverify -noautoopen "pack.temp.dmg" | \ egrep '^/dev/' | sed 1q | awk '{print $1}') 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): echo ' tell application "Finder" tell disk "'${title}'" 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 {400, 100, 885, 430} set theViewOptions to the icon view options of container window set arrangement of theViewOptions to not arranged set icon size of theViewOptions to 72 set background picture of theViewOptions to file ".background:'${backgroundPictureName}'" make new alias file at container window to POSIX file "/Applications" with properties {name:"Applications"} set position of item "'${applicationName}'" of container window to {100, 100} set position of item "Applications" of container window to {375, 100} update without registering applications delay 5 close end tell end tell ' | osascript Finialize the DMG by setting permissions properly, compressing and releasing it: chmod -Rf go-w /Volumes/"${title}" sync sync hdiutil detach ${device} hdiutil convert "/pack.temp.dmg" -format UDZO -imagekey zlib-level=9 -o "${finalDMGName}" rm -f /pack.temp.dmg
在雪豹上,上面的applescript不能正确设置图标的位置-这似乎是一个雪豹bug。一个解决方法是在设置图标后简单地调用close/open,即:
..
set position of item "'${applicationName}'" of container window to {100, 100}
set position of item "Applications" of container window to {375, 100}
close
open
为了创建一个漂亮的DMG,你现在可以使用一些写得很好的开放源代码:
create-dmg node-appdmg dmgbuild
不要去那里。作为一个长期的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(例如自动化的夜间构建)。
有一个叫做create-dmg的Bash小脚本,它可以构建具有自定义背景、自定义图标定位和卷名的花哨dmg。
这是我多年前为我当时经营的公司建造的;从那时起,它就依靠其他人的贡献而生存,据报道,它运行良好。
还有node-appdmg,它看起来更现代,更活跃,基于Node.js;也来看看吧。