Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Two AppleScripts for unmounting, mounting local disk volumes Apps
These AppleScripts are related to this hint. Both can already be found in the replies to the hint topic post, but I'm resubmitting them so that they will appear together.

The first is an improved version of the original AppleScript which presents a dialog box from which a selection of local volumes to be ejected can be made. An example of its usefulness might be that it provides a reliable method for quickly ejecting a MacBook's mounted local volumes for users on the go. I've configured it as a "Run AppleScript" step for an Automator-based Mac OS X Service called "Unmounter" and assigned it the keyboard shortcut of (Command-Control-Shift-E) in System Preferences on my system.

The second is a more robust version of the AppleScript included in my first reply to the original hint topic. It attempts to automatically unmount all unmountable local volumes, and if it finds none, attempts to mount any local volumes that are available. I've configured it as a "Run AppleScript" step for an Automator-based Mac OS X Service called "Toggle Available Local Volumes" and assigned it the keyboard shortcut (Command-Option-Control-Shift-E) in System Preferences on my system.

For both Automator-based Mac OS X Services that I created to provide key triggers for the AppleScripts, the scope specified is "Service receives no input in any application."
set alldisks to paragraphs of (do shell script "df -hlg | awk -F/ '/disk*/ {print $5}'")
set nonbootnumber to (count of alldisks)
try
   set alldisks to items 2 thru nonbootnumber of alldisks
   activate
   set your_selected_device_id to choose from list alldisks with prompt "Please choose one or more volumes to be unmounted." with multiple selections allowed
   repeat with the_Item in your_selected_device_id
       set the_ID to (do shell script "df -hlg | grep -m 1" & space & quoted form of the_Item & space & "| grep -o 'disk[0-9][a-z][0-9]*'")
        try
           do shell script "diskutil unmount /dev/" & the_ID
       on error the error_message number the error_number
           display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
       end try
   end repeat
on error the error_message number the error_number
   if the error_number is -128 or the error_number is -1708 then
   else
       display dialog "There are no unmountable volumes." buttons {"OK"} default button 1
   end if
end try


set alldisks to paragraphs of (do shell script "df -hlg | awk -F/ '/disk*/ {print $5}'")
set nonbootnumber to (count of alldisks)

try
   set all_non_boot_disks to items 2 thru nonbootnumber of alldisks
on error
   set all_non_boot_disks to {}
end try
if (count of all_non_boot_disks) > 0 then
   try
       activate
       repeat with the_Item in all_non_boot_disks
           set the_ID to (do shell script "df -hlg | grep -m 1" & space & quoted form of the_Item & space & "| grep -o 'disk[0-9][a-z][0-9]*'")
            try
               do shell script "diskutil unmount /dev/" & the_ID
           on error the error_message number the error_number
               display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
           end try
       end repeat
   on error the error_message number the error_number
       if the error_number is -128 or the error_number is -1708 then
       else
           display dialog "There are no unmountable volumes." buttons {"OK"} default button 1
       end if
   end try
else
   set actiondisks to {}
    set allvols to paragraphs of (do shell script "diskutil list -plist | grep -o 'disk[0-9][a-z][0-9]*'")
    repeat with i in allvols
       if i is not in actiondisks then
           set actiondisks to actiondisks & i
       end if
   end repeat
   repeat with myitem in actiondisks
       try
           do shell script "diskutil mountDisk /dev/" & myitem
       end try
   end repeat
