@NickeZ28
With which system were you able - if you ever did - to save the attachments with a script triggered by a rule ?
As it fails with 10.12.2, I tried with 10.12.1 : fails too.
I tried with 10.11.5 : fails too.
It fails always upon the instruction : set theAttachments to theMessage’s mail attachments.
As I was tired to hit a wall I decided to try an other scheme.
It rely upon three files.
A script attached to a rule which apply no change to the messages
(*
tell application "Mail"
set test_list to selection
tell me to perform mail action with messages (test_list)
end tell
*)
# Entry point used by the Mail rule
using terms from application "Mail"
on perform mail action with messages theMessages --for rule SAVE_ATTACHMENT_SIERRA
tell application "Mail"
set theList to {}
repeat with theMessage in theMessages
set messageID to (get message id of theMessage)
set end of theList to messageID
end repeat
end tell
set theString to my recolle(theList, linefeed)
tell application "Finder"
tell me to set thePath to ((path to desktop) as text) & "messagesIDs_sDIsegassem.txt" # Adjust to fit your needs
end tell
my writeto(thePath, theString, «class utf8», false)
tell application "Finder"
set theApp to open ((path to desktop as text) & "driven.app") as alias # Adjust to fit your needs
end tell
end perform mail action with messages
end using terms from
#=====
on recolle(l, d)
local oTIDs, t
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end recolle
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as «class furl»
set openFile to open for access targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access targetFile
end try
return false
end try
end writeto
#=====
The script creates the second file which contain the message id of every passed message.
When the 2nd file is written, the script call the 3rd file which is a script saved as an application.
on run
set finalMailBox to "aaa/aac" # Edit to fit your needs
# Builds the path to the text file containing the message IDs.
# It must match the path defined by the script triggered by the rule
set thePath to ((path to desktop) as text) & "messagesIDs_sDIsegassem.txt" #
# Read the message IDs
set theIDs to paragraphs of (read file thePath)
# Build the list of messages
set theMessages to {}
repeat with anID in theIDs
set end of theMessages to my searchMail(anID)
end repeat
tell me to say "point 2"
# Build the path to the folder were attachments must be saved
set attachmentsFolder to (path to documents folder as text) & "4attachments:" # Edit to fit your needs
tell application "Finder" # was System Events
tell me to say "point 3"
if not (exists folder attachmentsFolder) then
tell me
say "point 4"
error "the folder " & attachmentsFolder & " doesn't exist!"
end tell # current application
end if
end tell
tell application "Mail"
tell me to say "point 6"
tell me to say "il y a " & (count theMessages) & " messages"
set pass to 0
repeat with theMessage in theMessages
set pass to pass + 1
tell me to say "point 7 message " & pass
set theAttachments to every mail attachment of theMessage # FAILS when called by a rule
tell me
say "point 8 message " & pass
say (get count of theAttachments) --> say 1
end tell # current application
set attch to 0
repeat with theAttachment in theAttachments
set attch to attch + 1
tell me to say "point 9 message " & pass & " attachment " & attch
# CAUTION: if the Hfs name of an attachment contain a slash, Mail replace it by a colon
try
set originalName to name of theAttachment
tell me
say "point 10 message " & pass & " attachment " & attch
say originalName --> say "bouquins.rtf"
if originalName contains ":" then
set savePath to my remplace(originalName, ":", "/")
end if
set savePath to attachmentsFolder & originalName
say "point 11 message " & pass & " attachment " & attch
--say savePath --> say "SSD 500:Users:myHome:Documents:4attachments:bouquins.rtf"
close access (open for access savePath)
say "point 12 message " & pass & " attachment " & attch
# As this script is a workaround for Sierra 10.12.2/3, «class furl» is available
(*
set POSIXPath to POSIX path of savePath
tell me to set savePath to POSIX file POSIXPath
*)
set savePath to savePath as «class furl»
say "point 13 message " & pass & " attachment " & attch
end tell # current application
save theAttachment in savePath
tell me to say "point 14 message " & pass & " attachment " & attch & " is saved"
end try
end repeat # theAttachment
set read status of theMessage to true
tell me to say "point 15 message " & pass
move theMessage to mailbox "aaa/aac"
tell me to say "point 16 message " & pass
end repeat # theMessage
end tell # Mail
end run
on searchMail(messageID)
tell application "Mail"
repeat with a in accounts
repeat with mb in mailboxes of a
set x to (messages whose message id is messageID) of mb
-- if length of x is greater than 0 then return (item 1 of x)
if (count x) > 0 then return (item 1 of x)
end repeat
end repeat
repeat with mb in mailboxes
set x to (messages whose message id is messageID) of mb
-- if length of x is greater than 0 then return (item 1 of x)
if (count x) > 0 then return (item 1 of x)
end repeat
end tell
end searchMail
This late item will read the text file containing every message id .
It use them to build a list of references to the messages
If a message has attachments they are saved in the dedicated folder
The read status of the message is set to true
The message is moved to the dedicated mailbox.
I know that it’s awful but at least, it does the job.
Of course, if you decide to use the scripts, it would be a good idea to remove the instructions say something which are just useful to trace the script behavior during tests.
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 14 janvier 2017 18:27:02