Run an Applescript in the background every 30 seconds

Hi there,

I am trying to run an apple script once every 30 seconds and I am completely lost.

Here is the script:

tell application "Mail"
	set unreadBox to mailbox "[Gmail]/All Mail" of account "Gmail"
	set unreadCount to unread count of unreadBox
	if unreadCount is not 0 then
		repeat unreadCount times
			set unreadMail to first message of unreadBox whose read status is false
			set read status of unreadMail to true
		end repeat
	end if
end tell

[Applescript created by Wes G - http://www.wesg.ca]

Thanks for the help!

If you are simply trying to set all unread messages of one account to read, you can do that with a Mail rule.

Craig is right for the task you’ve specified, but in general, for tasks to be repeated at set intervals, you use the following format:

(*An "on idle" script, saved as a stay-open application will run at specified time intervals. Often used to "watch" something and respond.*)

on run
	(*do setup - this runs when the application is started to set things up. It is not necessary to have an 'on run' handler because the on idle handler runs immediately after this anyway. If you want this to run all the time after every login, list it your startup items. You quit it from it's dock icon.*)
end run

(*'on idle' runs immediately following the 'run' handler if there is one, and thereafter on the interval specified in the return at the end. (An 'on run' handler is not required, so omit if there is no setup)*)
on idle
	-- In this section, you do your script operations every interval
	return xx -- do this every xx *seconds*.
end idle

on quit
	(*do stuff - assumes you want to clean things up or save preferences or change properties before the program quits. This section runs only if the application is quit from the dock, or by any other means. It is not necessary to have one.*)
	
	(*if there is an on quit handler, then 'continue quit' must be the last statement or the script will not stop! The presence of an 'on quit' handler 'grabs' the system command to stop, so it will not complete the quit process until notified to do so. 'continue quit' does that.*)
	continue quit
end quit

Hey, grajohnson25

just yesterday i’ve written a similar script: enjoy!

--Mail check

property minutesBetweenSaves : 5

on run
	launch application "Mail"
end run

on idle
	tell application "Mail"
		check for new mail for mailbox 1 of account "Gmail"
		if unread count of mailbox 1 ≥ 1 then open message 1 of mailbox 1 of account "Gmail"
	end tell
	return minutesBetweenSaves * 60
end idle

on quit
	quit application "Mail"
	continue quit
end quit



Edit:
OOoops… i use Mail not so often…
there is no need for a script to check new emails. You’ve only to adjust the parameters -for incoming email- in the preferences of Mail itself…