从安装点获取.dmg的路径 [英] Get the path of the .dmg from the mount point
问题描述
我正在寻找一种获取带有已挂载点的已挂载磁盘映像的.dmg路径的方法.
I'm looking for a way to get the .dmg path of a mounted disk image with just its mount point.
我想编写一个简单的" Finder服务,该服务弹出磁盘映像并丢弃随附的.dmg.弹出是无关紧要的,但是在给定安装点的情况下,如何确定.dmg的路径我很茫然.
I want to write a "simple" Finder service that ejects the disk image and trashes the accompanying .dmg. The ejecting is trivial, but I'm at a loss as to how to figure out the path of the .dmg, given just the mount point.
diskutil 似乎不知道或没有说.
diskutil doesn't seem to know or isn't saying.
它是用于脚本的,因此首选基于AppleScript或基于shell的建议.
It's for a script, so AppleScript- or shell-based suggestions are preferred.
推荐答案
使用hdiutil info
获取有关当前安装的映像的信息.然后
使用hdiutil detach /Mount/Point
卸下所有文件系统,并卸下映像.
Use hdiutil info
to get the information about currently mounted images. Then
use hdiutil detach /Mount/Point
to dismount all file systems, and detach the image.
如果安装了多个图像,则需要解析hdiutil info
的输出以找到正确的图像路径.使用plist输出格式hdiutil info -plist
并将其运行到具有
You'll need to parse the output from hdiutil info
to find the right image-path if multiple images are mounted. It will probably be more robust to use the plist output format hdiutil info -plist
and run that into, say, a python script with plistlib or an AppleScript using the Property List Suite
from System Events
.
这是一个快速而肮脏的python脚本,可为您提供一个主意.使用python解释器很容易探索选项:
Here's a quick and dirty python script to give you an idea. It's easy to explore options using the python interpreter:
>>> import plistlib
>>> from subprocess import Popen, PIPE
>>> output = Popen(["hdiutil", "info", "-plist"], stdout=PIPE).communicate()[0]
>>> pl = plistlib.readPlistFromString(output)
>>> for image in pl['images']:
... for se in image['system-entities']:
... if se.get('mount-point') == '/Volumes/blah':
... print image['image-path']
/Path/To/blah.dmg
这篇关于从安装点获取.dmg的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!