如何使用命令行工具为 Mac OS X 创建漂亮的 DMG? [英] How do I create a nice-looking DMG for Mac OS X using command-line tools?

查看:23
本文介绍了如何使用命令行工具为 Mac OS X 创建漂亮的 DMG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要为 Mac 应用程序创建一个不错的安装程序.我希望它是一个磁盘映像 (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).

我已经使用hdiutil"完成了 DMG 创建,我还没有发现如何制作图标布局和指定背景位图.

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.

推荐答案

经过大量研究,我得出了这个答案,我将其放在这里作为我自己问题的答案,以供参考:

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:

  1. 确保在系统偏好设置">>通用访问"中选中启用辅助设备访问".AppleScript 需要它才能工作.您可能需要在此更改后重新启动(否则它在 Mac OS X Server 10.4 上不起作用).

  1. 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).

创建一个 R/W DMG.它必须大于结果.在此示例中,bash 变量size"包含以 Kb 为单位的大小,source"bash 变量中文件夹的内容将被复制到 DMG 中:

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

  • 挂载磁盘映像,并存储设备名称(您可能希望在此操作后使用 sleep 几秒钟):

  • 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}')
    

  • 将背景图片(PNG 格式)存储在 DMG 中名为.background"的文件夹中,并将其名称存储在backgroundPictureName"变量中.

  • Store the background picture (in PNG format) in a folder called ".background" in the DMG, and store its name in the "backgroundPictureName" variable.

    使用 AppleScript 设置视觉样式(.app 的名称必须在 bash 变量applicationName"中,其他属性根据需要使用变量):

    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
    

  • 通过正确设置权限、压缩和释放来完成 DMG:

  • 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 
    

  • 在Snow Leopard 上,上面的applescript 不会正确设置图标位置- 这似乎是Snow Leopard 的错误.一种解决方法是在设置图标后简单地调用 close/open,即:

    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.:

    ..
    set position of item "'${applicationName}'" of container window to {100, 100}
    set position of item "Applications" of container window to {375, 100}
    close
    open
    

    这篇关于如何使用命令行工具为 Mac OS X 创建漂亮的 DMG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