(*
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.
*)
on run
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 if any *)
set _ln to 1
set _statCommand to "stat -f " & "\"%a%t\"" & " /Volumes/" & _volName & "/* | head -1"
set _ln to 2
set _lastAccess to (do shell script _statCommand)
if _lastAccess is greater than 1 then
doNow(_lastAccess, _volName)
else
display dialog "Volume appears to be empty."
end if
end try
end run
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)
set _ln to 5
set _check to (_check / 60 / 60)
set _ln to 6
if _check is greater than 23 then
eraseVolume(_volName, _check)
end if
on error errorMessage number errorNumber
error1(_ln, errorMessage, errorNumber)
end try
end doNow
on eraseVolume(_volName, _check)
try
set _ln to 7 -- delete all the code between 'set _ln to 7' and 'set _ln to 8' 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"
set _ln to 8
try
do shell script "/usr/sbin/diskutil eraseVolume HFS+ " & _volName & " /Volumes/" & _volName with administrator privileges
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." with icon 2
end error2