script to unmount and mount local volume?

Hi, I searched all through the forums and I couldn’t find what I’m looking for… I’d like, on startup, to unmount an external firewire drive that I normally leave on and running and then mount the same drive right before my nightly backup. I’m sure it’s quite simple, but I’m a complete noob in AS. I suppose I’d actually need 2 scripts, the unmount script as a login item(?) and then the mount script I could trigger as a timed ical alarm event. Any advice?

Thanks

Model: iMac 2 GHz Intel Core Duo
Browser: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061024 BonEcho/2.0
Operating System: Mac OS X (10.4)

in terminal, type

for more information on how to use these commands. There is an apple script line of code that will interpret a string and run it as a shell command. Use the shell commands mount and umount to actually mount and unmount your drives.

On your mounting script, do something like

do shell script "mount /dev/diskN /Volumes/NAME"

where N is the number of the disk drive that you are mounting, and NAME is the name you gave your harddrive

if you dont know the number of your disk drive, go to Terminal and type

this will list all your harddrives and you will see something similar to this:

so if i wanted to mount my “Rob” disk i would do this:

do shell script "mount /dev/disk0s5 /Volumes/Rob"

Now if i want to Unmount my “Rob” disk i would do this in applescript:

do shell script "umount /Volumes/Rob"

Hi,

there are some difficulties to do it this way.

The mount command is quite inconvenient, because you have to set a mount point in /Volumes.
The second problem is, the df command lists only mounted volumes.

A better way is using diskutil
for mount:

set theDisk to "myVolume"
do shell script "diskutil mount `diskutil list | awk '/ " & theDisk & " / {print $NF}'`"

and for unmount:


set theDisk to "myVolume"
do shell script "diskutil unmount /Volumes/" & theDisk

or

tell application "Finder" to eject disk theDisk

Note: The Finder can only eject disks, whose ejectable flag is true

Thanks for your help, but neither of those methods worked. I just got error messages when I ran them in script editor. I was able to kludge a working method together though… For the mount script I poked around here a bit more and found this:

tell application “Finder”
set num to 0
set dvce to “not null”
repeat while dvce > “”
set dvnum to “diskutil list /dev/disk” & num & " | grep disk" & num & " | awk ‘{print $5}’ " as string
set dvce to do shell script dvnum as string
set cnt to (count of words of dvce) as string
if cnt > 1 then
set devicename to word 2 of dvce
if exists devicename then
– already mounted
else
do shell script “diskutil mountDisk /dev/” & devicename
end if
end if
set num to num + 1
end repeat
end tell

maybe it’s overkill. I have no idea what it means so I’m not sure, but it gets the job done. I’ve attached it to an ical event as an alarm that runs a few minutes before my backup.

For the unmount script I put together a simple app using automator and have attached that to an ical event as an alarm that runs a few minutes after startup.

So not very elegant, but I’m hoping it gets it done.

Hi,

this is very strange.
I use the diskutil method almost every day an it works always flawlessly.

Which error messages do you get?

:slight_smile: yea i just noticed that umount and mount may not work in this case… sorry.

StefanK-

this is what I get for the unmount script:

“Disk Utility Tool
Usage: diskutil [mount(Disk)|unmount(Disk)|eject]
[Mount Point|Disk Identifier|Device Node]
Mount, unmount or eject local disks or volumes.
force is only valid on unmount or unmountDisk.
Example: diskutil unmount /Volumes/SomeDisk”

I guess, the name of your disk contains a space character

try this:

set theDisk to "my Volume"
do shell script "diskutil unmount " & quoted form of ("/Volumes/" & theDisk)

StefanK-

yes, my volume does contain a space character. I tried the new script you’ve posted and still no success… I tried it as is and also by changing “theDisk” to the name of my volume. When I run your script in script editor that term “theDisk” is hi-lighted in green… I’m not sure what that indicates. Honestly, I really don’t know AS very well, I only delve into it when I need to automate some niggling task.

