• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?


  • Subject: Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?
  • From: 2551 <email@hidden>
  • Date: Sun, 23 Jun 2013 01:28:56 +0700

This requires plenty of testing, but appears to work on my own machine (running 10.8.4). It contains one confirmation and one authentication check. You can remove the first (instructions are in the code), I wouldn't advise removing the second, not until you're absolutely 100% sure it works without error. After considerable testing (I'd say at least a few weeks), you could post back (or google) how to avoid the request for the admin password. Please use a backup for your drive while you're testing this, or you risk losing data you didn't want to lose.

I've put lots of debugging stuff in here, so if it generates an error please select, copy and paste the entire error message into an email and post back.


Enough dire warnings, enjoy!

Best

Phil
http://applehelpwriter.com


--START
(*
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



--EOF






On 21 Jun 2013, at 03:22, Jim Kessler <email@hidden> wrote:

Here's the scenario: a Mac with a hard drive is partitioned into two volumes, one of which is the startup drive (Macintosh HD). The second volume (Temporary Drive) holds data files which are non-critical. I'd like to compose an AppleScript that compares (running at startup, through a login item) the current date to the modification date of the second volume, and erases the second volume if the modification date is more than a day (or 24 hours, or whatever measure of time necessary) older than the current date. Is this easily accomplished through AppleScript? (Running 10.8.4.)

Thanks!
Jim



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?
      • From: 2551 <email@hidden>
References: 
 >AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: Jim Kessler <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: Kaydell Leavitt <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: "koenig.yvan" <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: 2551 <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: Kaydell Leavitt <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: 2551 <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: Kaydell Leavitt <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: Brian Christmas <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: Kaydell Leavitt <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: 2551 <email@hidden>)
 >Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day? (From: 2551 <email@hidden>)

  • Prev by Date: Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?
  • Next by Date: Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?
  • Previous by thread: Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?
  • Next by thread: Re: AppleScript to erase one mounted volume if it's modification date is date is greater than one day?
  • Index(es):
    • Date
    • Thread