Advertisement
applehelpwriter

unmount DMGs only

Mar 23rd, 2014
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. ABOUT
  3. script by Phil Stokes (c)2014
  4.  
  5. applehelpwriter.com
  6.  
  7. HOW TO USE
  8. --run the script as is to eject all and only disk images (i.e. eject .dmg but not hard disks)
  9. --to make the script eject all external disks EXCEPT dmgs, look at the function near the end of the script called 'on compareAndEject(listDmgs, listDisks)'. In this function, replace the line:
  10.  
  11.     if i is in listDmgs then
  12.    
  13.     with the line:
  14.    
  15.     if i is not in listDmgs then
  16.  
  17.  
  18. *)
  19.  
  20.  
  21. getEveryDisk()
  22. set diskList to the result
  23.  
  24. set origDeLims to AppleScript's text item delimiters
  25. set AppleScript's text item delimiters to return
  26. set myDMGNames to {}
  27. set x to {}
  28. try
  29.     set x to (do shell script "hdiutil info | grep '/dev'") as text
  30. end try
  31. set AppleScript's text item delimiters to origDeLims
  32.  
  33. if x is not equal to {} then
  34.    
  35.     repeat with i from 1 to count of paragraphs in x
  36.         set z to getDMGName(paragraph i in x)
  37.         set end of myDMGNames to z
  38.        
  39.     end repeat
  40.     compareAndEject(myDMGNames, diskList)
  41.    
  42.    
  43. else
  44.     #comment out the next line and remove the hash for the subsequent line if OS is <10.9
  45.     display notification "No mounted disk images!"
  46.     #display dialog "No mounted disk images!"
  47. end if
  48.  
  49.  
  50. on getEveryDisk()
  51.     set origDeLims to AppleScript's text item delimiters
  52.     set AppleScript's text item delimiters to "/"
  53.     set allMounts to do shell script "diskutil list | grep /dev"
  54.     set allMounts to result as string
  55.     set allMounts to allMounts as text
  56.     set AppleScript's text item delimiters to origDeLims
  57.    
  58.     set diskList to {}
  59.     repeat with i from 1 to count of paragraphs in allMounts
  60.         set the end of diskList to (allMounts's paragraph i)
  61.     end repeat
  62.     return diskList
  63.    
  64. end getEveryDisk
  65.  
  66.  
  67. on getDMGName(aParagraph)
  68.     set a to items 1 thru 13 in aParagraph as list
  69.     set numList to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
  70.     repeat
  71.        
  72.         if numList contains last item of a then
  73.             exit repeat
  74.            
  75.         else
  76.             set b to the reverse of a
  77.             set a to the rest of b
  78.             set a to the reverse of a
  79.         end if
  80.     end repeat
  81.     set a to a as string
  82.     return a
  83. end getDMGName
  84.  
  85. on compareAndEject(listDmgs, listDisks)
  86.     try
  87.         repeat with i in listDisks
  88.             if i is in listDmgs then
  89.                 try
  90.                     do shell script "diskutil unmountDisk " & i
  91.                 end try
  92.             end if
  93.         end repeat
  94.     end try
  95.    
  96. end compareAndEject
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement