Two AppleScripts for unmounting, mounting local disk volumes

May 15, '12 07:30:00AM

Contributed by: llee

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.]

Comments (13)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20120514064909940