Script Started by Mail Rule

Another n00b question. From some googling I believe what I’m dealing with is a long-know, and yet never fixed, problem with the Mail app and its interface with scripts run from rules. I’m trying to use Applescript to pull an attachment from messages identified by a rule, store the attachment in a temporary holding directory, and then start a shell script to handling things from there (I’ve been writing *nix shell scripts for 30+ years and Applescript for two weeks, so I do the heavy lifting outside Applescript). The problem is that while this works if I manually highlight the mail message and force rule application things work fine. If I let it handling things automatically, the script fails part-way through.

The script as now configured (using logger to write into the system log file) is: (NOTE: There was an error in the original posted script. I had moved the lines which defined saveFolder and procScript in order to make the code a bit more readable, but in doing so I moved them outside the scope of where they were used, breaking the script. The posted script is now correct, but still doesn’t work.)


--
using terms from application "Mail"
	on perform mail action with messages these_messages for rule this_rule
		do shell script "logger -i At point 1"
		--
		tell application "Mail"
			--
			set the message_count to the count of these_messages
			do shell script "logger -i At point 2"
			set saveFolder to "/Users/jim/var/mailAtch/"
			set procScript to "/Users/jim/bin/r_move-gios-mailAtch"
			--
			repeat with n from 1 to the message_count
				--
				do shell script "logger -i At point 3"
				set this_message to item n of these_messages
				do shell script "logger -i At point 4"
				try
					set msg_id to the message id of this_message
				on error errMsg
					do shell script "logger -i Error at 3->4: " & errMsg
				end try
				do shell script "logger -i Message ID is " & msg_id
				--
				tell this_message
					--
					do shell script "logger -i At point 5"
					set these_attachments to every mail attachment
					do shell script "logger -i At point 6"
					repeat with z from 1 to the count of these_attachments
						set this_attachment to item z of these_attachments
						set this_name to the name of this_attachment
						do shell script "logger -i Attachment name: " & this_name
						save this_attachment in POSIX file (saveFolder & this_name)
					end repeat
					do shell script "logger -i At point 7"
					--
				end tell
				--
			end repeat
			--
		end tell
		--
		do shell script "/bin/tcsh " & procScript
		--
	end perform mail action with messages
end using terms from

As I said, if I send a test message and let Mail handling things automatically, the rule triggers running this script, but it halts after the “point 4” log message. It fails the first time I try to access information in the message. I put the try block around the “set msg_id” line to try to capture an error number, but the “do shell script” in the “on error” section never gets run, even when the script dies right after the “point 4” message. Now, Mail will do the right thing with this message IF I send another message that also triggers the rule. In this second time around, the new message’s processing dies at point #4 but this time the earlier message gets all the way through the processing. Just like when I manually trigger the rule. Here’s what I see in the system log in the case where I send two messages.

[format]
6/8/16 2:39:18.387 PM jim[7262]: At point 1
6/8/16 2:39:18.393 PM jim[7263]: At point 2
6/8/16 2:39:18.399 PM jim[7264]: At point 3
6/8/16 2:39:18.405 PM jim[7265]: At point 4
6/8/16 2:39:18.574 PM com.apple.xpc.launchd[1]: (com.apple.FileStatsAgent) Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
6/8/16 2:40:28.410 PM jim[7286]: At point 1
6/8/16 2:40:28.416 PM jim[7287]: At point 2
6/8/16 2:40:28.422 PM jim[7288]: At point 3
6/8/16 2:40:28.428 PM jim[7289]: At point 4
6/8/16 2:40:28.437 PM jim[7290]: Message ID is 57589083.e4b4420a.c2dbe.ffff9900@mx.google.com
6/8/16 2:40:28.441 PM jim[7291]: At point 5
6/8/16 2:40:28.450 PM jim[7292]: At point 6
6/8/16 2:40:28.455 PM jim[7293]: Attachment name: 20160608_143915_data.bin
6/8/16 2:40:28.624 PM com.apple.xpc.launchd[1]: (com.apple.FileStatsAgent) Service only ran for 0 seconds. Pushing respawn out by 10 seconds.
[/format]

I found a reference elsewhere that postulated for a very similar problem that POP vrs. IMAP connections to the external mail server might be an issue. I normally use POP (Gmail and my company’s server), so I pulled up an old IMAP account I don’t use much (on iCloud) and switched over to testing using it. Same exact behavior.

I’ve also tried putting delays in the script, and other tricks like moving the message to another mailbox and trying to process it there, and no luck. I’m about to throw in the towel and just brute-force search the Mail app’s folders for attachments to process using a tcsh script.

I’m running whatever version of Applescript is standard on 10.11.5.

Any ideas?