1

I made the below AppleScript

tell application "DiskImageMounter"
    try
        open alias imagePath
    on error errText
        display dialog "Could not mount" & errText
    end try
end tell    

I would like to remake it in JavaScript. I tried to find the current API in the Script Editor library, but could not find how to control Disk Utility with JavaScript.

What is the syntax or reference to interface with Disk Utility or otherwise mount a disk image file from the JXA framework?

0

1 Answer 1

2

One wouldn't use DiskImageMounter to do this because, despite it giving the illusion that it's scriptable and loaded with commands in its AppleScript dictionary, it never returns a valid reference to any image that it opens; therefore AppleScript never gets informed it's done the job; and DiskImageMounter sacks it off, quits, and invalidates the connection with AppleScript thus throwing an error. And it does this every. Single. Time. So, it is reliably balls.

The way to mount a disk image with AppleScript is to use an application I very rarely endorse using but for very few specific tasks, which is Finder. DiskImageMounter is the only application less reliable than Finder, but Finder is a trier, and it does succeed with mounting volumes without causing too much of a stink:

set f to POSIX file "/path/to/diskimage.dmg" as alias
tell application id "com.apple.Finder" to open f

It, too, doesn't return a reference to the mounted volume, but you can get Finder to enumerate the disks already mounted beforehand, then enumerate them again afterwards, and the new one that appears will be your mounted volume:

set f to POSIX file "/path/to/diskImage.dmg" as alias
tell application id "com.apple.Finder"
    set ejectables to disks

    open f

    repeat until disks ≠ ejectables
        delay 1
    end repeat

    return the POSIX path of last item of (disks as alias list)
end tell

Finally... The JXA:

Finder=Application('com.apple.Finder');
f=Path('/path/to/diskImage.dmg');
Finder.open(f);

You must log in to answer this question.

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