Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (*
- Scripting by Applehelpwriter 2013
- http://applehelpwriter.com
- -------------------------------------
- Version 1.3.2
- Last revised: 24 Jun 22:39
- Change Log
- 1.3.2 - #cosmetic / tidied up some superfluous code (_ln 4)
- 1.3.1 - #advisory / removed explicit call to run handler #caused error in some AS editors
- 1.3 - #cosmetic / improved dialog box messages
- 1.2 - #critical / added 'sort' pipe to _statCommand
- ------------------------------------
- This script will erase the volume called "Temporary Drive" if it has not been used for 24 hours or more.
- This script requires confirmation and authentication by an admin user
- WARNING: *************
- 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.
- WARNING: *************
- ---------------------------------
- *)
- try
- 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. *)
- set _ln to 1
- set _statCommand to "stat -f " & "\"%a%t\"" & " /Volumes/" & _volName & "/* | sort -rn | head -1"
- set _ln to 2
- set _lastAccess to (do shell script _statCommand)
- if _lastAccess is greater than or equal to 1 then
- doNow(_lastAccess, _volName)
- else
- display dialog "The volume " & "‘" & _volName & "‘" & " appears to be empty or does not exist." buttons "OK" default button {"OK"} with title "Erase cancelled"
- end if
- on error errorMessage number errorNumber
- error1(_ln, errorMessage, errorNumber)
- end try
- on doNow(_lastAccess, _volName)
- try
- set _ln to 3
- set _now to do shell script "date +%s"
- set _ln to 4
- set _check to ((_now - _lastAccess) / 60 / 60)
- set _ln to 5
- if _check is greater than 23 then
- eraseVolume(_volName, _check)
- else
- 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"
- end if
- on error errorMessage number errorNumber
- error1(_ln, errorMessage, errorNumber)
- end try
- end doNow
- on eraseVolume(_volName, _check)
- try
- set _ln to 6 -- delete all the code between 'set _ln to 6' and 'set _ln to 7' to remove this confirmation
- 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?"
- set _ln to 7
- try
- do shell script "/usr/sbin/diskutil eraseVolume HFS+ " & _volName & " /Volumes/" & _volName with administrator privileges
- display dialog result buttons "OK" default button {"OK"} with title "Erase complete"
- on error
- error2(_volName)
- end try
- on error errorMessage number errorNumber
- error1(_ln, errorMessage, errorNumber)
- end try
- end eraseVolume
- on error1(_ln, errorMessage, errorNumber)
- log ("(ln " & _ln & ") errorMessage: " & errorMessage & ", errorNumber: " & errorNumber)
- display dialog ("(ln " & _ln & ") errorMessage: " & errorMessage & ", errorNumber: " & errorNumber) with icon 2
- end error1
- on error2(_volName)
- display dialog _volName & " was not erased - check that the volume is not in use if you did not cancel the operation." with icon 2
- end error2
- --EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement