Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Run an applescript at certain time?

On a public computer for the company I work for, people scan things in to the computer and never delete them. The boss wants the files (001.pdf, 002.pdf, 003.pdf etc.) cleared at midnight every night. To do this I decided to make a Cocoa-Applescript app that will start on start up, and stay running all day and clear files at midnight. right now, I'm not sure how to do the following things and would like suggestions on how to do it:

  1. Have an applescript run at 0:01 evey day
  2. Keep non admin users from closing the app
  3. Select and perminently delete files in the backround, requireing no current user input

Please help.

MacBook Pro, OS X Mountain Lion (10.8.2), 2.3 GHz i5, 4GB RAM, OCZ 120GB SSD

Posted on Feb 18, 2013 10:11 AM

Reply
44 replies

Feb 19, 2013 6:48 PM in response to MacMan240

Eh, never mind, I think this will work:


tell application "System Events"


-- find all pdf files that were created in the last 24 hours

set questionableFiles to files of desktop folder whose type identifier is "com.adobe.pdf" and creation date > ((current date) - 1 * days)


-- loop through files, deleting files whose name is all numbers

repeat with thisFile in questionableFiles

set mainName to text 1 thru -5 of (get name of thisFile)

try


-- if the file name has any non-numbers, this will throw an error and skip the delete

mainName as integer

deletethisFile

end try

end repeat

end tell

save this as a script file somewhere convenient, then change the plist I gave above to read as follows (all that's changed is the ProgramArguments key, which now will point to an external file rather than build the script internally):


<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>Label</key>

<string>user.delete.daily</string>

<key>ProgramArguments</key>

<array>

<string>osascript</string>

<string>/path/to/script file.scpt</string>

</array>

<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>0</integer>

<key>Minute</key>

<integer>0</integer>

</dict>

</dict>

</plist>

Feb 19, 2013 6:52 PM in response to MacMan240

MacMan240 wrote:


Numerical name only please, the paramedics also have instructional pdfs on the desktop as well.

Oh man you really need to take a step back and set this up in a more sane manner. Having both temporary files and permanent files in the same folder (and having that folder be the Desktop) with the same file extension is not a good thing. At some point some or all of the instructional pdfs will get clobbered.


Why not at least set up two folders one for the instructional pdfs and one for the temporary ones. It will make your life a lot easier in the long run.

Feb 20, 2013 12:34 PM in response to twtwtw

twtwtw wrote:


since you have control of the scanner, can you add an identifying label to the name? If the file names were something like "Temporary Scan 001.pdf" that would make life easier.

all scans will now have a prefix "face_sheet_" followed by a serial "001, 002, 003, etc" to make "face_sheet_001.pdf, face_sheet_002.pdf, face_sheet_003.pdf, etc"

Feb 20, 2013 1:59 PM in response to MacMan240

ok, then you can simplify the script I gave above to read so:


tell application "System Events"


-- find all pdf files that were created in the last 24 hours that start with face_sheet

delete (files of desktop folder whose type identifier is "com.adobe.pdf" and creation date > ((current date) - 1 * days) and name starts with "face_sheet_")

end tell

Feb 20, 2013 2:21 PM in response to MacMan240

what exactly are you trying to accomplish with this? If it's just to keep from deleting files that are being worked on at the moment, a better solution would be this, where you offset deletion by an hour:


tell application "System Events"


-- find all pdf files that were created between 1 hour and 26 hours ago that start with face_sheet

set lowerLimit to ((current date) - 1 * days - 2 * hours)

set upperLimit to ((current date) - 1 * hours)

delete (files of desktop folder whose type identifier is "com.adobe.pdf" and creation datelowerLimit and creation date < lowerLimit and name starts with "face_sheet_")

end tell


This won't touch any files newer than an hour old, but will catch them the next day if they're still there. (I used 2 hours on the lower limit for overlap, so you don't get some weird situation where a file is created exactly on the hour and never gets deleted).

Feb 20, 2013 2:26 PM in response to MacMan240

Thinking about overlaps, I should point out that if the computer happens to be off or asleep at midnight, the script will run the next time the computer's activated. However, it only looks back a day and 2 hours; if the machine isn't reactivated until (say) 6am there's a four hour window where the script might miss files. Probably not an issue, but...

Feb 20, 2013 2:30 PM in response to Frank Caggiano

Frank Caggiano wrote:


Oh man you really need to take a step back and set this up in a more sane manner. Having both temporary files and permanent files in the same folder (and having that folder be the Desktop) with the same file extension is not a good thing.


Frank has a good point here. You might want to take this advice to heart.

Run an applescript at certain time?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.