如何使用命令行工具为 Mac OS X 创建美观的 DMG?

发布于 2024-07-06 16:05:57 字数 205 浏览 12 评论 0原文

我需要为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

旧街凉风 2024-07-13 16:05:57

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

  1. 确保在系统中选中“启用辅助设备的访问”首选项>>通用访问。 AppleScript 需要它才能工作。 进行此更改后,您可能需要重新启动(否则在 Mac OS X Server 10.4 上不起作用)。

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

    hdiutil create -srcfolder "${source}" -volname "${title}" -fs HFS+ \ 
            -fsargs "-cc=64,a=16,e=16" -format UDRW -size ${size}k pack.temp.dmg 
      
  3. 挂载磁盘映像,并存储设备名称(您执行此操作后可能需要使用睡眠几秒钟):

    device=$(hdiutil Attach -readwrite -noverify -noautoopen "pack.temp.dmg" | \ 
               egrep '^/dev/' | '^/dev/' |   sed 1q |   awk '{print $1}') 
      
  4. 将背景图片(PNG 格式)存储在名为的文件夹中DMG 中的“.background”,并将其名称存储在“backgroundPictureName”变量中。

  5. 使用 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
    关闭
    结束告诉
    结束告诉
    ' | 奥萨脚本

  6. 通过正确设置权限来完成 DMG,压缩并释放它:

    chmod -Rf go-w /Volumes/"${title}" 
      同步 
      同步 
      hdiutil 分离 ${device} 
      hdiutil 转换“/pack.temp.dmg”-format UDZO -imagekey zlib-level=9 -o“${finalDMGName}” 
      rm -f /pack.temp.dmg  
      

在 Snow Leopard 上,上面的 applescript 将无法正确设置图标位置 - 这似乎是 Snow Leopard 的错误。 一种解决方法是在设置图标后简单地调用关闭/打开,即:

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

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

  2. 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
    
  3. 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}')
    
  4. Store the background picture (in PNG format) in a folder called ".background" in the DMG, and store its name in the "backgroundPictureName" variable.

  5. 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
    
  6. 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 
    

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
三生一梦 2024-07-13 16:05:57

有一个名为 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.

比忠 2024-07-13 16:05:57

别去那里。 作为一名长期的 Mac 开发人员,我可以向您保证,没有任何解决方案真正有效。 我尝试了很多解决方案,但都不是很好。 我认为问题在于苹果并没有真正记录必要数据的元数据格式。

以下是我很长一段时间以来非常成功的做法:

  1. 创建一个新的 DMG,可写(!),足够大以容纳预期的二进制文件和额外文件,如自述文件(稀疏可能有效)。

  2. 安装 DMG 并在 Finder 中或使用任何适合您执行此操作的工具手动为其指定布局。 背景图像通常是我们放入 DMG 上的隐藏文件夹(“.something”)中的图像。 将您的应用程序的副本放在那里(任何版本,甚至过时的版本都可以)。 再次复制您想要的其他文件(别名、自述文件等),过时的版本也可以。 确保图标具有正确的大小和位置(IOW,按照您想要的方式布局 DMG)。

  3. 再次卸载 DMG,所有设置现在应该已存储。

  4. 编写一个创建 DMG 脚本,其工作原理如下:

  • 它复制 DMG,因此不再触及原始 DMG。
  • 它安装副本。
  • 它将所有文件替换为最新的文件(例如构建后的最新应用程序)。 您只需在命令行上使用 mvditto 即可。 请注意,当您替换这样的文件时,图标将保持不变,位置将保持不变,除了文件(或目录)内容之外的所有内容都保持不变(至少与我们通常用于该任务的同上) 。 当然,您也可以将背景图像替换为另一张图像(只需确保其尺寸相同)。
  • 替换文件后,使脚本再次卸载 DMG 副本。
  • 最后调用 hdiutil 将可写转换为压缩的(不可写的)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:

  1. Create a new DMG, writeable(!), big enough to hold the expected binary and extra files like readme (sparse might work).

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

  3. Unmount the DMG again, all settings should be stored by now.

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

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

不奢求什么 2024-07-13 16:05:57

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

appdmg 是一个简单、易于使用的开源命令行程序,可根据简单的 json 规范创建 dmg 文件。 看一下官网的readme:

https://github.com/LinusU/node-appdmg

