Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Script to automatically UNMOUT all disks EXCEPT Booting Disk

The following script works directly as "AppleScript Editor" script or application (without any extra modification required) to automatically unmount the "Macintosh HD" disk after booting from other external disk:


---------------------

set volname to "Macintosh HD" -- # name of target volume

set p to (POSIX path of (volname & ":" as alias))'s text 1 thru -2

set sh to "diskutil umount " & quoted form of p & " &> /dev/null &"

do shell script sh

---------------------


How to modify it (or create a new one from scratch) to unmount all disks (except the booting one, of course), whatever their names and number?


Better if the script does not issue any confirmation or warning of any kind, but works in the background without any feedback to the user.


That is great to automatically unmount ALL disks (except the booting one) after rebooting or booting, saving energy, heat, noise and speeding up the Mac performance (eg., when selecting save as, etc, since such unused disks do not show), extending the life of the unused disks.


Of course, any disk can be later on manually mounted using the Disk Utility at any time, if required.


Thanks.

Posted on Jun 9, 2013 3:23 AM

Reply
Question marked as Best reply

Posted on Jun 9, 2013 9:44 AM

You shouldn't make assumptions about what the names of the volumes are - both the Finder and System Events have terminology to determine if a disk is the startup volume (or a local volume, for that matter), for example:


tellapplication"System Events"


repeatwithsomeDiskin (getdiskswhosestartupisfalseandlocal volumeistrue)

setsomeDisktoPOSIX pathofsomeDisk

do shell script"diskutil umount " & quoted formofsomeDisk & " &> /dev/null &"


endrepeat

endtell


Note that if you are unmounting a disk from a standard account you will be prompted for administrator authentication.

28 replies
Question marked as Best reply

Jun 9, 2013 9:44 AM in response to ApMaX

You shouldn't make assumptions about what the names of the volumes are - both the Finder and System Events have terminology to determine if a disk is the startup volume (or a local volume, for that matter), for example:


tellapplication"System Events"


repeatwithsomeDiskin (getdiskswhosestartupisfalseandlocal volumeistrue)

setsomeDisktoPOSIX pathofsomeDisk

do shell script"diskutil umount " & quoted formofsomeDisk & " &> /dev/null &"


endrepeat

endtell


Note that if you are unmounting a disk from a standard account you will be prompted for administrator authentication.

Jun 9, 2013 10:47 AM in response to red_menace

I pasted exactly the script above on Mac mini with Fusion Drive 1TB:


tell application "System Events"

repeat with someDisk in (get disks whose startup is false and local volume is true)

set someDisk to POSIX path of someDisk

do shell script "diskutil umount " & quoted form of someDisk & " &> /dev/null &"

end repeat

end tell


yet got the error indiated above. Console shows no messages.

Jun 9, 2013 11:26 AM in response to ApMaX

I'd guess the copy went bad.


Start up with a fresh copy of script editor.


Copy & paste again.


Strip down script & see what line is the problem.


tell application "System Events"


end tell


etc.


Post picture of script edit session.


To git rid of the crude, try copied to textwranger then from textwrangler to script editor.


finding old versions of TextWrangler

http://www.barebones.com/support/textwrangler/updates.html

Jun 9, 2013 11:58 AM in response to twtwtw

Thanks. I have found what was going on after a Google search of (with quotes):

"Expected “end” or “end tell” but found unknown token"


For instance (even though it refers to Leopard, it is also valid for Mountain Lion):


----------------------------------------

Compiling Embedded Scripts in Leopard (OS 10.5.1)

.../...

The problem is actually in Leopard. With all the unicode changes to AppleScript it now does not like the whitespace characters that Indigo uses to format the scripts. I worked around the problem in 3.0 by having it strip the white space characters before it sends the source to the OS to compile it.

.../...

Now that I know what's happening, I'll just strip the white space manually, or if it's a long script move it into Word and strip it with Edit/Replace.


Source: http://perceptiveautomation.com/userforum/viewtopic.php?f=4&t=2736

----------------------------------------


So, what I did was just remove all spaces on the left of the AppleScript Editor lines and added them manually (just one or two spaces) in such AppleScript Editor.


Then boh scripts above (from red_menace & twtwtw) work BEAUTIFULLY!!!

I will use the second one (the one from twtwtw), since it seems simpler.


A MILLION THANKS FOR ALL YOUR GREAT HELP!!!

Script to automatically UNMOUT all disks EXCEPT Booting Disk

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.