Sunday, February 23, 2020

#1 2020-02-10 11:08:14 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3951

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 :

Applescript:

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

Offline

 

#2 2020-02-10 11:50:59 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11668
Website

Re: mount a disk image

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


regards

Stefan

Offline

 

#3 2020-02-10 01:38:32 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3951

Re: mount a disk image

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

Offline

 

#4 2020-02-10 04:36:09 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6192

Re: mount a disk image

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


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2020-02-11 04:48:30 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3951

Re: mount a disk image

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

Applescript:

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:

Applescript:

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

Offline

 

#6 2020-02-11 04:59:09 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11668
Website

Re: mount a disk image

By the way the line

Applescript:

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

Applescript:

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

This works also with an AppleScript alias specifier

Last edited by StefanK (2020-02-11 05:02:43 am)


regards

Stefan

Offline

 

#7 2020-02-11 05:06:01 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3951

Re: mount a disk image

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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)