Quick example :

  1. 安装appdmg

    npm install -g appdmg 
      
  2. 编写json文件(spec.json)

    <前><代码>{
    "title": "测试标题",
    "背景": "背景.png",
    “图标大小”:80,
    “内容”: [
    { "x": 192, "y": 344, "类型": "文件", "路径": "TestApp.app" },
    {“x”:448,“y”:344,“类型”:“链接”,“路径”:“/应用程序”}
    ]
    }

  3. 运行程序

    appdmg spec.json test.dmg 
      

(免责声明。我是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:

  1. Install appdmg

    npm install -g appdmg
    
  2. Write a json file (spec.json)

    {
      "title": "Test Title",
      "background": "background.png",
      "icon-size": 80,
      "contents": [
        { "x": 192, "y": 344, "type": "file", "path": "TestApp.app" },
        { "x": 448, "y": 344, "type": "link", "path": "/Applications" }
      ]
    }
    
  3. Run program

    appdmg spec.json test.dmg
    

(disclaimer. I'm the creator of appdmg)

上课铃就是安魂曲 2024-07-13 16:05:57

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

For those of you that are interested in this topic, I should mention how I create the DMG:

hdiutil create XXX.dmg -volname "YYY" -fs HFS+ -srcfolder "ZZZ"

where

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
清引 2024-07-13 16:05:57

我的应用程序 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.

時窥 2024-07-13 16:05:57

为了创建一个漂亮的 DMG,您现在可以使用一些编写良好的开源代码:

For creating a nice looking DMG, you can now just use some well written open sources:

叹梦 2024-07-13 16:05:57

我发现这个很棒的 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

小鸟爱天空丶 2024-07-13 16:05:57

如果您想设置自定义音量图标,请使用以下命令

/*Add a drive icon*/
cp "/Volumes/customIcon.icns" "/Volumes/dmgName/.VolumeIcon.icns"  


/*SetFile -c icnC will change the creator of the file to icnC*/
SetFile -c icnC /<your path>/.VolumeIcon.icns

现在创建读/写 dmg

/*to set custom icon attribute*/
SetFile -a C /Volumes/dmgName

If you want to set custom volume icon then use below command

/*Add a drive icon*/
cp "/Volumes/customIcon.icns" "/Volumes/dmgName/.VolumeIcon.icns"  


/*SetFile -c icnC will change the creator of the file to icnC*/
SetFile -c icnC /<your path>/.VolumeIcon.icns

Now create read/write dmg

/*to set custom icon attribute*/
SetFile -a C /Volumes/dmgName
乖乖 2024-07-13 16:05:57

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

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

#!/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

Script 2 (Set Window Properties Script):

#!/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

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

#!/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"

Make sure the image files you are using are in the $PROJECT_DIR/$PROJECT_NAME/ directory!

素染倾城色 2024-07-13 16:05:57

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

波浪屿的海角声 2024-07-13 16:05:57

我还需要使用命令行方法“以脚本方式”进行打包和 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

歌枕肩 2024-07-13 16:05:57

我刚刚编写了一个新的(友好的)命令行实用程序来执行此操作。 它不依赖于 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.

一抹微笑 2024-07-13 16:05:57

我使用了 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" }
    ]
}
  • 宽度值是背景的宽度。

  • 窗口栏的高度值应为背景高度 + 20。

  • 在终端中:dmgbuild -s settings.json "YOUR_VOLUME_NAME" output.dmg

I used dmgbuild.

  • Installation: pip3 install dmgbuild
  • Mount your volume
  • Create a settings file:
{
    "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" }
    ]
}
  • 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

德意的啸 2024-07-13 16:05:57

这些答案太复杂了,而且时代已经变了。 以下内容在 10.9 上运行得很好,权限正确并且看起来不错。

从目录创建只读 DMG

#!/bin/sh
# create_dmg Frobulator Frobulator.dmg path/to/frobulator/dir [ 'Your Code Sign Identity' ]
set -e

VOLNAME="$1"
DMG="$2"
SRC_DIR="$3"
CODESIGN_IDENTITY="$4"

hdiutil create -srcfolder "$SRC_DIR" \
  -volname "$VOLNAME" \
  -fs HFS+ -fsargs "-c c=64,a=16,e=16" \
  -format UDZO -imagekey zlib-level=9 "$DMG"

