General clipboard question

I would like to unmount an external hard drive volume with a disk id “disk1s3” (just as an example). I find that the simple AppleScript command

do shell script "diskutil unmount disk1s3"

consistently takes about 10 seconds to execute and always ends in the AppleScript error “Volume failed to unmount” (even though the volume actually does unmount). Alternatively, when I issue the command directly in Terminal, it uniformly unmounts in less than a second and without an error.

In an effort to reproduce the Terminal result while in AppleScript, I’ve created an executable Terminal shell script (“MacIntosh HD:Users:username:UnmountVolume.command”) which consists of a single line of text:

$(pbpaste) – executes whatever Terminal command is in the clipboard

I then use AppleScript to copy to the clipboard the desired Terminal command and execute that command by running the Terminal shell script (“MacIntosh HD:Users:username:UnmountVolume.command”). The AppleScript code is as follows:

set the clipboard to "diskutil unmount disk1s3"
tell application "Finder" to open file "MacIntosh HD:Users:username:UnmountVolume.command"

Using the clipboard technique, I consistently achieve error-free unmounting via AppleScript in less than a second, just as if I had run the command directly in Terminal.

I would like to ask two questions:

  1. Regarding the specific issue of unmounting a volume, why does the “do shell script” approach take so long and end in an error?

  2. More generally, I’ve been unable to figure out a way to send to the clipboard a multiline text containing more than one Terminal command. When I run the Terminal shell script, it executes only the first command, whether I separate the commands with semicolons or returns. Is there a way of sending more than one Terminal command via the clipboard?

Thank you very much for any insight you may be able to offer.

bmose

Hi bmose,

I don’t know why it takes so long, but the error is because the whole disk is not unmounted, just the partition. Try replacing ‘unmount’ with ‘unmountDisk’. Take a look at man diskutil in the Terminal.

Is there a reason you are not getting the Finder to do this?

set myName to "NAME OF DISK"
tell application "Finder" to eject (disks whose name is myName)

Best wishes

John M

Thank you for the response. Your ‘Finder’ approach works quickly and without error. But I should have clarified. I specifically want to unmount only one volume but leave the remaining volumes (partitions) of the disk mounted, and thus the use of ‘diskutil unmount …’ rather than ‘diskutil unmountdisk …’.

The puzzling aspect of this is that the ‘diskutil unmount …’ commands works perfectly when executed from Terminal but hangs up and generates an error (even though it eventually unmounts the volume) when executed via AppleScript’s 'do shell script “diskutil unmount …” '.

You might want to take a look at this page that gives various reasons why ‘do shell script’ is different from the Termnal:
http://developer.apple.com/technotes/tn2002/tn2065.html

John M

John,

Thank you again. I looked through the document you recommended and gained a number of insights on differences between Terminal and AS’s “do shell script”. Still, the specific problem noted above concerning “diskutil unmount …” remains a mystery! Nonetheless, it was fruitful reading.

bmose