getting a script to work in Mail rules

I have a script that works fine in Script Editor or as a stand alone script. I would like to get it to work from within Mail rules but for some reason it doesn’t want to. I have tried placing hte script within the brackets of

and

which I was thought to believe enabled script to work with Mail rules. If aynone has any ideas how to resolve this I would love to hear from them.

My script is as follows.


tell application "Mail"
set the_message to item 1 of (get selection)
set theSubject to the subject of the_message
if theSubject is "Nouvelle Réservation" then
set the_message to item 1 of (get selection)
set message_text to content of the_message as string
set nom to paragraph 8 of message_text
set prenom_script to paragraph 9 of message_text
set spectacle_script to paragraph 2 of message_text
set date_script to paragraph 3 of message_text
set no_de_places_script to paragraph 5 of message_text
end if
end tell
tell application "FileMaker Pro Advanced"
activate
open file "xxx:Users:yyy:Documents:zzz:fichiers fp7:Import reservations script.fp7"
try
delete every record
end try
set newRecord to create record -- store the result as 'newRecord'
tell newRecord -- now target that new record
set cell "nom_script" to nom
set cell "prenom_script" to prenom_script
set cell "spectacle_script" to spectacle_script
set cell "date_script" to date_script
set cell "no_de_places_script" to no_de_places_script
do script "whole import process into «¢_reservations»"
end tell
end tell 

Hello.

I think you should insert some try - on error e number n - end try blocks, together with some tell me ” activate ” display dialog e & " : " & n ” end tell blocks inside the “on error clause” and configure the script to be run as a mail message again, and watch what may pop up.

And pardon me for mentioning it, but you must of course install it in mail rule.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		-- set the_message to item 1 of (get selection)
		try
			set theSubject to the subject of the_message
			if theSubject is "Nouvelle Réservation" then
				set the_message to item 1 of (get selection)
				set message_text to content of the_message as string
				set nom to paragraph 8 of message_text
				set prenom_script to paragraph 9 of message_text
				set spectacle_script to paragraph 2 of message_text
				set date_script to paragraph 3 of message_text
				set no_de_places_script to paragraph 5 of message_text
			end if
		on error e number n
			tell me
				activate
				display alert "Mail block : " & e & " : " & n
			end tell
		end try
		try
			tell application "FileMaker Pro Advanced"
				activate
				open file "xxx:Users:yyy:Documents:zzz:fichiers fp7:Import reservations script.fp7"
				try
					delete every record
				end try
				set newRecord to create record -- store the result as 'newRecord'
				tell newRecord -- now target that new record
					set cell "nom_script" to nom
					set cell "prenom_script" to prenom_script
					set cell "spectacle_script" to spectacle_script
					set cell "date_script" to date_script
					set cell "no_de_places_script" to no_de_places_script
					do script "whole import process into «¢_reservations»"
				end tell
			end tell
		on error e number n
			tell me
				activate
				display alert "File Maker pro advanced block : " & e & " : " & n
			end tell
		end try
	end perform mail action with messages
end using terms from

Thanks for that. That seems to ensure that the script is triggered, however what’s weird is that the script works fine by itself but when it is launched via Mail rules, it gets stuck in the Filemaker part of it around here
“try
delete every record
end try”

Any ideas as what could be impeding it from advancing just because it’s launched from Mail rules?

regards
gd

Hello…

think it is because it is run like it is, from within a mail script. The other context you are running the script in are more forgiving. You should try to assign the open file to some variable, then tell that file to delete its records within a tell block.

Edit I took out the Advanced of FileMaker Pro Advanced block in order to make it compile on my machine. Try the script now.

Hello.

See the post above

Hello,

Thanks for all that once again. I tried you proposition but for the moment it has got me into a bit of a bind. Mail now displays a dialogue box saying

That would be so bad except that the OK button on the message doesn’t function and so Mail seems to be caught in a never ending loop. And as the OK button doesn’t work I can’t access preferences to disable the script. Any ideas on either how to disactivate the script and/or how to diagnose it to make it work?

regards

GD

Hi,

I resolved the never ending loop by accessing my mail account via my provider’s site and deleting the incoming message that the script couldn’t resolve. So that’s no longer a problem, however the problem with the script remains. Any helps would be much appreciated.

GD

Hello. It would be interesting if you could disable the script (at least for a short while be hitting command period (.), then hitting command comma, so that you would get to the preferences, and disable your mail rule. If necessary, kill mail by cmd option escape. You need to test if there are any messages to be processed before starting to executing the script.

I’ll come back to you with a fix if you manage to stop the rule. I might se if I can program the rule away, but then I need to know the name of the mail rule.

Hi,

if you create a mail rule with the perform mail action event handler, you should not use the selection command.
The variable theMessages contains a list of the currently received messages.
Use a repeat loop to process the messages one by one.
The activate statement in the Filemaker block is actually not needed.
Omit also the display dialog statements, mail rules don’t support user interaction


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		my initFilemaker()
		repeat with oneMessage in theMessages
			set theSubject to the subject of oneMessage
			if theSubject is "Nouvelle Réservation" then
				set message_text to content of oneMessage
				set nom to paragraph 8 of message_text
				set prenom_script to paragraph 9 of message_text
				set spectacle_script to paragraph 2 of message_text
				set date_script to paragraph 3 of message_text
				set no_de_places_script to paragraph 5 of message_text
			end if
			tell application "FileMaker Pro Advanced"
				set newRecord to create record -- store the result as 'newRecord'
				tell newRecord -- now target that new record
					set cell "nom_script" to nom
					set cell "prenom_script" to prenom_script
					set cell "spectacle_script" to spectacle_script
					set cell "date_script" to date_script
					set cell "no_de_places_script" to no_de_places_script
					do script "whole import process into «¢_reservations»"
				end tell
			end tell
		end repeat
	end perform mail action with messages
end using terms from

on initFilemaker()
	tell application "FileMaker Pro Advanced"
		open file "xxx:Users:yyy:Documents:zzz:fichiers fp7:Import reservations script.fp7"
		try
			delete every record
		end try
	end tell
end initFilemaker


Stefan is absolutely correct, I over looked the get selection statement further below in the code.

Thanks very much for that StefanK but it doesn’t seem to work either. I can’t see any trace of the script triggering. I don’t understand the functions of

and

, but of course I don’t need to understand, just get it to work. However what strikes me as strange in your construction is that this action

tell application "FileMaker Pro Advanced"
				set newRecord to create record -- store the result as 'newRecord'
				tell newRecord -- now target that new record
					set cell "nom_script" to nom
					set cell "prenom_script" to prenom_script
					set cell "spectacle_script" to spectacle_script
					set cell "date_script" to date_script
					set cell "no_de_places_script" to no_de_places_script
					do script "whole import process into «¢_reservations»"
				end tell
			end tell

precedes

on initFilemaker()
	tell application "FileMaker Pro Advanced"
		open file "Albatross:Users:geoffreydyson:Documents:Krakatoa:fichiers fp7:Import reservations script.fp7"
		try
			delete every record
		end try
	end tell
end initFilemaker

but the file “Import reservations script.fp7” needs to be open and running for all the other Filemaker actions to take place. I would therefore naively assume with my limited knowledge of Applescript that this

on initFilemaker()
	tell application "FileMaker Pro Advanced"
		open file "Albatross:Users:geoffreydyson:Documents:Krakatoa:fichiers fp7:Import reservations script.fp7"
		try
			delete every record
		end try
	end tell
end initFilemaker

must precede this

tell application "FileMaker Pro Advanced"
				set newRecord to create record -- store the result as 'newRecord'
				tell newRecord -- now target that new record
					set cell "nom_script" to nom
					set cell "prenom_script" to prenom_script
					set cell "spectacle_script" to spectacle_script
					set cell "date_script" to date_script
					set cell "no_de_places_script" to no_de_places_script
					do script "whole import process into «¢_reservations»"
				end tell
			end tell

somehow. Is that not so ? In this case would you be kind enough to show me how this is done using on initFilemaker?

Hello.

Stefan’s code is totally correct. The declaration of a handler can come after another and still be called within the preceding one. He uses the “my” keyword to address the handler within the top level scope of the script, since the handler is called within the terminology scope of mail, where that handler otherwise is unknown.

The line below calls the “handler” that initiates FileMaker before other actions with FileMaker are called upon.


my initFileMaker()

One reason that your script may fail is that if any of the variables used in this block didn’t get any values, -I’m just speculating here, but it would be natural if those variables were undefined, if there were no values that could be assigned to them in this block:


set message_text to content of oneMessage
set nom to paragraph 8 of message_text
set prenom_script to paragraph 9 of message_text
set spectacle_script to paragraph 2 of message_text
set date_script to paragraph 3 of message_text
set no_de_places_script to paragraph 5 of message_text

Your script will fail if it encounters and undefined variable. So you should have a statement like this in the case that there is some missing data in an email. (Which would improve the robustness, even if it turns out that it isn’t so in this case.


” initialization of variables, precedes the statements above.
   set message_text to ""
   set nom to ""
   set prenom_script to ""
   set spectacle_script to ""
   set date_script to "" 
   set no_de_places_script to ""

Krakatoa . :smiley:

Thank you for that bit of input. I have added your idea of ensuring that variables are defined even though that isn’t the problem as the “manual” version of the script always works. So I am still stumped to find out what is impeding this script from working, so if anyone has any ideas, I would be very happy to try them out.