Automatically Quit Application in Background if it's not Active

I feel like this should be pretty easy, but I’m having trouble getting it implemented properly. Basically, I want Address Book to close if it isn’t the frontmost application (e.g. it’s not actively is use), and I want the computer to check every 60 seconds or so.

Why? Because I am using Snow Leopard Server, which does not allow several users to share an editable address book. The workaround is to have all the users sharing one account so that we can all access and modify the same address book. However, data integrity has been pretty poor since Address Book doesn’t seem to update the server properly, and it seems that ABs left open on other computers will erase the work done by someone else. I think the solution is to have ABs automatically close after a minute of being in the background so that as few ABs are open as possible. This way, only if someone is actively using it will the database be used.

So, I have this so far:

tell application “System Events”
if exists (some process whose name contains “Address Book”) then
set theAddressBookApp to some process whose name contains “Address Book”
else – Address book is not running
return
end if

if frontmost of theAddressBookApp is false then
	tell application "Address Book" to quit
end if

end tell

I think that will work…but what’s the best way to set a timer? Should I use a cron job, or should I have applescript act as the timer?

Is there a better way to accomplish my goal?

Thanks,

Bhavesh

This script checks if Address Book is active, when so it adds 1 to theLoops. When theLoops is greater than or equal to 60 it quits Address Book. When Address Book is not active then theLoops is set to 0.

property theLoops : 0
property addressBookIsActive : false


on idle
	tell application "System Events"
		set addressBookIsActive to true
		if exists (some process whose name contains "Address Book") then set adressBookIsActive to (frontmost of process (name of some process whose name contains "Address Book"))
		if addressBookIsActive is false then set theLoops to theLoops + 1
		if addressBookIsActive is true then set theLoops to 0
		if theLoops > 59 then tell application "Address Book" to quit
	end tell
	
	return 1
end idle

hope it works,
ief2