SHARE
TWEET

erase volume not in use for 24hrs

applehelpwriter Jun 22nd, 2013 155 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (*
  2. Scripting by Applehelpwriter 2013
  3. http://applehelpwriter.com
  4.  
  5. -------------------------------------
  6. Version 1.3.2
  7. Last revised: 24 Jun 22:39
  8.  
  9. Change Log
  10. 1.3.2 - #cosmetic / tidied up some superfluous code (_ln 4)
  11. 1.3.1 - #advisory / removed explicit call to run handler #caused error in some AS editors
  12. 1.3 - #cosmetic  / improved dialog box messages
  13. 1.2 - #critical / added 'sort' pipe to _statCommand
  14. ------------------------------------
  15.  
  16. This script will erase the volume called "Temporary Drive" if it has not been used for 24 hours or more.
  17. This script requires confirmation and authentication by an admin user
  18.  
  19. WARNING: *************
  20. This script is for test purposes only and any consequences of its use are solely the responsibility of the user. NEVER test this script on a volume that contains data of any value without first making a backup.
  21. WARNING: *************
  22.  
  23. ---------------------------------
  24. *)
  25.  
  26. try
  27.         set _volName to "Temporary\\ Drive" (* You should be able to replace the volume name with any other disk name here, but be sure to include the \\ before any spaces in the name. *)
  28.        
  29.         set _ln to 1
  30.         set _statCommand to "stat -f " & "\"%a%t\"" & " /Volumes/" & _volName & "/* | sort -rn | head -1"
  31.        
  32.         set _ln to 2
  33.         set _lastAccess to (do shell script _statCommand)
  34.        
  35.         if _lastAccess is greater than or equal to 1 then
  36.                 doNow(_lastAccess, _volName)
  37.         else
  38.                 display dialog "The volume " & "‘" & _volName & "‘" & " appears to be empty or does not exist." buttons "OK" default button {"OK"} with title "Erase cancelled"
  39.         end if
  40.        
  41. on error errorMessage number errorNumber
  42.         error1(_ln, errorMessage, errorNumber)
  43. end try
  44.  
  45.  
  46. on doNow(_lastAccess, _volName)
  47.         try
  48.                 set _ln to 3
  49.                 set _now to do shell script "date +%s"
  50.                
  51.                 set _ln to 4
  52.                 set _check to ((_now - _lastAccess) / 60 / 60)
  53.                
  54.                 set _ln to 5
  55.                
  56.                 if _check is greater than 23 then
  57.                         eraseVolume(_volName, _check)
  58.                 else
  59.                         display dialog _volName & " has been used in the last 24 hours." & return & return & "The volume will NOT be erased." buttons "OK" default button {"OK"} with title "Erase cancelled"
  60.                        
  61.                 end if
  62.                
  63.                
  64.         on error errorMessage number errorNumber
  65.                 error1(_ln, errorMessage, errorNumber)
  66.         end try
  67. end doNow
  68.  
  69.  
  70.  
  71. on eraseVolume(_volName, _check)
  72.         try
  73.                
  74.                 set _ln to 6 -- delete all the code between 'set _ln to 6' and 'set _ln to 7' to remove this confirmation
  75.                 display dialog "The disk " & _volName & " appears to have been last used " & return & return & _check & " hour(s) ago" & return & return & "Would you like to erase it now?" default button "Cancel" with title "Confirm erase?"
  76.                
  77.                 set _ln to 7
  78.                 try
  79.                        
  80.                         do shell script "/usr/sbin/diskutil eraseVolume HFS+ " & _volName & " /Volumes/" & _volName with administrator privileges
  81.                         display dialog result buttons "OK" default button {"OK"} with title "Erase complete"
  82.                 on error
  83.                         error2(_volName)
  84.                 end try
  85.                
  86.         on error errorMessage number errorNumber
  87.                 error1(_ln, errorMessage, errorNumber)
  88.                
  89.         end try
  90. end eraseVolume
  91.  
  92.  
  93. on error1(_ln, errorMessage, errorNumber)
  94.         log ("(ln " & _ln & ") errorMessage: " & errorMessage & ", errorNumber: " & errorNumber)
  95.         display dialog ("(ln " & _ln & ") errorMessage: " & errorMessage & ", errorNumber: " & errorNumber) with icon 2
  96. end error1
  97.  
  98. on error2(_volName)
  99.         display dialog _volName & " was not erased - check that the volume is not in use if you did not cancel the operation." with icon 2
  100. end error2
  101. --EOF
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top