15

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.

wbg
  • 1,104
  • 2
  • 8
  • 18
  • I don't think you're going to get an AppleScript or shell based suggestion because you're probably going to have to talk to the driver, or at least its user agent, for that. – Azeem.Butt Dec 22 '09 at 23:43
  • I thought there might be something like hdiutil or diskutil that could help, or perhaps a Finder property on mounted disk images. – wbg Dec 22 '09 at 23:53

2 Answers2

18

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
Ned Deily
  • 83,389
  • 16
  • 128
  • 151
  • That won't help me trash the .dmg, though. – wbg Dec 22 '09 at 23:51
  • Brilliant! hdiutil info is just what I'm looking for! – wbg Dec 23 '09 at 01:32
  • @wbg @NedDeily I'm reading this in 2019 and cannot get the suggested solution to work. Has the `hdiutil` program changed? When I run it I only get information about the framework and driver versions. – Neil Bartlett Feb 19 '19 at 10:05
  • @NeilBartlett Do you have any disk images mounted? The framework/driver info is normal. There's an additional `images` array when there are images mounted. – wbg Feb 26 '19 at 21:26
  • @wbg Yes, I have two images mounted. – Neil Bartlett Feb 28 '19 at 07:45
4

Start Terminal, do:

$ cd /Volumes
$ hdiutil 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.

James Bedford
  • 28,702
  • 8
  • 57
  • 64
janbjorn
  • 41
  • 1