set dvnum to "diskutil list /dev/disk" & num & " | grep disk" & num & " | awk '{print $5}' " as string

AFAIK disks with 0 are you local hard disk scheme.

You know the name of the disk you want to un/mount
The awk '{print $5} will always print column 5 but device node entry may be in a column after that.

So just using set dvnum to do shell script "diskutil list"will get all your disks.

Because again AFAIK there is no guarantee that the device node entry will always be the same, if you have other volumes mounted like cd’s DVD’s.

So Grep your disk name. and the last word in the result will always be the device node entry
So you can get the last word by using set dvceMount to word -1 of dvce as string or set dvceMount to last word of dvce as string

To use unmount or mount you need to use /dev/device node entry not /volumes/ when using the device node entry

tell application "Finder"
	set dvce to "Mark\\ iPod"
	set dvce to do shell script "diskutil list   | grep " & quoted form of dvce
	set dvceMount to word -1 of dvce as string
end tell
do shell script "diskutil unmount " & quoted form of ("/dev/" & dvceMount)

I really don’t understand this :confused:

the normal shell syntax is: diskutil unmount /Volumes/MacHD
the syntax with quoted path is: diskutil unmount ‘/Volumes/Macintosh HD’

with the code:

set theDisk to "Macintosh HD"
display dialog  "diskutil unmount " & quoted form of ("/Volumes/" & theDisk)

exactly the string diskutil unmount ‘/Volumes/Macintosh HD’ will be displayed.

It takes some seconds till the volume disappears.

Stefan,
I took out the quote’s from your script and it unmounted my disk.

But would not remount it.

Using the device node entry (see above post) unmounts and mounts without fail so far.

Hi Mark,

of course it does, if the name of the disk contains no space character :wink:

I agree totally, that’s the same what I wrote in my first post (for mount):

set theDisk to "myVolume" do shell script "diskutil mount `diskutil list | awk '/ " & theDisk & " / {print $NF}'`"

yes I should have made that a bit clearer,

the device I used was named “Mark iPod”.

I escaped the space before hand. “Mark\ iPod”

Using

set theDisk to "Mark\\ iPod"
do shell script "diskutil mount " & quoted form of ("/Volumes/" & theDisk)

With the quotes it does not work.
without them it, unmounts but does not mount.

If you use the /dev/ device node entry way you can umount and mount. with or without the quotes (spaces escaped)

So as you see spaces make no difference here.

PS.

I do like the way you back ticked the awk into the diskutil, did not spot that first time around

Now I got it, Mark. :slight_smile:

It’s interesting, that quoting doesn’t work in some cases (machines)

On both mine (PPC G4 & G5) quoting works perfectly

Yes it is, I wish I could figure it out as I have noticed this before after some frustration.

Stefan -

I’ve got your scripts (above) working nicely for one of my drives - thank you. I’d like them to consecutively mount / unmount two others if possible, rather than create three, or rather six, separate scripts. Assuming this is possible, please could you let me know what adjustments to make?

Many thanks, Tim.

Hi Tim,

just add a list and a repeat loop:

set mountDiskList to {"Volume1", "Volume2", "Volume3", "Volume4"}
repeat with i in mountDiskList
	do shell script "diskutil mount `diskutil list | awk '/ " & i & " / {print $NF}'`"
end repeat


set unmountDiskList to {"Volume1", "Volume2", "Volume3", "Volume4"}
repeat with i in unmountDiskList
	do shell script "diskutil unmount " & quoted form of ("/Volumes/" & i)
end repeat

Yeah - got it. It wasn’t working because I had spaces in my volume names. Derr! Now I’ve got rid of those it works nicely, so many thanks. One thing though: if I had minded and wanted to keep the spaces, what could I have done to make it work? I seem to remember that in Unix Exec scripts, you put a '' before any space. I tried that, but it didn’t work.

Tim.

Both (last) scripts should work also with spaces in disk names.
The mount script has a quotation (the slashes) in the awk command
The unmount script uses the quoted form of statement