1 2 3 Previous Next 44 Replies Latest reply: Dec 14, 2013 7:38 AM by dickguertin
MacMan240 Level 1 Level 1 (80 points)

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
  • 1. Re: Run an applescript at certain time?
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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)

  • 2. Re: Run an applescript at certain time?
    MacMan240 Level 1 Level 1 (80 points)

    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.

  • 3. Re: Run an applescript at certain time?
    kikashi Level 1 Level 1 (10 points)

    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 http://www.macupdate.com/app/mac/7486/cronnix. I've not tried it myself (I just edit the file).

     

    Jeremy

  • 4. Re: Run an applescript at certain time?
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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.

  • 5. Re: Run an applescript at certain time?
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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.

  • 6. Re: Run an applescript at certain time?
    MacMan240 Level 1 Level 1 (80 points)

    I don't want all the files gone though, I just want to make sure that the EMTs and Paramedics are deleting the charts they scan in, and if not, the program deletes them.

  • 7. Re: Run an applescript at certain time?
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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.

  • 8. Re: Run an applescript at certain time?
    twtwtw Level 5 Level 5 (4,690 points)

    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

  • 9. Re: Run an applescript at certain time?
    twtwtw Level 5 Level 5 (4,690 points)

    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>

  • 10. Re: Run an applescript at certain time?
    MacMan240 Level 1 Level 1 (80 points)

    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

  • 11. Re: Run an applescript at certain time?
    MacMan240 Level 1 Level 1 (80 points)

    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?

  • 12. Re: Run an applescript at certain time?
    twtwtw Level 5 Level 5 (4,690 points)

    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?

  • 13. Re: Run an applescript at certain time?
    Frank Caggiano Level 7 Level 7 (23,830 points)

    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

  • 14. Re: Run an applescript at certain time?
    MacMan240 Level 1 Level 1 (80 points)

    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

1 2 3 Previous Next