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:
Regarding the specific issue of unmounting a volume, why does the “do shell script” approach take so long and end in an error?
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.