Mail Message Attachment count problem - message id fail

I have to say, every time I have tried to use Mail Rules and Mail Rule Actions using AppleScript, I have regretted it. Seems like every new OS has a new bug with Mail Rules, and I wait and wait, hoping it is fixed. By that time there is another problem which makes it dysfunctional. Today is no exception.

Here is a script I want to use, based on a script Mark A posted here recently. I wanted to use it in a Mail Rule to move attachments into a specific folder when the message had a certain Subject. (Note on my Mac, using POSIX file to save the attachment did not work, which I mentioned in his previous post)

using terms from application "Mail"
	on perform mail action with messages passedMessages for rule theRule
		set desktopPath to (path to desktop)
		-- Get incoming messages that match the rule
		tell application "Mail"
			repeat with aMessage in passedMessages
				set attachmentList to (every mail attachment of aMessage) 
			--the line above chokes and error msg is "osascript got an error: Canā€™t get every mail attachment of message id 90245 of mailbox "INBOX" of account id "A5369D37-E8CD-42D4-90CD-B1AE39D530F6".
				repeat with anAttachment in attachmentList
					set attachmentName to name of anAttachment
					save anAttachment in file (desktopPath & attachmentName) 
					-- Mark's original script here used POSIX file, but that did not work on High Sierra on my Mac
				end repeat
			end repeat
		end tell
	end perform mail action with messages
end using terms from

With various incarnations and rearrangements, sometimes I would get the error that Mail doesnā€™t understand ā€˜countā€™ in regard to mail attachments. But it still choked on the same line.
What seems to be causing the problem is the term ā€˜message id 90425ā€™ from what some experimentation below revealed:

   (*
	tell application "Mail"
	set tlist to every mail attachment of message id 90245 of mailbox "INBOX" of account id "A5369D37-E8CD-42D4-90CD-B1AE39D530F6"
	end tell
	-- this won't compile
	
	--Error message is "Expected end of line but found number" and '90245' is highlighted
	--mail rule passes this when the rule catches a message which matches the rule:
	 -- message id 90245 of mailbox "INBOX" of account id "A5369D37-E8CD-42D4-90CD-B1AE39D530F6"
	 
	 --this won't compile either:
	 tell application "Mail.app"
	 	subject of message id 90245 of mailbox "INBOX" of account id "A5369D37-E8CD-42D4-90CD-B1AE39D530F6"
	 end tell
*)


tell application "Mail"
	set tlist to every mail attachment of message 2 of mailbox "INBOX" of account id "A5369D37-E8CD-42D4-90CD-B1AE39D530F6"
end tell

-- this is 'copy reference' for the selected message which has a caution icon in Explorer...it uses Message 2
-- to get the descriptor for the selected message which uses the message id.
--if Mail passes the message id which does not seem to work in script, what's the workaround???
tell application "Mail"
	tell its account "iCloud"
		tell its mailbox "INBOX"
			message 2
		end tell
	end tell
end tell

OS X 10.13.6
Mail Version 11.5 (3445.9.1)

So, anyone got any thoughts?
Thanks in advance

Hello Vinnie,

As a novice I fought a similar problem for several weeks and finally got the following script working. Apologies for the poor formattingā€¦ Regarding the oascript error message you are receiving while I donā€™t understand it completely I was able to work around it by re-saving my script and closing the corresponding tab.

Regarding the rule below it saves the message attachments in a specified folder and them moves the message to the ā€œArchiveā€ associated with the account. I found if I didnā€™t specify the account it would save it in the ā€œArchiveā€ Mailbox on my Mac not the ā€œArchiveā€ mailbox associated with the mail account.

Hope this helps.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
using terms from application "Mail"
	on perform mail action with messages aMessage for rule theRule
		tell application "Mail"
			set theArchiveMailboxName to "Archive"
			set mailAccountID to "FA7820C4-3EDC-4D63-9C96-73E1A8314D7C" -- Jolene Email 
			set desktopPath to ("Macintosh HD:Users:Tim:Dropbox:Tim & Jolene:Wirecard:")'s POSIX path
			repeat with aMessage in (get selection)
				repeat with anAttachment in mail attachments of aMessage
					set attachmentName to name of anAttachment
					save anAttachment in POSIX file (desktopPath & attachmentName)
				end repeat
				move aMessage to mailbox theArchiveMailboxName of account id mailAccountID
			end repeat
		end tell
	end perform mail action with messages
end using terms from

To format code in posts, precede it by three tickmarks (the ` character) on a line, and follow it with the same. Iā€™ve added them to your post.

Iā€™m not sure whether or not this is intentional, but this script initially looks like itā€™s designed to run in response to a Mail rule, acting upon a set of messages passed to the script and stored in the variable aMessage.

However, your outer repeat block then overwrites the aMessage variable, which sequentially reads the message objects stored in the selection.

Sorry for pointing this out if this was intentional.

Thank you ā€¦ :slight_smile:

Thank for reviewing and commenting. As an acknowledged novice the tips are much appreciated. Regarding your comment, you are correct this is intended to be run as a mail rule handler.However if I remove the outer ā€œRepeat with aMessageā€ and corresponding ā€œend repeatā€ the ā€œMove aMessageā€¦ā€ statement doesnā€™t seem to execute or if it does the message isnā€™t actually moved. Tried a couple of variations and this is what I could get to work.

Thanks again

Hi Vinnie-Bob,

My OS version is Sierra. So I canā€™t reproduce the issue youā€™re experiencing.
But I can identify 2 potential problems in your code. See the comments for explanations.

using terms from application "Mail"
	on perform mail action with messages passedMessages for rule theRule
		## coerce desktop path to string, so we can append the attachment name to the path :
		set desktopPath to (path to desktop) as string
		tell application "Mail"
			repeat with aMessage in passedMessages
				## test if there is at least 1 attachment to avoid errors :
				if exists mail attachment 1 of aMessage then
					## next line is an alternative to the previous :
					--if (count of mail attachment of aMessage) > 0 then
					set attachmentList to (every mail attachment of aMessage)
					repeat with anAttachment in attachmentList
						set attachmentName to name of anAttachment
						save anAttachment in file (desktopPath & attachmentName)
					end repeat
				end if
			end repeat
		end tell
	end perform mail action with messages
end using terms from
1 Like

I have never coded error checking into ā€˜repeat with listā€™ statements because if the list is empty, it simply doesnā€™t run. And it doesnā€™t throw an error.

set testList to {"test"}
set testList_empty to {}
set myResult to ""

repeat with an_item in testList
    set myResult to an_item
end repeat

repeat with something in testList_empty
    set myResult to myResult & return & {"empty list does nothing"}
end repeat