Need help with Idle

I am trying to get the contents of an email when it arrives. I the script is activated by a rule in the Mail app. The problem is the script goes before he email has actually been downloaded. I tried to keep running my script until the mail shows up, but the script takes up so much resources that Mail hangs. Here is my script could someone help with it.

global myMsgList
on getMsg()
	tell application "Mail"
		set myMailbox to mailbox "XMLFiles"
		set myMsgList to every message of myMailbox
		return count (myMsgList)
	end tell
end getMsg
repeat while getMsg() is less than 1
	--do something
end repeat

I know this is no the right way to go about it, but what is?

In the Applescript language guide it says if you use this code my Mac will beep every thirty seconds.

on idle
beep
end idle

however it does nothing when I press run. I assume this is because it is not waiting on anythings. So how do I make the script idle until the Mail app. has downloaded the message?

I would (were it me) always explicitly give the return time. So something like

on idle
beep
return 30
end idle

In normal AppleScript for the idle handler to get called the app has to stay running after the run handler finishes so it has to be saved as Stay Open…

Having a loop without even a pause will tie up resources, thats a fact.

How about something like…

global myMsgList

on idle
if getmsg() is not 0 then do_something()
return 30m – I might make this more like 120…who is in such a rush
end idle

on getMsg()
tell application “Mail”
set myMailbox to mailbox “XMLFiles”
set myMsgList to every message of myMailbox
return count (myMsgList)
end tell
end getMsg

on do_something()

– something really cool

end do_something