mail script in MacOS Sierra that extracts attachments and changes filename

Hello


Until El Capitan, I successfully used a script in Mail.app as a rule, that would extract appended PDF and JPG and would copy them in a directory "Incoming OCR" and rename it with "date-time-filesize"


Unfortunately, since MacOS Sierra, it does not work anymore and I get the following error in the console :


An exception was thrown during execution of an NSScriptCommand... [<MCAppleScriptAttachment 0x600001852810> valueForUndefinedKey:]: this class is not key value coding-compliant for the key MIMEType.


Any help would be welcome.





using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       set dt to path to desktop
       #set outputPath to (dt as rich text) & "Incoming OCR:"
       set outputPath to "/Users/john/Incoming OCR/"
       set x to 1
       repeat with thisMessage in theMessages
           tell mail attachments of thisMessage to set {attachs, cnt} to {it, count it}
           set y to 1
           repeat with thisItem in attachs
               try
                   if MIME type of thisItem is "application/pdf" and file size of thisItem > 2000 then
                       tell ((date received of thisMessage) as «class isot» as string) to ¬
                           tell ((date received of thisMessage) as «class isot» as string) to ¬
                               rich text 1 thru 4 & "-" & rich text 6 thru 7 & "-" & rich text 9 thru 10 & "-" & ¬
                               rich text 12 thru 13 & "." & rich text 15 thru 16
                     
                     
                       save thisItem in (POSIX file (outputPath & result & "-#" & file size of thisItem & ".pdf"))
                       set y to y + 1
                   end if
                   if MIME type of thisItem is "image/jpeg" and file size of thisItem > 30000 then
                       tell ((date received of thisMessage) as «class isot» as string) to ¬
                           tell ((date received of thisMessage) as «class isot» as string) to ¬
                               rich text 1 thru 4 & "-" & rich text 6 thru 7 & "-" & rich text 9 thru 10 & "-" & ¬
                               rich text 12 thru 13 & "." & rich text 15 thru 16
                     
                       save thisItem in (POSIX file (outputPath & result & "-#" & file size of thisItem & ".jpg"))
                       set y to y + 1
                   end if
               end try
           end repeat
           set x to x + 1
       end repeat
   end perform mail action with messages
end using terms from

I also am getting AppleEvent errors when trying to access MIME Type of Mail message attachments. The error is reported as a AppleEvent handler error -10000. I have narrowed it down to accessing MIME Type. My script worked in 10.11 and does not in 10.12.

mail script in MacOS Sierra that extracts attachments and changes filename
 
 
Q