PDA

View Full Version : Help with a script




GabooN
Jul 4, 2012, 06:56 PM
I hope I am asking in the correct sub forum.

I just installed an optibay style hard drive. I have my HDD in the dedicated hard drive space, and my SDD (boot drive) in the optibay. Everything is fine with the drives and setup itself. This is in 2009 13" MBP

I am trying to write an Apple Script to mount and unmount the drive (it is 2 partitions but I would be mounting the entire disk, not selective volumes). I wanted a single script to do both action for simplicity sake. I use this MacBook for school, so I wanted to be able to 'turn off' the HDD to save power/wear and tear since it houses my iTunes, Aperture library, Downloads folder etc.

What I have so far.


tell application "Finder"
if not (exists the disk "disk1") then
do shell script "diskutil mountDisk /dev/disk1"
delay 2
end if
end tell

tell application "Finder"
if (exists the disk "disk1") then
do shell script "diskutil unmountDisk /dev/disk1"
end if
end tell

This works to MOUNT the drive if I have already manually unmounted it, but it does not work to UMOUNT the drive if it is currently mounted and showing on my desktop. I tried substituting eject instead of unmountDisk but that didn't work.

the disk name disk1 is correct. The two partitions are disk1s2 and disk1s3 if that matters at all.

I also tried using the disk name ST95005620AS Media (which shows up in info pane for the drive in Disk Utility) on the if and if not lines of the script.

Any help with what I am missing would be appreciated.



GabooN
Jul 4, 2012, 08:12 PM
I have tried to write it this way as well (this time just targeting 1 partition of the drive "disk1s2". The other partition get automatically unmounted by carbon copy cloner after a clone process)

tell application "Finder"
if not (exists the disk "disk1s2") then
do shell script "diskutil mount /dev/disk1s2"
delay 2
else if (exists the disk "disk1s2") then
do shell script "diskutil unmount /dev/disk1s2"
end if
end tell

Still only mounts the partition, does not unmount.

This script works on it's own, I just can't get it to work within the larger script that will do both, dependent on the current mount state.

do shell script "diskutil unmount /dev/disk1s2"

kryten2
Jul 4, 2012, 08:28 PM
I really can't believe you named your disk disk1. That's the identifier. In Terminal do :

diskutil list

and look at the name of your disk. Substitute disk1 with the name of your disk in the script.

GabooN
Jul 4, 2012, 08:53 PM
I didn't name it disk1, I was just trying to follow some script I found. It was working for mounting but not for unmounting.

Ok, so the disk is called Rubicon HD, so I substituted the name in the code. The code now will mount and then unmount my drive if it is currently unmounted, or it will simply unmount the drive if it is currently mounted. What modifier am I missing in order to stop it if mounting is successful?

I can currently do it with 2 simple scripts, but was hoping to just have one script.

*edit* I misunderstood the source for the script, they were displaying two separate scripts. How do I turn these two actions into one script that will run the mount/unmount depending on the current state of the drive?

tell application "Finder"
if not (exists the disk "Rubicon HD") then
do shell script "diskutil mount /dev/disk1s2"
delay 2
end if
end tell

tell application "Finder"
if (exists the disk "Rubicon HD") then
do shell script "diskutil unmount /dev/disk1s2"
end if
end tell

----------

Ok I sorted it out by using an Else.

Thanks for your help, I had never worked with applescript before.

tell application "Finder"
if not (exists the disk "Rubicon HD") then
do shell script "diskutil mount /dev/disk1s2"
else
do shell script "diskutil unmount /dev/disk1s2"
end if
end tell