AppleScript for selecting local volumes to unmount

May 03, '12 07:30:00AM

Contributed by: llee

Note: An updated version of this script (in fact two new scripts) has been posted as a new hint. These new scripts take into account many of the comments posted below. Please see the new hint to find the improved version.

Here's an AppleScript that presents a dialog box listing mounted local volumes, one or more of which can be selected for unmounting. Some solutions for doing this either require the use of additional system resources or are offered commercially. This AppleScript is free, of course. I’ve only tested it in Lion so far. I recommend that you set up a keyboard shortcut that launches the AppleScript either as part of a Mac OS X Automator Service, or from the Script menu, which (in Lion) can be configured using the Keyboard preferences.

set alldisks to paragraphs of (do shell script "df -hlg | awk -F/ '/disk*/ {print $5}'")
try
   set item 1 of alldisks to ((characters 1 thru -2 of (path to startup disk as string)) as text)
on error
   set alldisks to {((characters 1 thru -2 of (path to startup disk as string)) as text)}
end try

set dur to {}
repeat with m in alldisks
   considering case
       if m is not in {"MobileBackups"} then
           set dur to dur & m
       end if
   end considering
end repeat

set devnames to paragraphs of (do shell script "df -k | awk -F/ '/disk*/ {print $3}'")
set t to {}
repeat with s in devnames
   set t to t & (word 1 of s)
end repeat

set the keylist to dur
set the foundDisks to t
set answer_list to {}

repeat with the_answer in keylist
   set theIndex to my CollectUniqueItemIndex(keylist, (the_answer as string))
    set theValue to item theIndex of foundDisks
   activate
   --set answer_list to answer_list & return & ((the_answer & ":" & space & theValue) as string)
   --set answer_list to answer_list & ((the_answer & ":" & space & theValue) as string)
   set answer_list to answer_list & (the_answer as string)
end repeat


set your_selected_device_id to choose from list answer_list with prompt "Please choose one or more volumes to be unmounted." with multiple selections allowed
repeat with b in your_selected_device_id
   set theIndex to my CollectUniqueItemIndex(answer_list, (b as string))
    set theValue to item theIndex of foundDisks
   --display dialog "The device name of a volume you selected is:" & space & "\"" & theValue & "\"." & space & "Are you sure you want to unmount it?"
   try
       do shell script "diskutil unmount /dev/" & theValue
   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 CollectUniqueItemIndex(theList, theItem) -- the Item can be string, number, constant, app object or list    
   local aListMember
   repeat with i from 1 to (count theList)
        set aListMember to item i of theList
       if aListMember = theItem then
           set theIndex to i
           exit repeat
       end if
   end repeat
   return theIndex
end CollectUniqueItemIndex

[kirkmc adds: This is a very nice script, works as advertised.]

Comments (24)


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