Hiroto wrote:
Hello
If it is safe to assume you're ejecting every device node mounted on mount point in /Volumes
Purely as an intellectual exercise, I decided to write a script to see if I could distinguish dmgs from other devices in /Volumes.
The following script appears to work to unmount every "external" (sic "ejectable") device except DMGs, though my testing hasn't been robust enough to guarantee it (at least one problem would be if you had more than 99 mounted devices, it would fail).
It could also be used to do the reverse - unmount only DMGs and nothing else - by adding 'not' to the conditional in the 'compareAndEject' handler.
getEveryDisk()
set diskList to the result
set origDeLims to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to return
set myDMGNames to {}
set x to {}
try
set x to (do shell script "hdiutil info | grep '/dev'") as text
end try
set AppleScript'stext item delimiters to origDeLims
if x is not equal to {} then
repeat with i from 1 to count of paragraphs in x
set z to getDMGName(paragraph i in x)
set end of myDMGNames to z
end repeat
compareAndEject(myDMGNames, diskList)
else
#add a # to the next line and remove the # from the following line if not on 10.9
display notification "No mounted disk images!"
#display dialog "No mounted disk images!"
endif
on getEveryDisk()
set origDeLims to AppleScript'stext item delimiters
set AppleScript'stext item delimiters to "/"
set allMounts to do shell script "diskutil list | grep /dev"
set allMounts to result as string
set allMounts to allMounts as text
set AppleScript'stext item delimiters to origDeLims
set diskList to {}
repeat with i from 1 to count of paragraphs in allMounts
set the end of diskList to (allMounts's paragraph i)
endrepeat
return diskList
end getEveryDisk
on getDMGName(aParagraph)
set a to items 1 thru 13 in aParagraph as list
set numList to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
repeat
if numList contains last item of a then
exit repeat
else
set b to the reverse of a
set a to the rest of b
set a to the reverse of a
end if
end repeat
set a to a as string
return a
end getDMGName
on compareAndEject(listDmgs, listDisks)
try
repeat with i in listDisks
if i is in listDmgs then
try
do shell script "diskutil unmountDisk " & i
end try
end if
end repeat
end try
end compareAndEject