Mail rule launches .scpt but won't process non mail.app portions of it

Hello. I’ve been trying to figure this one out on my own, but really can’t find anything that addresses the problem.

My workflow on this starts with an e-mail that triggers the rule that launches this script. The problem i’m having is that only the portions of this script that directly relate to Mail.app (v 3.5 under OS 10.5.6) are processed. the rest just dies…

in my testing the only way that the non-mail.app portions will run is if the mail portion errors out when saving the attachment (like… if an identically named file exists in the save location and i don’t tell it how to handle that problem)… which renders everything down the line useless.

i’ve tried nesting the non-mail stuff inside the mail code and tried using "using terms from application ‘XXX’ " to no avail.

i’m trying to avoid using a folder action to launch the rest of the script, but if all else fails then maybe it’ll have to do.

any insight would be spectacular.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set destinationFolder to ((path to desktop) as Unicode text)
		tell application "Mail"
			repeat with This_Message in theMessages
				repeat with oneAttachment in mail attachments of This_Message
					set {name:theName, MIME type:mimeType} to oneAttachment
					if theName ends with ".csv" then
						save oneAttachment in (destinationFolder as Unicode text) & theName
					end if
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from
set openpath to ((path to desktop) & "test.csv" as Unicode text)
tell application "BBEdit"
	activate
	open {file openpath}
end tell

Hi,

in a mail rule script everything outside the event handler will not be recognized respectively executed
try this


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		set destinationFolder to (path to desktop)
		tell application "Mail"
			repeat with This_Message in theMessages
				repeat with oneAttachment in mail attachments of This_Message
					set {name:theName, MIME type:mimeType} to oneAttachment
					if theName ends with ".csv" then
						set destinationPath to (destinationFolder as text) & theName
						save oneAttachment in destinationPath
						my openFileInBBEdit(destinationPath)
					end if
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

on openFileInBBEdit(openpath)
	tell application "BBEdit"
		activate
		open (openpath as alias)
	end tell
end openFileInBBEdit

Note: In Leopard there is one unique text class text, Unicode text is only needed in read/write operations to specify the text encoding

hrm.

your code worked very well with the example given, but when i try to substitute in the code that i need to run the script not only fails, but also crashes Mail.app. kinda cool, but not all that practical. hehehe.

when i initially submitted my question i was expecting to be able to place whatever script elements back in once i figured out why it wasn’t performing as expected.

now that i know that i’m limited to things in the event handler i was hoping to just call a new script and be done with it.

this hasn’t been as trivial as i was expecting…

here’s a capture of one of the windows that popped up on one of the occasions where mail didn’t just completely crash. http://www.glitchedmedia.com/pictures/scriptfail.png the script being called was

display dialog "Whee!." buttons {"Continue", "Cancel"} default button 1

If this Mail.app rule can launch a new script after moving the attachment then i’ll be in great shape… otherwise it’s time to move on and find a way to do it outside of mail.app.

when i modified your “openFileInBBEdit” routine to try and run a script instead that’s when things started crashing and behaving erratically. (“openpath” is defined in the main block but i’ve also tried defining it in this block with similar results.)

on openFileInBBEdit(openpath)
	try
		run script file (openpath & "whee.scpt")
	on error
		display dialog "Script launch failed." buttons {"Continue", "Cancel"} default button 1
	end try
end openFileInBBEdit

i think this is probably as simple as a fundamental misunderstanding on my part of the process of using “my openFileblahblahblah” and the accompanying code.

specifically, what is the reasoning behind having that parenthetical after “my openFileInBBEdit”? it’s referenced above which calls the statement below, but when it’s time to END that process the parenthetical isn’t there… I’ve seen this process used in other examples, but the reasoning hasn’t clicked yet.

user interaction like display dialog is not supported in mail rule scripts