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


Click here to return to the 'AppleScript for selecting local volumes to unmount' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
AppleScript for selecting local volumes to unmount
Authored by: llee on May 07, '12 11:56:46AM

Some disk names were too long to be included, so I worked on this some more. It's not the most elegant, because it may execute some redundant commands, and may not be optimized as much as it could be, but local disk lists are usually finite enough that considerations such as those don't have to take top priority.

For clarification, I should probably state clearly what this does and what it might be good for. It toggles the mount status of all local volumes. Initially, it tries to unmount all local volumes except for your boot volume. If it finds no local volumes except for your boot volume mounted, it tries to mount them all. One practical use for it might be if you were busy working, then discovered that you needed to install something but you knew the installer would try to spend time examining all your local disks. With this script, you could unmount every local volume but your boot volume, perform the installation, then quickly mount all your available local volumes again.

-----
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
set dur to dur & m
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
set answer_list to answer_list & (the_answer as string)
end repeat

set active_list to {}
repeat with tish from 1 to count of answer_list
--set recordnum to tish
set this_key to item tish of answer_list
set this_value to item tish of foundDisks
--set newrec to {a:1}
set this_rec to {recordnum:tish, diskname:this_key, devname:this_value} as record
set this_rec to {diskname:this_key, devname:this_value} as record
set the end of active_list to this_rec
end repeat

set not_boot_disks to {}
set not_boot_devs to {}
repeat with ant in active_list
if diskname of ant is not in {((characters 1 thru -2 of (path to startup disk as string)) as text)} then
set not_boot_disks to not_boot_disks & (diskname of ant)
set not_boot_devs to not_boot_devs & (devname of ant)
end if
end repeat

if (count of not_boot_disks) > 0 then
repeat with b in not_boot_disks
set theIndex to my CollectUniqueItemIndex(not_boot_disks, (b as string))
set theValue to item theIndex of not_boot_devs
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
else
--this tries to mount all available disks and isn't particular about what kind or whether already mounted
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

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

Edited on May 07, '12 12:14:14PM by llee



[ Reply to This | # ]
AppleScript for selecting local volumes to unmount
Authored by: llee on May 10, '12 10:32:19AM

And here's the condensed version:

-----

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



[ Reply to This | # ]