Benches macOS

8

*.dmg files and Applescript

apple Verified

Imported from the Apple Support Forums via the Wayback Machine 🔗

hi,

I'm looking for a better solution to mount or unmont disk image (file with .dmg extension).

The only way I found it's using : "do shel script" with the "hdiutil" command.

Is there a better solution ? what about password on dmg ?

Thanks

i.e. to unmount a disk :

-------------------

-CODE

tell application "Finder"

if exists "mydmgdisk" then

do shell script "hdiutil detach "/Volumes/mydmgdisk""

end if

end tell

- END CODE

8 replies

Sign in or join Beachball Help to reply.

  1. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Actually, there's more to consider:

    If the disk image is on the desktop, either approach in my earlier post will get it off the desktop, but in different ways.

    If "eject" is used, then the disk identifier associated with the disk image is lost, as there is no longer a disk image (mounted or unmounted). In this case, to get the disk image again, you will have to open the related .dmg file.

    But if "hdiutil unmount" is used the disk image is simply unmounted and retains a disk identifier -- and you should be able to remount it with appropriate code.

    I think detach and attach are synonymous with unmount and mount, but check man hdiutil.

    The disk identifiers can be found in do shell script "hdiutil info". You can "coerce" the disk image name to a disk identifier with a bit of code, and that allows you to repeatedly mount and unmount the disk image with:

    tell app "Terminal" to do script "hdiutil (un)mount diskXXX"

    But once you "eject" the disk image, there will be no disk identifier.

  2. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    ANOTHER TRY AT THE FORMATTING PROBLEMS

    I've had a moment to summarize how all this actually works:

    If you don't want to lose the disk image, use "hdiutil unmount diskxxx" in the terminal script.

    To mount it again, use "hdiutil mountvol diskxxx" (trying to mount by opening the .dmg file won't work because the disk already exists -- but is unmounted). Note the change in the command to include "vol".

    If you are using the disk identifier, "diskutil (un)mount diskxxx" also works.

    If you DO want to lose the disk image, use "hdiutil detach (or eject) diskxxx"

    (or you can eject in the Finder using the disk image name). In either case, you will have to open the .dmg file to get it back. To do that use:

    "hdiutil attach path to .dmg file"

    NOTE: all of these work with "do shell script" but any command that involves unmount, detach, or eject has a long delay when you use the "do shell script" approach.

  3. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Is the long time it takes the problem? This is an inexplicable delay when doing this command (and some others) via "do shell script."

    Here are two other approaches. The first one, however, will cause a Terminal window to open (you could add code to close it after the unmount).

    #1

    tell application "Finder"

    if exists "mydmgdisk" then

    tell application "Terminal" to do script "hdiutil detach "/Volumes/mydmgdisk""

    end if

    end tell

    #2

    tell application "Finder"

    if exists "mydmgdisk" then

    eject folder "/Volumes/mydmgdisk""

    end if

    end tell

    If you want to mount the disk image again, say with hdutil attach, then I think you're going to have to use the identifier for the .dmg file, as "mydmgdisk" will no longer be on the desktop.

    I don't know what will happen with the password -- if you find out, please post some information on that.

  4. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Actually, there's more to consider:

    If the disk image is on the desktop, either approach in my earlier post will get it off the desktop, but in different ways.

    If "eject" is used, then the disk identifier associated with the disk image is lost, as there is no longer a disk image (mounted or unmounted). In this case, to get the disk image again, you will have to open the related .dmg file.

    But if "hdiutil unmount" is used the disk image is simply unmounted and retains a disk identifier -- and you should be able to remount it with appropriate code.

    I think detach and attach are synonymous with unmount and mount, but check man hdiutil.

    The disk identifiers can be found in do shell script "hdiutil info". You can "coerce" the disk image name to a disk identifier with a bit of code, and that allows you to repeatedly mount and unmount the disk image with:

    tell app "Terminal" to do script "hdiutil (un)mount diskXXX"

    But once you "eject" the disk image, there will be no disk identifier.

  5. 0

    Henri Dominique Rapin

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    You are right, using "detach" and you loose the disk id.

    Man Page of hdiutil

    detach dev_name [-force]

    detach a disk image and terminate any associated hdid process.

    dev_name is a partial /dev node path (e.g. "disk1"). As of OS

    X 10.4, dev_name can also be a mount point.

    If Disk Arbitra-tion Arbitration

    tion is running, detach will use it to unmount any filesystems

    and detach the image. If not, detach will attempt to unmount

    any filesystems and detach the image directly (using the

    `eject' ioctl). If Disk Arbitration is not running, it may be

    necessary to unmount the filesystems with umount(8) before

    detaching the image. eject is a synonym for detach.

    Options:

    force Similar to umount -f. Unmounts any filesystems and

    detaches the image, regardless of any open files on

    the image.

    http://developer.apple.com/documentation/Darwin/Reference/ManPages/man1/hdiutil. 1.html

  6. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    I've had a moment to summarize how all this actually works:

    If you don't want to lose the disk image, use "hdiutil unmount diskxxx" in the terminal script.

    To mount it again, use "hdiutil mountvol diskxxx" (trying to mount by opening the .dmg file won't work because the disk already exists -- but is unmounted). Note the change in the command to include "vol".

    If you are using the disk identifier, "diskutil (un)mount diskxxx" also works.

    If you DO want to lose the disk image, use "hdiutil detach (or eject) diskxxx"

    (or you can eject in the Finder using the disk image name). In either case, you will have to open the .dmg file to get it back. To do that use:

    "hdiutil attach >path to .dmg file<"do shell script" approach.

  7. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    Correction of formatting problems in previous post (hopefully)

    I've had a moment to summarize how all this actually works:

    If you don't want to lose the disk image, use "hdiutil unmount diskxxx" in the terminal script.

    To mount it again, use "hdiutil mountvol diskxxx" (trying to mount by opening the .dmg file won't work because the disk already exists -- but is unmounted). Note the change in the command to include "vol".

    If you are using the disk identifier, "diskutil (un)mount diskxxx" also works.

    If you DO want to lose the disk image, use "hdiutil detach (or eject) diskxxx"

    (or you can eject in the Finder using the disk image name). In either case, you will have to open the .dmg file to get it back. To do that:

    use "hdiutil attach >path to .dmg file<"do shell script" approach.

  8. 0

    osimp

    Imported from the Apple Support Forums via the Wayback Machine 🔗

    ANOTHER TRY AT THE FORMATTING PROBLEMS

    I've had a moment to summarize how all this actually works:

    If you don't want to lose the disk image, use "hdiutil unmount diskxxx" in the terminal script.

    To mount it again, use "hdiutil mountvol diskxxx" (trying to mount by opening the .dmg file won't work because the disk already exists -- but is unmounted). Note the change in the command to include "vol".

    If you are using the disk identifier, "diskutil (un)mount diskxxx" also works.

    If you DO want to lose the disk image, use "hdiutil detach (or eject) diskxxx"

    (or you can eject in the Finder using the disk image name). In either case, you will have to open the .dmg file to get it back. To do that use:

    "hdiutil attach path to .dmg file"

    NOTE: all of these work with "do shell script" but any command that involves unmount, detach, or eject has a long delay when you use the "do shell script" approach.