Apple Event: May 7th at 7 am PT

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

Jun 11, 2013 12:36 PM in response to ApMaX

STOP THE PRESS - UPDATE


Well, the above scripts unmount USB PenDrives and Memory Flash Cards, but they do NOT unmount the internal "Macintosh HD" disk when booting from an external disk. In such later case, the only one that unmounts such latter disk is the one indicated on the first post of this thread; that is:


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

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

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


But such script does not unmount the disks of USB PenDrives and Memory Flash Cards.


How to modify it (or create a new one) to unmount all disks except the external booting one, of course (without giving any feedback or warning if any of them was not mounted originally)?:


Internal Macintosh HD

PenDrives

Memory Cards


Ideally, even if the internal disk (when booting from an external one) has any other name and not necessarily "Macintosh HD".


Thanks.

Jun 11, 2013 5:35 PM in response to ApMaX

Just combine the two, wrapping the unmount/eject commands in a "try" block to suppress any errors:


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 &"

try


do shell scriptsh

tell application "Finder" to eject (every disk whose ejectable is true)

end try

Jun 11, 2013 6:22 PM in response to Phil Stokes

Thanks, yet as indicated in the link, the script:


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

tell application "Finder"

eject (every disk)

end tell

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


does not unmount the internal iMac disk. Console indicates:


AppleScript Editor[693]: *** WARNING: Method selectedRowEnumerator in class NSOutlineView is deprecated. It will be removed in a future release and should no longer be used.

Jun 11, 2013 6:48 PM in response to ApMaX

Hmph, looks like I didn't fully understand the question.


The easiest way to do what you want is to use the fstab file (/etc/fstab - not the file called fstab.hd, which is nothing but an exercise in cheekiness). On the external boot drive, create or edit the fstab file and put a line in it that reads something like:


UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx none hfs rw,noauto


where the x-ed out section is the UUID of your main volume. You can get that from diskutil (diskutil info volumeName). That will keep that volume from mounting at startup. Read man fstab.


So far as I know, fstab needs to specify volumes exactly. That's good for hard drives (which are usually few in number and don't change too often), but not so useful for flash drives and things like that. You might need to use fstab in concert with the eject script to get full results.

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.