We have a build process that is now doing the common layout for distributed DMGs. This is done in a way that I've seen in many places -- the DMG is attached, then Finder does its job via osascript. However, the script must refer to the mounted image by its volume name, and AFAICT, these are not unique. Since the machine that is doing this work is not dedicated for that, I worry that it might already have a mounted image by the same name (likely, since the person who works with this machine is a client of the generated DMGs too).

Is there some way to refer to a disk uniquely in the osascript if, for example, I know its device or its mount point? Or maybe there's some other way to resolve this, like attaching an image with a different volname so I can pass some randomly generated name?

(I'm surprised that I didn't see any mention of this on the web, so maybe I'm missing something more obvious.)

  • You want to refer to the <image name>.dmg or /Volumes/<volume name>?
    – duci9y
    Jan 13, 2013 at 18:14
  • @duci9y The script has tell disk <volname> and that name might not be unique. Jan 13, 2013 at 18:17

1 Answer 1


After a lot of searching and much cursing, I think that this is really a problem that practically nobody is aware of or concerned about. Still, I was able to find a solution, it's not great, but it should work. The idea is to mount the DMG in a known directory with some random name -- this name (the last element in the -mountpoint path) will be the name property of the disk. So using this I walk over all of the disks and look for ones with a matching name -- complain if I find none or more than one, otherwise, I have the needed object ready to work with.

hdiutil attach -readwrite -noverify -noautoopen \
               -mountpoint "/tmp/$mnt" "/tmp/out.dmg"
/usr/bin/osascript <<-EOF
      tell application "Finder"
        set myDisks to every disk of desktop
        set theDMGDisk to ""
        repeat with d in myDisks
          if name of d = "$mnt"
            if theDMGDisk = ""
              set theDMGDisk to d
              error "Too many attached DMGs found!"
            end if
          end if
        end repeat
        if theDMGDisk = "" then error "Attached DMG not found!"
        -- found a single matching disk, continue
        tell theDMGDisk
... etc ...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .