Help: Applescript Triggered by Incoming Email Rule Cannot Select...

I set up Mail to constantly monitor an email address. Anytime it receives an email, I want it to trigger an applescript that will grab the subject and content info in the email and copy to Filemaker. I created an applescript that does this:

tell application “FileMaker Pro Advanced”
activate
delay 5
end tell

tell application “Mail”
set theSelection to every message of mailbox “INBOX” of account 1
set theMessage to item 1 of theSelection
set theSubject to subject of theMessage
set theContect to content of theMessage
end tell

tell application “FileMaker Pro Advanced”
activate

if (exists database "Tasks.FP7") then
	go to database "Tasks.FP7"
else
	getURL "fmp7://192.168.0.101/Tasks.fp7"
	delay 5
end if

tell database "Tasks"
	tell table "Requests"
		create record
		tell last record
			set cell "title" to theSubject
			
			set cell "Assigned To" to "Michael"
			
			set cell "Notes" to theContect
			
		end tell
		
	end tell
	
end tell

end tell

tell application “Mail”
activate
set theSelection to every message of mailbox “INBOX” of account 1
set theMessage to item 1 of theSelection
delete theMessage
end tell

However, here is the problem… the incoming emails do not get placed in the Inbox until AFTER the applescript has completed execution. Therefore, there is no way for me to SELECT the incoming email in order to get the subject and content info. I tried adding delays to the applescript, but still, the incoming email will not appear in the Inbox until the applescript has finished - nomatter how long the delays.

I need to figure out how to select the incoming email after it arrives, but before it gets placed in the Inbox. Or I need to figure out a way to delay the applescript until Mail completes the receive process.

Hello and welcome to MacScripter :slight_smile:

Have a look of the structure of the sample rule action script which was delivered with Mail in SL


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			set theText to "This AppleScript is intended to be used as an AppleScript rule action, but is also an example of how to write scripts that act on a selection of messages or mailboxes." & return & return & "To view this script, hold down the option key and select it again from the Scripts menu."
			repeat with eachMessage in theMessages
				set theSubject to subject of eachMessage
				try
					-- If this is not being executed as a rule action,
					-- getting the name of theRule variable will fail.
					set theRuleName to name of theRule
					set theText to "The rule named '" & theRuleName & "' matched this message:"
					set theText to theText & return & return & "Subject: " & theSubject
					display dialog theText
					set theText to ""
				end try
			end repeat
			if theText is not equal to "" then
				display dialog theText buttons {"OK"} default button 1
			end if
		end tell
	end perform mail action with messages
end using terms from

You must use this structure in order to make an an AppleScript execute when a mail rule is triggered.
The on perform mail action is rather mandatory.

-And of course you must attach it to the mail rule.

Best Regards

McUsr