end if
[kirkmc adds: I'm posting this so the final scripts can be easier to find. There were a lot of comments that helped the poster improve on his original submission, and I thank all the users who gave their thoughts on the original script, and the poster who took the time to improve it and re-post it.]
    •    
  • Currently 2.00 / 5
  You rated: 4 / 5 (4 votes cast)
 
[9,208 views]  

Two AppleScripts for unmounting, mounting local disk volumes | 13 comments | Create New Account
Click here to return to the 'Two AppleScripts for unmounting, mounting local disk volumes' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: oschrenk on May 15, '12 07:59:04AM

Great scripts!

Would you be able to post a version of this script that just unmounts all local mounted disks without the dialog box? My use case is that I I have one hdd with two partitions that I regularly mount (and unmount), no need for me to select which one.

Another idea would be to use growl when the disks are successfully unmounted.



[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: llee on May 15, '12 10:15:05AM

The 2nd script only unmounts local volumes as long as any besides the boot volume remain mounted, so it does what you want, unless everything is already unmounted. If you want to defeat its mounting ability, just remove everything between the last "else" and "end if".

I don't know about Growl. I stopped using when it stopped being free. Anyway Mountain Lion will have that feature, so I might wait.

As far as any other notification that all the local volumes have been ejected, I'll have to think about it. Other's might be able to contribute that more readily.



[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: designr on May 15, '12 12:18:18PM

tell application "Finder"
eject (every disk)
end tell



[ Reply to This | # ]
For Quicksilver: Two AppleScripts for unmounting, mounting local disk volumes
Authored by: morespace54 on May 15, '12 12:21:30PM
If you use Quicksilver, you could add:

tell application "Quicksilver" 
    show notification "Volumes unmounted." 
end tell 
or

tell application "Quicksilver" to show large type (Volumes unmounted.)
end tell 
Edited on May 15, '12 12:22:18PM by morespace54


[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: oschrenk on May 16, '12 08:49:08AM
Thanks for the input! For now I'll use this:

tell application "Finder"
	eject (every disk)
end tell

tell application "System Events"
	set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
end tell

if isRunning then
	tell application id "com.Growl.GrowlHelperApp"
		set the allNotificationsList to {"Eject Disk Notification"}
		set the enabledNotificationsList to {"Eject Disk Notification"}
		
		register as application 
			"Growl Eject Disk Notification" all notifications allNotificationsList 
			default notifications enabledNotificationsList 
			--       Send a Notification...
		notify with name 
			"Eject Disk Notification" title 
			"Eject Disk Notification" description 
			"Ejected all disks." application name "Growl Eject Disk Notification" icon of application "Finder.app"
	end tell
end if


[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: veke on May 15, '12 10:43:46AM

I'd like to have a script that does the opposite of the first one, displays a list of unmounted volumes for me to mount? I regularly mouth/unmount volumes I don't use all the time. I thought the second script was for that, but it isn't. I use an application for the mounting bit now, but it would be very nice to do it through a script. Any ideas?



[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: Rhodess on May 15, '12 10:28:36PM

I love the scripts....... But how do I modify them to list all mounted network shares and then allow one to choose which share to unmount?

ls /Volumes
hdiutil unmount /Volumes/

tell application "Finder"
if (exists the disk "ShareName") then
do shell script ("hdiutil unmount /Volumes/ShareName -force")
end if
end tell

I tried playing around with the above commands, but I can't get it to work.



[ Reply to This | # ]
AppleScript for unmounting volumes (without do shell script)
Authored by: DougKorns on May 22, '12 06:07:26AM

Every time I see scripts with some cryptic UNIX command in them, I get the impression that the UNIX people just don't get AppleScript. The full capability to do this action can be done simply and elegantly using just Finder Scripting:

--
tell application "Finder"
set StartupDisk to name of first disk whose startup is true
set EveryDisk to name of every disk
--set LocalDisks to name of every disk whose local volume is true
--set ServerDisks to name of every disk whose local volume is false

set EligibleDisks to {}
repeat with EachDisk in EveryDisk
if EachDisk is not in {StartupDisk, "home", "net"} then
set end of EligibleDisks to EachDisk -- Eligible for dismount
--Might also want to omit "Time Machine Backups"
end repeat

choose from list EligibleDisks with prompt "Please choose one or more volumes to be unmounted." default items EligibleDisks with multiple selections allowed
set ChosenDisks to the result

if class of ChosenDisks is boolean then
-- User Canceled
else
repeat with EachDisk in ChosenDisks
try
tell application "Finder" to eject EachDisk
on error ErrMsg number ErrNmbr
display dialog "Unable to eject disk: " & EachDisk
end try
end repeat
end if
end tell
--
Here the default list to dismount is preset to all eligible disks.

The problem with two disks having the same same can't be solved, without additional script to recognize that situation, because you can't identify which disk of the two was selected in the 'choose from list' result.



[ Reply to This | # ]
AppleScript for unmounting volumes (without do shell script)
Authored by: llee on Jun 11, '12 01:29:29PM

When you're dealing with more than one disk with the same name, i think there's a good chance you'll remember which was mounted in which order. If you can't, you're going to have to examine anyway, in which case it's probably useless to depend on this kind of script to give you enough information to be of any benefit.



[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: MacTipster on Jun 01, '13 11:54:20AM

How to prevent the internal iMac disk from mounting when booting OS X 10.8.3 (Mountain Lion) or later from an external SSD?

Scenario 1: only one iMac used, booting from the external SSD.
Scenario 2: two iMacs used (work and home), booting from the same external SSD.

Thanks.

Edited on Jun 01, '13 11:55:15AM by MacTipster



[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: MacTipster on Jun 02, '13 01:00:31AM

Yesterday I run the script indicated above:

-----------------
tell application "Finder"
eject (every disk)
end tell
-----------------

and it worked ejecting the internal iMac disk after booting from an external SSD.

But today it does not work. The internal iMac disk is not ejected. 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.

How to fix that?
Thanks



[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: MacTipster on Jun 15, '13 05:36:43AM

Best script is shown here:

MERGING two Scripts to Unmount disks except booting disk
https://discussions.apple.com/thread/5102909

Enjoy!



[ Reply to This | # ]
Two AppleScripts for unmounting, mounting local disk volumes
Authored by: llee on May 09, '14 02:16:37PM

Updated for Mavericks:


--begin script


set alldisks to paragraphs of (do shell script
"diskutil list|grep -v 'EFI EFI'|grep -v 'Recovery HD'|grep -o 'disk[0-9][a-z][0-9]*'")
set all_non_boot_disks to paragraphs of (do shell script "df -hlg | awk -F/ '/disk*/ {print $5}'")
set nonbootnumber to (count of alldisks)
if (count of all_non_boot_disks) > 0 then
try
set all_non_boot_disks to items 2 thru nonbootnumber of alldisks
on error
set all_non_boot_disks to {}
end try
activate
repeat with the_Item in alldisks
try
set the_ID to (do shell script "df -hlg | grep -m 1" & space &
quoted form of the_Item & space & "| grep -o 'disk[0-9][a-z][0-9]*'")
do shell script "diskutil unmount" & space & the_ID
on error the error_message number the error_number
if the error_message does not contain ":" then
else
if button returned of (display dialog "A disk couldn't be unmounted.
Would you like to re-mount those already unmounted?" buttons
{"Cancel", "Re-mount"} default button 2) is "Re-mount" then
my mountAll(alldisks)
end if
end if
end try
end repeat
else
my mountAll(alldisks)
end if

on mountAll(alldisks)
set actiondisks to {}
repeat with i in alldisks
if i is not in actiondisks then
set actiondisks to actiondisks & i
end if
end repeat
repeat with myitem in actiondisks
try
do shell script "diskutil mount" & space & myitem
end try
end repeat
end mountAll

--end script



[ Reply to This | # ]