Script to check a file periodically

I am just starting my foray into Applescript. I have a particular file that I want to check each day so I decided to write a script that will open it at a certain time each day so I will remember to check it. To do this I think I need several elements to the script: it has to always be running, it has to check the current time, if the time meets the parameter, it should open the file or otherwise continue checking periodically.

I think I have the checking time and opening file part working. My main problem is how to keep the script running 24/7 so it can check the time, and how to have it check the time on its own. (the script works if I manually run it, but it won’t check the time on its own.) Below is the script I have so far. Can anyone help me complete the script?

tell application "Finder"
	if time string of (current date) is greater than "9:00:00 PM" and time string of (current date) is less than "10:00:00 PM" then
		open file "Macintosh HD:Users:BrianFlynn:Documents:Lists:CECS 2004 Goals/Projects.ooutline"
	else --this else statement is here to assure me it is working--
		say "It is not time to open the file" using "Victoria"
	end if
end tell

P.S. I just discovered that AppleScript does not seem to recognize AM and PM. Is there a better way to specify the time?

Maybe this will work. Save it as a stay open application and, once launched, it will check the time once every hour. Note that the check might not fall at the top of the hour.

on idle
	set curr_date to (current date)
	set begin_time to date ((date string of curr_date) & " 9:00:00 PM")
	set end_time to date ((date string of curr_date) & " 10:00:00 PM")
	
	if curr_date > begin_time and curr_date < end_time then
		tell application "Finder"
			open file "Macintosh HD:Users:BrianFlynn:Documents:Lists:CECS 2004 Goals/Projects.ooutline"
		end tell
	else --this else statement is here to assure me it is working-- 
		say "It is not time to open the file" using "Victoria"
	end if
	return (1 * hours) -- check once every hour (3600 seconds)
end idle

In your original script, the time comparison will fail because the script is comparing strings of text and not time.

You might want to consider using iCal for this task (might require v1.5.2 to be reliable). You could set up a repeating event to open the file at 9 PM every day. This would be easier to implement and less demanding on resources since iCal doesn’t even need to be running for it to work. :slight_smile:

– Rob

Frankly, I would not use the script to do the time stuff. I’d use OS X’s built-in timer process, “cron”. A freeware utility, “cronnix”, gives you a graphical interface to set up items. Just have a script that opens the file, and schedule it to run every ten minutes between the hours you want. Or, for that matter, I think you might be able to set up a file open in cron itself.

If you insist on using the script to do it all, I can offer a partial answer: to have a stay-open application, use “save as”, save it as an application, and check the “stay open” checkbox. You’ll need an “on idle” handler to cause the recurring runs. Check out Bill Cheeseman’s article, “Idle thoughts” on his website:

http://www.applescriptsourcebook.com/tips/idlethoughts.html

Thank you both for your suggestions. I will have to look at the cronix application some time. In the meantime, I considered your suggestion Rob to use iCal. I use Entourage instead of iCal, so I did some checking. I was able to create a timed schedule in Entourage that will open a file at a specified time. It worked, so problem solved.

Nonetheless, this was an exercise to help me learn AppleScript a little. I will continue to play with the code you sent to see if it works. The next project is to create a backup script that will copy folders to my iPod on a timer.