Get the path of the .dmg from the mount point

有些话、适合烂在心里 提交于 2019-12-18 12:58:14

问题


I'm looking for a way to get the .dmg path of a mounted disk image with just its mount point.

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 doesn't seem to know or isn't saying.

It's for a script, so AppleScript- or shell-based suggestions are preferred.


回答1:


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.

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.

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



回答2:


Start Terminal, do:

$ cd /Volumes
$ hdutil info

The location of suspected dmg-files will show up under image-path

cd to that location, and do:

$ ls filename

Unmount volume in Finder, and finally in Terminal:

$ rm filename

Good luck.



来源:https://stackoverflow.com/questions/1949879/get-the-path-of-the-dmg-from-the-mount-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!