Delays, sleeping and CPU usage in Applescript

We’re using Macs as a kiosk here at the place that I work for customers that come into our branches. We noticed that people would leave personal documents they downloaded from the internet on the machines and we don’t have the manpower to constantly check these machines, so I started playing with Applescript.

The users are in Simple Finder mode with two Applescripts that run - the first just automatically opens the “My Applications” folder for easy use. The second is a little more complicated.

Basically, what I want the script to do is delete everything from the desktop as well as the documents folder every 5 minutes. Here is the script that I’m using:

tell application "Finder"
try
delete (every item of folder "Desktop" of folder "kioskuser" of folder "Users" of folder "Macintosh HD")
end try
try
delete (every item of folder "Documents" of folder "kioskuser" of folder "Users" of folder "Macintosh HD")
end try
empty trash
delay 300
end tell

Now, the script works perfectly fine. It deletes everything like it needs to and it empties the trash. The problem is this - every time the script runs, it basically causes the computer to seize. Someone mentioned trying to run the shell script “sleep”, but when I modified the app, it halted the computer immediately and gave me a “script error”, then proceeded to freeze the Finder.

Ideally, instead the leaving the app open and having it run on a delay, I’d like for it to open on a set interval, run, and then close itself. But I’m pretty sure that would have to be a script that run as a cron job, but I’m completely lost when it comes to cron scripting.

Does anyone have any suggestions on what I need to do to get this script to play nice?

  1. Save your script code as an application without the delay command included
  2. Create a text file (e.g. with TextWranger) with the following contents:

*/5 * * * * ‘/path to /your/applescript/as/posix/path/’

  1. Open a Terminal window and enter the following:

crontab

  1. Then drag the text file into the open Terminal window and hit enter

Now your AppleScript should be excuted every 5 minutes.

Doing this several times a day :smiley:

I’d have to set up the cron job under the Administrator account - would it still run the job when a user other than Administrator is logged in?

Why not running the script from the kioskuser’s crontab?

I just went ahead and tried it under the Administrator account and under the kioskuser account. I created a rtf file using Textedit called cronjob with the contents of:

/5***‘/HD/DeleteDocs.app’

When I did a crontab, I get this error message:

/Users/user/Documents/cronjob.rtf:1: bad day-of-month
crontab: error in crontab file, can’t install

Even if I run crontab with a sudo and -u trigger, I get the same error.

Hi,

Don’t use a RTF file, but rather plain text. Moreover I do not see spaces between your stars (*). And it should read ‘/Volumes/HD/DeleteDocs.app’.

Hope that helps.

That seemed to have worked.

Thank you for all the help!

I seem to have spoken too soon. The crontab took the file with no problem, but nothing actually happens. The app never runs.

If I run the app manually, it works perfectly fine. There is no mail for the system.

Another small edit - I went into the terminal and did a crontab -l and the cron job is indeed listed, so I know it’s there. Just don’t know why it’s not running.

Hi,

Try to use the open command before the script path, I also use this in our crontabs:

*/5 * * * * open '/path to /your/applescript/as/posix/path/

Still doesn’t appear to be working :frowning:

I’m 99.9% certain my pathing is correct, because if I do the last section of the job (open ‘/path/to/.app’) from the Terminal, it runs just fine. Someone mentioned that I might need to make a cron.allow file?