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 18, 2013 10:23 AM in response to MacMan240

Why have the program running all day if it is only going to do its thing at midnight? Use launchd to schedule your program to run at the time you want it to. Then you don't need to worry about anyone closing the program.


You don;t mention where the files will be and who they will belong to. Is the machine setup like a kiosk, do all public users use the same login? A simple Applescript will do the actual job of cleaning out the files.


regards


Message was edited by: Frank Caggiano - of course if the public users used the Guest account then any files they create will be automatically deleted when they log out. (I believe)

Feb 18, 2013 10:27 AM in response to Frank Caggiano

I want to have it running all the time because if it randomly opens when someone is using it they may close it or less computer savvy people will be freaking out because the computer is doing this on its own. The files are on the user desktop, there are two users, Admin (Me), and User (Everyone Else), only User should be cleaned. Also, It would be great if this could possible run even if the admin account is the one currently active.

Feb 18, 2013 10:45 AM in response to MacMan240

Not really a concern. If the program is stared by launchd it will be running in the background, the same as all the other things going on on the systrem all the time. It won't 'randomly open' nothing will appear on the screen when the program runs.


Again if it is run by launchd it will be run by the system at the time you setup regardless of who is logged in.


But as everyone is loggign in under one acount like this you should really check out the guest account in Mountain Lion. As I wrote all the files created by the guest user are deleted whrn the guest user logs out.

Feb 18, 2013 11:22 AM in response to MacMan240

Then run the program from launchd at a time or times of your choosing.


Of course doing this (by any method) carries a risk of the program running while someone is creating or just created one of these files. How do you know when it is safe to delete the file, when it is no longer needed?


You may want to have the program delete files older then certain time, say 1 hour.

Feb 18, 2013 12:23 PM in response to MacMan240

Here's a launchd script that should delete files from a given folder that are more than 1 day old, every day at midnight. Save it in /Library/LaunchAgents/ with the file name user.delete.daily.plist. Make sure you set /path/to/folder to the path of the folder you want cleared.


I haven't tested this (in a bit of a rush right now) the way for you to test it is to use the following commands in terminal:


# this line loads the launchd job

launchctl load /Library/LaunchAgents/user.delete.daily.plist


# this line forces the job to trigger immediately rather than waiting until midnight

launchctl start user.delete.daily


# this line unloads the plist, in case there's an error

launchctl unload /Library/LaunchAgents/user.delete.daily.plist

Feb 18, 2013 12:22 PM in response to twtwtw

whoops, forgot to inclue the pist. 😊


<?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>-e</string>

<string>set cd to (current date)</string>

<string>-e</string>

<string>tell application "System Events" to delete (files of folder "/path/to/folder" whose modification date is less than (cd - 1 * days))</string>

</array>

<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>0</integer>

<key>Minute</key>

<integer>0</integer>

</dict>

</dict>

</plist>

Feb 18, 2013 8:53 PM in response to twtwtw

plist isnt working for me


scratch that, it works, but i dont want it randomly deleting old files. Unless you have any other ideas, I would like to to either:

  1. Delete everything that is in one folder
  2. Delete only certain file names
  3. Delete only certain file types
  4. Delete files that are less than 1 day and 1 minute old along with criteria 2 or 3 above.


Message was edited by: MacMan240

Feb 18, 2013 8:56 PM in response to Frank Caggiano

Frank Caggiano wrote:


Then run the program from launchd at a time or times of your choosing.


Of course doing this (by any method) carries a risk of the program running while someone is creating or just created one of these files. How do you know when it is safe to delete the file, when it is no longer needed?


You may want to have the program delete files older then certain time, say 1 hour.


Using applescript, could I display an alert a warning with and a 10 minute delay to prevent that prolem or not? Is that to complicated?

Feb 18, 2013 10:20 PM in response to MacMan240

MacMan240 wrote:


I would like to to either:

  1. Delete everything that is in one folder
  2. Delete only certain file names
  3. Delete only certain file types
  4. Delete files that are less than 1 day and 1 minute old along with criteria 2 or 3 above.

All of those are doable, individually or in combination. Which do you want implemented?

Feb 19, 2013 6:00 AM in response to MacMan240

At this point tw and I have given you a few different ways to attack this problem. Now it is really up to you to decide just want it is you what to do.


Your descriptions of your situation so far have been somewhat hazy. You need to describe what it is you need to do with more detail. Any and all of the things you've asked about can be done. And they all have different ramifications.


So a few questions to help you think about this:

How long after the pdf files are loaded onto this machine by the EMT's are they considered OK to delete? If they are being loaded onto the system they must be used for something when is this something over? And is it always the same for all pdf's?


Are all the pfd's in one place on the system? How many are generated in a period of time say 12hr or 24hr?


When you can delete the files really depends on the answer to my first question. But in any case I wouldn't think you want to delete files that are only some short period of time old.


Finally are you looking for a turn-key solution or are you looking for information to help you solve the problem?


regards

Feb 19, 2013 8:16 AM in response to Frank Caggiano

The PDFs (001.pdf, 002.pdf 003.pdf, etc) are all automatically put on th desktop, and about 99% of the things scanned in is paperwork from EMT/Paramedics. They are submitted to insurance companies or NYS or something, and it is done within about 30 minutes of being scanned, sometimes more sometimes less. There is also always the chance that there was a call at about 22:00 and they are scanning it in around midnight in which case I would like to build in a safety feature (A warning and 10 minute delay upon click "Delay") to prevent a file being used from being deleted. Im really not sure whats frowned upon or hard to do as far as computer/application programming and if any one way is better than another which is why I'm kind of hazy on this, I guess I'm just trying to get information from you guys on what you think would be the best way to attack this. This purely for trying to solve a problem, I doubt I could sell it.


Message was edited by: MacMan240

Feb 19, 2013 8:31 AM in response to MacMan240

Here's the eternal help-desk misunderstanding. You don't know how to approach the problem but have all sorts of information about what your specific problem is. We know how to approach problems like this, but have very little information about your specific needs. Help occurs when we bridge that gap. 🙂


We need to know things like the following:

  • what folder do you want things deleted from?
  • what kinds of files are the files to be deleted (extension, file type, uti, or etc)?
  • what pattern of file names do you want deleted (prefixes, suffixes, keywords, or etc)?

Don't be shy about spelling things out in detail; we like that!

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.