mount a disk image

Hello

One of my scripts must mount a disk image storing datas which aren’t supposed to be inline all the time.

At this time the useful code doing the job is :

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

property |⌘| : a reference to current application

set p2d to path to desktop as text
set diskYK to "resources YK" -- the name of the mounted disk image
set diskImageURL to (p2d & diskYK & ".sparseimage") as «class furl»
set diskImageURL to (|⌘|'s NSArray's arrayWithObject:diskImageURL)'s firstObject()

set diskURL to (|⌘|'s NSArray's arrayWithObject:(diskYK as «class furl»))'s firstObject()
set avail to (diskURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean
if not avail then do shell script "hdiutil attach " & quoted form of (diskImageURL's |path|() as text)

Is it a way to achieve this task without using do Shell Script ?

In the help of Xcode I found instance methods named “attach” belonging to Kernel but I really don’t know if we may trigger them with ASObjC.
As the help just states : “No overview available”, it doesn’t really help me.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 10 février 2020 18:06:43

Yvan, there is no Objective-C API to mount disk images.

Thank you Stefan, it’s what I faired.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 10 février 2020 20:38:23

Yvan, you could possibly store a bookmark on disk, and resolve that to mount it.

Thank you Shane, it’s exactly what I needed.

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

property |⌘| : a reference to current application

set p2d to path to desktop as text
set diskYK to "resources YK" -- the name of the mounted disk image
set p2alias to p2d & diskYK -- alias pointing upon the sparseimage
set aliasURL to (|⌘|'s NSArray's arrayWithObject:(p2alias as «class furl»))'s firstObject()
set forYK to (aliasURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean
if forYK  then set diskURL to (|⌘|'s |NSURL|'s URLByResolvingAliasFileAtURL:aliasURL options:0 |error|:(missing value))

I tried to do that with a symlink but got nothing.
I may also unmount the disk with:

use AppleScript version "2.5"
use framework "Foundation"
use framework "AppKit" -- for Workspace
use scripting additions

property |⌘| : a reference to current application

set diskYK to "resources YK" -- the name of the mounted disk image
set diskURL to (|⌘|'s NSArray's arrayWithObject:(diskYK as «class furl»))'s firstObject()
if (diskURL's checkResourceIsReachableAndReturnError:(missing value)) as boolean then
	set theNSWorkspace to |⌘|'s NSWorkspace's sharedWorkspace()
	theNSWorkspace's unmountAndEjectDeviceAtURL:diskURL |error|:(missing value)
end if

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 11 février 2020 11:39:29

By the way the line

set aliasURL to (|⌘|'s NSArray's arrayWithObject:(p2alias as «class furl»))'s firstObject()

is pretty cumbersome. Apparently p2alias is an HFS path so the NSURL API is more suitable

set aliasURL to |⌘|'s NSURL's fileURLWithPath:(POSIX path of p2alias)

This works also with an AppleScript alias specifier

Hello Stefan

I wish to work without using POSIX paths, that’s why I borrow the code used by Shane Stanley in FileManagerLib.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mardi 11 février 2020 12:05:24