if [ -n "$CODESIGN_IDENTITY" ]; then
  codesign -s "$CODESIGN_IDENTITY" -v "$DMG"
fi

使用图标(.icns 类型)创建只读 DMG

#!/bin/sh
# create_dmg_with_icon Frobulator Frobulator.dmg path/to/frobulator/dir path/to/someicon.icns [ 'Your Code Sign Identity' ]
set -e
VOLNAME="$1"
DMG="$2"
SRC_DIR="$3"
ICON_FILE="$4"
CODESIGN_IDENTITY="$5"

TMP_DMG="$(mktemp -u -t XXXXXXX)"
trap 'RESULT=$?; rm -f "$TMP_DMG"; exit $RESULT' INT QUIT TERM EXIT
hdiutil create -srcfolder "$SRC_DIR" -volname "$VOLNAME" -fs HFS+ \
               -fsargs "-c c=64,a=16,e=16" -format UDRW "$TMP_DMG"
TMP_DMG="${TMP_DMG}.dmg" # because OSX appends .dmg
DEVICE="$(hdiutil attach -readwrite -noautoopen "$TMP_DMG" | awk 'NR==1{print$1}')"
VOLUME="$(mount | grep "$DEVICE" | sed 's/^[^ ]* on //;s/ ([^)]*)$//')"
# start of DMG changes
cp "$ICON_FILE" "$VOLUME/.VolumeIcon.icns"
SetFile -c icnC "$VOLUME/.VolumeIcon.icns"
SetFile -a C "$VOLUME"
# end of DMG changes
hdiutil detach "$DEVICE"
hdiutil convert "$TMP_DMG" -format UDZO -imagekey zlib-level=9 -o "$DMG"
if [ -n "$CODESIGN_IDENTITY" ]; then
  codesign -s "$CODESIGN_IDENTITY" -v "$DMG"
fi

如果需要发生其他情况,最简单的方法是创建 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

#!/bin/sh
# create_dmg Frobulator Frobulator.dmg path/to/frobulator/dir [ 'Your Code Sign Identity' ]
set -e

VOLNAME="$1"
DMG="$2"
SRC_DIR="$3"
CODESIGN_IDENTITY="$4"

hdiutil create -srcfolder "$SRC_DIR" \
  -volname "$VOLNAME" \
  -fs HFS+ -fsargs "-c c=64,a=16,e=16" \
  -format UDZO -imagekey zlib-level=9 "$DMG"

if [ -n "$CODESIGN_IDENTITY" ]; then
  codesign -s "$CODESIGN_IDENTITY" -v "$DMG"
fi

Create read-only DMG with an icon (.icns type)

#!/bin/sh
# create_dmg_with_icon Frobulator Frobulator.dmg path/to/frobulator/dir path/to/someicon.icns [ 'Your Code Sign Identity' ]
set -e
VOLNAME="$1"
DMG="$2"
SRC_DIR="$3"
ICON_FILE="$4"
CODESIGN_IDENTITY="$5"

TMP_DMG="$(mktemp -u -t XXXXXXX)"
trap 'RESULT=$?; rm -f "$TMP_DMG"; exit $RESULT' INT QUIT TERM EXIT
hdiutil create -srcfolder "$SRC_DIR" -volname "$VOLNAME" -fs HFS+ \
               -fsargs "-c c=64,a=16,e=16" -format UDRW "$TMP_DMG"
TMP_DMG="${TMP_DMG}.dmg" # because OSX appends .dmg
DEVICE="$(hdiutil attach -readwrite -noautoopen "$TMP_DMG" | awk 'NR==1{print$1}')"
VOLUME="$(mount | grep "$DEVICE" | sed 's/^[^ ]* on //;s/ ([^)]*)$//')"
# start of DMG changes
cp "$ICON_FILE" "$VOLUME/.VolumeIcon.icns"
SetFile -c icnC "$VOLUME/.VolumeIcon.icns"
SetFile -a C "$VOLUME"
# end of DMG changes
hdiutil detach "$DEVICE"
hdiutil convert "$TMP_DMG" -format UDZO -imagekey zlib-level=9 -o "$DMG"
if [ -n "$CODESIGN_IDENTITY" ]; then
  codesign -s "$CODESIGN_IDENTITY" -v "$DMG"
fi

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文