Run Applescript every 15 minutes

Hi, I need to run an applescript every 15 minutes. I have read several posts which suggest iCal, but I haven’t found a way to do this. I have also read a few others that suggest cron, but I don’t know the command to run an applescript from the terminal. Any ideas will be appreciated. Thanks!

OK, just found the solution on another forum. A script is run when setting the alarm on a new event. I would still like more information, if anyone can provide, about running the applescript from the terminal window. I’m a Linux user, and I like to use every chance I have to run stuff from the terminal. Thanks!

Just figured something out, iCal won’t run the script every 15 minutes like I need to, it would run every day, week, etc., so I guess I do have to use crontab. I have scheduled events on a Linux web server using cron, for example, backing up a mysql database, but here I’m quite lost. Any ideas will be appreciated. Thanks!

use the on idle. Create a stay open application. property values are remember, so its simple as checking last time with current time.

property currenttime : 0
property previousbreak : 0 as integer
property currentbreak : 0 as integer
on idle
	set currenttime to time of (current date)
	set currentbreak to (currenttime / 60 / 60 / 4) as integer
	if currentbreak is not equal to previousbreak then
		display dialog "It has been 15 minutes" --Do your script here
		set previousbreak to currentbreak
	end if
end idle

Have fun.

Bevan www.applescript.co.nz

To launch an AppleScript from the terminal you want to take a look at the man page for osascript

as for using cron while you can do that, I would suggest you take a look at using launchd as it is replacing cron.

Running a script every 15 minutes using launchd is the simplest solution if you don’t want a stay-open application. This article will help get you started if you want to go that way.

I tripped out and didn’t wait 15 minutes to test, My above script runs every 4 hours not 15 minutes, you times by 4 not divid

property currenttime : 0
property previousbreak : 0 as integer
property currentbreak : 0 as integer
on idle
	set currenttime to time of (current date)
	set currentbreak to (currenttime / 60 / 60 * 4) as integer
	if currentbreak is not equal to previousbreak then
		display dialog "It has been 15 minutes" --Do your script here
		set previousbreak to currentbreak
	end if
end idle

There’s backgrounder from sentman.com if you want it running in the back ground
http://www.sentman.com/backgrounder/

have fun

To do what you have there, bevos, requires only this much:

on idle
	display dialog "It has been 15 minutes" giving up after 5
	return 15 * 60
end idle

where the return N is the length of time in seconds that the idle handler should wait before firing again. It loafs in the background in between. I the intermittent process takes very long, set the idle time a bit shorter because it starts again when the process before return N finishes. In the example, since the dialog takes 5 seconds if not acknowledged, then you might reduce the return time by that five seconds. This is not a clock, however.

Learn something every day, return is cool. One I do checks the for week days, only run between 7am and 6pm, I guess I could work out the return, save the idle running all the time

Hi bevos,

these coercions to integer are not needed, 0 in a property definition is a integer anyway

I was running a launchd initiated script on the hour, Craig, but went back to an on idle formulation for several reasons: first was that it didn’t fire reliably on the hour, and secondly it seemed to run at such a low priority (no matter how I set that) that it would take much too long to run. Once an on idle script starts, it finishes without interruption.

Craig, I have a question about launchd, how do you indicate when to run it? I read “Getting Started with lauchd” from the developer connection in the Apple website, and I didn’t find how to do this, just one example on how to run a program every day at 3:15. I’m pretty sure you’ve seen this plist, but here is a portion of it so you can see it:

LowPriorityIO

Nice
1
StartCalendarInterval

Hour
3
Minute
15

etc.

I’m imagining that if you place a 0 in the Hour intefer, the program will be run every day at 12:15 a.m.
Also, once you have created the plist file, how to you tell launchd to run it? Do you place something in LaunchAgests? I am completely new to MACs, so I get lost very often. Thanks!

Hi,

you need something like this

[code]<?xml version="1.0" encoding="UTF-8"?>

Label com.myAgent LowPriorityIO Program /usr/bin/osascript ProgramArguments osascript full/path/to/script.scpt StartInterval 900 [/code] StartInterval 900 is run every 15 minutes

I’ll try it out. thanks!

When you have both your .plist and AppleScript files ready to go, you must put them both in their final resting places so you can get the paths correct for them. The .plist file needs to go in a certain folder so it can be automatically loaded at computer startup. (You can name the .plist file anything that you want. Even the .plist file extension is supposedly optional, but I prefer not to tempt fate, so I saved mine as WatchCB.plist.) The folder in which to save the .plist file must be titled LaunchAgents and must be located in your user’s Library folder (yourHD:Users:YOURUSERNAME:Library:). The LaunchAgents folder is not normally installed with the system, so you will need to create it. Once the .plist file is in its folder, it needs to be loaded (from the Terminal) so that it will run forever until you unload it.

To load the agent from the Terminal, you need to use the syntax, launchctl load -w FULLPATHTOTHE.PLISTFILE so that it will run. As long as the .plist file is in your user’s LaunchAgents folder, and the Disabled key is false, it will run every time you start up your machine. The Disabled key is NOT required, so you can ignore it if you wish, and the agent will still load every time you power up the machine.

Good luck,

Thanks Craig. I have created the plist file b modifying the code provided by StefanK. I’ll follow your instructions and let you guys know how it goes, I would like to get this script running automatically as soon as possible. Thanks!

I got the following error when I typed in the command suggested:

M060601:/usr/bin prepress$ launchctl load -w /Users/prepress/Library/LaunchAgents/save-mail.plist
M060601:/usr/bin prepress$ Workaround Bonjour: Unknown error: 0

Any ideas?