Mail Message Attachment count problem - message id fail


(Vince Angeloni) #1

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


#2

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

(Shane Stanley) #3

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.


(CK) #4

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.


#5

Thank you … :slight_smile:


#6

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


(Jonas Whale) #7

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

(Vince Angeloni) #8

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