Mail.app scripts not running

Well, I think I got it to make sense, but there’s something wrong. Every time it runs I get ‘Error 8: NSInternal message’

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		repeat with eachMessage in theMessages
			tell application "Mail"
				try
					set thesubject to subject of eachMessage
					set theSender to sender of eachMessage
					set bytes to message size of eachMessage
					set thebody to content of eachMessage
					-- conditional loop
					set myAttachments to every mail attachment of theMessages
					if count (items of myAttachments) < 1 then
						--do the forward of the email that has no attachments
						set newMessage to make new outgoing message with properties {subject:thesubject & "From" & theSender, content:thebody, sender:"mailer@nburmandesign.com", visible:true}
						tell newMessage
							make new to recipient at end of to recipients with properties {name:"Filtering", address:"nburman@rogers.blackberry.net"}
							send
						end tell
						
					else -- send attachment notification
						set newMessage to make new outgoing message with properties {subject:"New mail + attachment", content:"You've got a new message sent to Contact from " & theSender & ", Subject: " & thesubject & ". Size of message: " & bytes & " bytes. ", sender:"mailer@nburmandesign.com", visible:true}
						tell newMessage
							make new to recipient at end of to recipients with properties {name:"Filtering", address:"nburman@rogers.blackberry.net"}
							send
							
						end tell
					end if
				on error the error_message number the error_number
					display dialog " Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
				end try
			end tell
		end repeat
	end perform mail action with messages
end using terms from

Interestingly, you get more info than I do. When I try it, it just hangs (or dies, not sure which). I’m tied up most of the day, but will try to look at this further this evening.

Thanks Kevin. I tried commenting out lines, but didn’t get anywhere.

Can anyone tell me why I’m getting an error with this script?

Sorry Nicholas, I’ve been swamped and no opportunity to work on this. I’ll take a look at it and see if I can figure out something.

Nicholas,

Here’s what I have so far. The part that handles messages WITH attachments works, but the other half doesn’t. I suspect that the “no attachments” code needs to go in the on error block. But I’ll have time later to check that.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				say "processing"
				
				set thesubject to subject of eachMessage
				set theSender to sender of eachMessage
				set bytes to message size of eachMessage
				set thebody to content of eachMessage
				-- conditional loop
				set myAttachments to {}
				if exists (mail attachment of eachMessage) then
					
					--try
					set myAttachments to every mail attachment of eachMessage
					say "exists attachments"
					(*
on error the error_message number the error_number
						display dialog "Error: " & the error_number & ". " & the error_message buttons {"Cancel"} default button 1
					end try
*)
				else
					say "no exists attachments"
				end if
				
				
				say aCount & " attachments"
				if aCount < 1 then
					say "no attachments"
					--do the forward of the email that has no attachments
					set newMessage to make new outgoing message with properties {subject:thesubject & "From" & theSender, content:thebody, sender:"nitewing98@comcast.net", visible:true}
					tell newMessage
						make new to recipient at end of to recipients with properties {name:"Filtering", address:"konkrypton@gmail.com"}
						send
					end tell
					say "message sent - no attachments"
				else -- send attachment notification
					say "got attachments"
					set newMessage to make new outgoing message with properties {subject:"New mail + attachment", content:"You've got a new message sent to Contact from " & theSender & ", Subject: " & thesubject & ". Size of message: " & bytes & " bytes. ", sender:"nitewing98@comcast.net", visible:true}
					tell newMessage
						make new to recipient at end of to recipients with properties {name:"Filtering", address:"konkrypton@gmail.com"}
						send
						say "message sent - with attachments"
					end tell
				end if
			end repeat
		end tell
		
	end perform mail action with messages
end using terms from

OK! :smiley:

I finally found the fly in the ointment. The Applescript support in Mail 2.0 is notoriously flawed (try saving a signature, it’s impossible) and apparently there are some messages whose content is impossible to get. I’ve got just such a message, and on inspecting it, it seems to have inline mime data. In theory, you should be able to get the message’s source (also a property like content), but for messages of this type, neither keyword seems to work.

I got around it by using a try block and just putting in a message that the content can’t be gotten.

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				say "processing"
				
				
				set thesubject to subject of eachMessage
				set theSender to sender of eachMessage
				set bytes to message size of eachMessage
				try
					set thebody to content of eachMessage
				on error
					set thebody to "Message has inline content"
				end try
				
				set myAttachments to {}
				try
					set myAttachments to every mail attachment of eachMessage
					set aCount to count items of myAttachments
				on error
					set aCount to 0
				end try
				
				if aCount > 0 then
					say "got attachments"
					set newMessage to make new outgoing message with properties {subject:"New mail + attachment", content:"You've got a new message sent to Contact from " & theSender & ", Subject: " & thesubject & ". Size of message: " & bytes & " bytes. ", sender:"nitewing98@comcast.net", visible:true}
					tell newMessage
						make new to recipient at end of to recipients with properties {name:"Filtering", address:"konkrypton@gmail.com"}
						send
						say "message sent - with attachments"
					end tell
				else
					say "no attachments"
					--do the forward of the email that has no attachments
					set newMessage to make new outgoing message with properties {subject:thesubject & "From" & theSender, content:thebody, sender:"nitewing98@comcast.net", visible:true}
					tell newMessage
						make new to recipient at end of to recipients with properties {name:"Filtering", address:"konkrypton@gmail.com"}
						send
					end tell
					say "message sent - no attachments"
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from

I put the say commands in there so you could track what’s going on, you can remove them when you’re done playing with it.

Wow - thanks Kevin. I’ll give it a spin later and let you know what happens.

For the best part, it worked! Mail politely announced its actions, but it looks like the inline content stopped the message being forwarded. I changed this -

on error
					say  "An error occured."
					set thebody to "Message has inline content"
				end try

and the message didn’t get forwarded. Would ‘forward’ send it if the error still occurred?

The portion that notifies of attachments worked great tho!