Skip navigation

Run an applescript at certain time?

6797 Views 44 Replies Latest reply: Dec 14, 2013 7:38 AM by dickguertin RSS
1 2 3 Previous Next
MacMan240 Level 1 Level 1 (80 points)
Currently Being Moderated
Feb 18, 2013 10:11 AM

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
  • Frank Caggiano Level 7 Level 7 (22,760 points)
    Currently Being Moderated
    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.




    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)

  • kikashi Calculating status...
    Currently Being Moderated
    Feb 18, 2013 10:29 AM (in response to Frank Caggiano)

    Or you could use Unix's built-in scheduler, cron (try man cron in Terminal). There are several GUIs for building the table of instructions for cron: for example, Cronnix at I've not tried it myself (I just edit the file).



  • Frank Caggiano Level 7 Level 7 (22,760 points)
    Currently Being Moderated
    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.

  • Frank Caggiano Level 7 Level 7 (22,760 points)
    Currently Being Moderated
    Feb 18, 2013 10:47 AM (in response to kikashi)

    In OS X launchd is the way to start programs on schedule. Cron is depreciated.


    While cron still exists on OS X it is just a front end to launchd.

  • Frank Caggiano Level 7 Level 7 (22,760 points)
    Currently Being Moderated
    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.

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    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

  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    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" "">

    <plist version="1.0">








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


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











  • twtwtw Level 5 Level 5 (4,580 points)
    Currently Being Moderated
    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?

  • Frank Caggiano Level 7 Level 7 (22,760 points)
    Currently Being Moderated
    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?



1 2 3 Previous Next


More Like This

  • Retrieving data ...

Bookmarked By (3)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.