Hello
Trying to help an asker in macScripter I wrote a piece of code saving attachments of the selected Mail's messages. It behaves flawlessly. Alas, when I encapsulate it so that a rule may trigger it it fails. I inserted several say instructions to trace the execution. The complete code is available below. The script correctly say "point 7" then fail when trying to execute the instruction: set theAttachments to theMessage's mail attachments
Is one of you able to explain what is wrong or at least deliver a workaround? I moved most of the code in a handler because it's easier to test the script from the Script Editor and that the instruction : set attachmentsFolder to (path to documents folder as text) & "4attachments:" is wrongly compiled when it is in the standard wrapper. It become : set attachmentsFolder to (path to documents folder rule type rich text) & "4attachments:". Adding a couple of parenthesis give a better behavior: set attachmentsFolder to ((path to documents folder) as rich text) & "4attachments:" but I dislike the insertion of 'rich".
I have a workaround for that : move the instruction in the tell Finder block.
I add - but I'm not sure that it's relevant - that the original version used a tell System Events block but when the script was called by the rule the test upon the existence of the folder returned always false.
# Entry point used by the Mail rule using terms from application "Mail" on perform mail action with messages theMessages for rule SAVE_ATTACHMENT_SIERRA say "point 1" my germaine(theMessages) end perform mail action with messages end using terms from
#===== # Entry point used for tests on run say "point 1alt" tell application "Mail" activate set theMessages to (get the selection) end tell my germaine(theMessages) end run
#=====
on germaine(theMessages) say "point 2" set attachmentsFolder to (path to documents folder as text) & "4attachments:" say "point 3" say attachmentsFolder --> "SSD 500:Users:myHome:Documents:4attachments:" tell application "Finder" # was System Events if not (exists folder attachmentsFolder) then tell me say "the folder " & attachmentsFolder & " doesn't exist!" error "the folder " & attachmentsFolder & " doesn't exist!" end tell # current application end if end tell say "point 5" tell application "Mail" tell me to say "point 6" repeat with theMessage in theMessages tell me to say "point 7" set theAttachments to theMessage's mail attachments # FAILS when called by a rule tell me say "point 8" say (get count of theAttachments) --> say 1 end tell # current application repeat with theAttachment in theAttachments tell me to say "point 9" # 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" 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" say savePath --> say "SSD 500:Users:myHome:Documents:4attachments:bouquins.rtf" close access (open for access savePath) say "point 12" # 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" end tell # current application save theAttachment in savePath tell me to say "point 14" end try end repeat # theAttachment end repeat # theMessage end tell # Mail end germaine
#===== (* replaces every occurences of d1 by d2 in the text t *) on remplace(t, d1, d2) local oTIDs, l set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1} set l to text items of t set AppleScript's text item delimiters to d2 set t to l as text set AppleScript's text item delimiters to oTIDs return t end remplace
#=====
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 13 janvier 2017 15:40:54
|