Saturday, April 29, 2017

#1 2016-12-24 02:40:49 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Mail.app save attachment - stopped working after Sierra 10.12.2 update

Hi,

This part of my script stopped working after the Sierra 10.12.2 update. Does anyone have any clues about this ?

Thanks,


Applescript:

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       set attachmentsFolder to "Volumes:folder1:folder2:" as rich text
       tell application "Mail"
           set selectedMessages to theMessages
           try
               repeat with theMessage in selectedMessages
                   repeat with theAttachment in theMessage's mail attachments
                       set originalName to name of theAttachment
                       set savePath to attachmentsFolder & originalName
                       save theAttachment in file (savePath)
                   end repeat
               end repeat
           end try
       end tell
   end perform mail action with messages
end using terms from

Offline

 

#2 2016-12-24 04:35:41 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

I've temporary solved it by moving the script to another computer running OS 10.6.8

Working fine there.

Niklas

Offline

 

#3 2016-12-24 05:12:10 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

I'm getting the same behavior.
Each attempt to save issue the message :
--> error "Pour afficher ou modifier vos autorisations, sélectionnez l’élément dans le Finder, puis choisissez Fichier > Lire les informations." number -10000

I tested with messages from an inBox and with messages in "standard" mailboxes.

Fails with 10.12.3 too.
Works with 10.11.6

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 24 décembre 2016 12:11:50

Last edited by Yvan Koenig (2016-12-24 06:02:10 am)

Offline

 

#4 2016-12-25 05:16:53 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Problem reported as:
29804595  10.12.2 (& 10.12.3) killed save Mail attachments thru AppleScript

As always, it would be useful to file others reports so that the problem would be prioritized.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 25 décembre 2016 12:16:47

Offline

 

#5 2016-12-25 10:23:02 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Thanks,

I can file a report to, but how and where do I report that ?

Niklas

Offline

 

#6 2016-12-25 01:06:19 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Enter :
https://bugreport.apple.com/

As you didn't know that, you will be asked to register. Don't worry, it's free.
Then you will be able to file a report.

Here is what I posted :
Summary:
Select some Mail messages with attachments, run a script which saved the attachments for years and nothing is saved

Steps to Reproduce:
Select some Mail messages with attachments
run the attached script

Expected Results:
With 10.9 thru 10.12.1, the attachments were correctly saved

Actual Results:
With 10.12.2 (& 10.12.3) the attachments aren't saved.
--> error "Pour afficher ou modifier vos autorisations, sélectionnez l’élément dans le Finder, puis choisissez Fichier > Lire les informations." number -10000
Under 10.12.1 and 10.12.2 , the permissions for 4attachments on the desktop are:
me read/write
admin read only
everyone read only
for 4attachments in Documents folder they were:
me read/write
admin read/write
everyone read only
After the tests I restored admin to read only.

Version:
Mail Version 10.2 (3259)
macOS 10.12.2 public. I didn't tested with beta versions
failed also with the two 10.12.3 beta delivered at this time

Notes:
Really boring because I have no workaround except returning to 10.12.1
Problem reported by different macScripter's users and appleScript's users too.
I hope that you will repair that quickly.
It's just annoying to see that it's at least the 3rd feature related to AppleScript broken by 10.12.2

Configuration:
iMac KOENIG Yvan 2016-12-25.spx.zip attached

Attachments:
'4mail?.scpt.zip' and 'iMac KOENIG Yvan 2016-12-25.spx.zip' were successfully uploaded.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 25 décembre 2016 20:06:06

Offline

 

#7 2017-01-03 02:28:50 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Here is a script usable as a workaround.

Applescript:

------------------------------------------------------------
# Select messages with attachments to save
# Run the script to save the files in the folder defined below
# Of course you may define an other path.
# define the storage folder
set POSIXDestFolder to POSIX path of (path to downloads folder)

set {theApp, mt, mi} to {"Mail", 3, 13}

tell application id "com.apple.systemevents" to tell process theApp
   set frontmost to true
   tell menu bar 1
       -- name of menu bar items
       (*{
01 - "Apple",
02 - "Mail",
03 - "Fichier",
04 - "Édition",
05 - "Présentation",
06 - "Boîte aux lettres",
07 - "Message",
08 - "Format",
09 - "Fenêtre",
10 - "Aide"
}*)

       -- name of menu bar item mt
       --> "Fichier"
       tell menu bar item mt to tell menu 1
           -- set theMenuItems to name of menu items
           (* {
01 - "Nouveau message"
02 - "Nouvelle fenêtre de visualisation"
03 - "Ouvrir le Message"
04 - "missing value"
05 - "Fermer la fenêtre"
06 - "Tout fermer"
07 - "Fermer l’onglet"
08 - "Enregistrer"
09 - "Enregistrer sous…"
10 - "Enregistrer comme modèle…"
11 - "missing value"
12 - "Joindre des fichiers…"
13 - "Enregistrer les pièces jointes…"
14 - "Coup d’œil sur les pièces jointes…"
15 - "missing value"
16 - "Importer des boîtes aux lettres…"
17 - "missing value"
18 - "Exporter au format PDF…"
19 - "missing value"
20 - "Imprimer…"
}*)

           -- name of menu item mi
           --> "Enregistrer les pièces jointes…"
           click menu item mi
       end tell # menu bar item mt
   end tell # menu bar 1
   
   tell window 1
       -- its name --> "Boîte de réception (899 messages)"
       repeat until exists sheet 1
           delay 0.02
       end repeat
       -- class of UI elements --> {splitter group, button, button, button, toolbar, group, static text, sheet}
       tell sheet 1
           -- class of UI elements --> {button, button, button, group}
           -- class of UI elements of group 1 --> {splitter group, group, radio group, pop up button, text field}
           
           keystroke "g" using {command down, shift down}
           repeat until exists sheet 1
               delay 0.02
           end repeat
           tell sheet 1
               -- class of UI elements --> {static text, combo box, button, button}
               tell me to set sysv2 to (system attribute "sys2")
               if sysv2 < 11 then
                   set theTarget to text field 1
               else
                   set theTarget to combo box 1
               end if
               # Defines the path of the folder where the file must be saved
               set value of theTarget to POSIXDestFolder
               -- title of buttons --> {"Aller", "Annuler"}
               keystroke return # ditto click button 1 (ou Aller)
           end tell # sheet 1
           -- name of buttons --> {"Enregistrer", "Nouveau dossier", "Annuler"}
           click button 1
       end tell # sheet 1
   end tell # window 1
end tell # system Events & process
------------------------------------------------------------

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mardi 3 janvier 2017 21:27:26

Last edited by Yvan Koenig (2017-01-04 04:21:14 am)

Offline

 

#8 2017-01-04 05:28:29 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Big surprise this morning.

I re-ran the script which I used to test your code :

Applescript:

set attachmentsFolder to (path to downloads folder) as text

tell application "Mail"
   set selectedMessages to the selection
   
   repeat with theMessage in selectedMessages
       repeat with theAttachment in theMessage's mail attachments
           set originalName to name of theAttachment
           set savePath to attachmentsFolder & originalName
           try
               save theAttachment in file (savePath)
           end try
       end repeat
   end repeat
   
end tell

This time it behaved flawlessly.
I was able to reproduce the odd behavior when I replaced the instruction defining the destination folder by :

set attachmentsFolder to (path to desktop) as text

On december 24th I was getting : --> error "Pour afficher ou modifier vos autorisations, sélectionnez l’élément dans le Finder, puis choisissez Fichier > Lire les informations." number -10000 with every folder.

I made other attempts with the Documents folder for instance

It's really puzzling because all the tested folders, including the one where I was accustomed to save my attachments
(path to documents folder as text) & "Attachments:"
have the same permissions :
me : read-write
everyone : unauthorized access (translated from the french Accès interdit)

I have no idea of what may explain that.

I made a complementary test. As it is positive, it give an efficient scheme.

Applescript:

set folderName to "attachments" # Change the name if you wish
set downloadFolder to (path to downloads folder as text) # Don't change this instruction
set attachmentsFolder to downloadFolder & folderName & ":" # Don't remove the ending colon
tell application "System Events"
   if not (exists folder attachmentsFolder) then
       make new folder at end of folder downloadFolder with properties {name:folderName}
   end if
end tell

tell application "Mail"
   set selectedMessages to every message of mailbox "aaa"
   
   repeat with theMessage in selectedMessages
       repeat with theAttachment in theMessage's mail attachments
           set PosixName to name of theAttachment
           # CAUTION, if the name of the file contain some slashes,
           # it replace them by colons. So the next instruction reset the slashs.
           set originalName to my remplace(PosixName, ":", "/")
           set savePath to attachmentsFolder & originalName
           try
               save theAttachment in file (savePath)
           end try
       end repeat
   end repeat
   
end tell

#=====
(*
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

#=====

If the attachmentsFolder didn't existed at first execution the script creates it.
Once it's done, we may attach a folder action script to the folder so that it moves the files to the folder of our choice in which - at this time - we can't store them directly.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 4 janvier 2017 12:27:58

Last edited by Yvan Koenig (2017-01-04 07:09:00 am)

Offline

 

#9 2017-01-12 04:50:17 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Thanks.  smile

I've looked at another solution.

I guess (for my use), I could do a search for the specific item that are not older than 24hrs I need in the folder : "~/Library/Containers/com.apple.mail/Data/Library/Mail Downloads/" once every 24hrs. And copy those to the folder which handles the rest of the script.

Im just guessing that will work too. Haven't tried it yet.

Niklas

Offline

 

#10 2017-01-12 06:22:25 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Here is a neater scheme using a tip which I got in applescript-users@lists.apple.com

Applescript:

set attachmentsFolder to "Volumes:folder1:folder2:"
set attachmentsFolder to (path to documents folder as text) & "4attachments:"
--set attachmentsFolder to (path to desktop as text) & "4attachments:"
--return attachmentsFolder
tell application "System Events"
   if not (exists folder attachmentsFolder) then error "the folder " & attachmentsFolder & " doesn't exist!"
end tell
tell application "Mail"
   set theMessages to the selection
   set selectedMessages to theMessages
   repeat with theMessage in selectedMessages
       repeat with theAttachment in theMessage's mail attachments
           # CAUTION: if the Hfs name of an attachment contain a slash, Mail replace it by a colon
           try
               set originalName to name of theAttachment
               if originalName contains ":" then
                   set savePath to my remplace(originalName, ":", "/")
               end if
               set savePath to attachmentsFolder & originalName
               
               close access (open for access savePath) # THE TIP
               # 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
               *)

               tell me to set savePath to savePath as «class furl»
               save theAttachment in savePath
           end try
       end repeat # theAttachment
   end repeat # theMessage
end tell # Mail

#=====
(*
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

#=====

The main tip is the instruction
close access (open for access savePath) # THE TIP

Don't forget the instructions replacing a possible colon in the attachment name returned by Mail.
This app returns the file name in POSIX format so the colon is the counterpart of a slash existing in the Hfs name.
If we drop these instructions we will get an awful "file unavailable" error.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 12 janvier 2017 13:22:14

Offline

 

#11 2017-01-13 03:24:35 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

It works flawlessly .. Perfect!

But Im having trouble getting Apple Mail.app to run the rule automatically.  Thats probably due to my mail rules settings though.

I have three different things that will happend to the incoming emails :

1. Move the email to a specific folder in my Mail.app
2. Mark the email as read.
3. Run the Applescript (above)


I've added :

Applescript:


using terms from application "Mail"
   on perform mail action with messages theMessages for rule SAVE_ATTACHMENT_SIERRA


   end perform mail action with messages
end using terms from

But it did nothing.


However if I manually run the rules for the email I select. It saves the attachment perfectly.

Wierd. Will have to investigate

Thanks,
Niklas

Offline

 

#12 2017-01-13 04:09:16 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

I made several attempts but I never got a rule able to run an applescript.

Bingo. Today I'm more lucky. I was able to get a script running.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 13 janvier 2017 11:09:10

Last edited by Yvan Koenig (2017-01-13 04:42:53 am)

Offline

 

#13 2017-01-13 05:47:52 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Here is where I am.
I edited the script a bit because the compiler refused to compile the instruction

Applescript:

set attachmentsFolder to (path to documents folder as text) & "4attachments:"

I replaced tell application "System Events" by tell application "Finder" because with the original instruction the existing folder was not seen.
As you may see, I added several say instructions to trace the execution.
At this time execution reach point 7 but fail to execute the instruction

Applescript:

set theAttachments to theMessage's mail attachments

Here is the script called by the rule :

Applescript:


using terms from application "Mail"
   on perform mail action with messages theMessages for rule SAVE_ATTACHMENT_SIERRA
       say "point 1"
       tell application "Finder"
           tell me to say "point 2"
           tell me to set attachmentsFolder to (path to documents folder as text) & "4attachments:"
           tell me to say "point 3"
           if not (exists folder attachmentsFolder) then
               tell me to say "the folder " & attachmentsFolder & " doesn't exist!"
               error "the folder " & attachmentsFolder & " doesn't exist!"
           end if
           tell me to say "point 4"
       end tell
       say "point 5"
       tell application "Mail"
           set selectedMessages to theMessages
           tell me to say "point 6"
           repeat with theMessage in selectedMessages
               tell me to say "point 7"
               set theAttachments to theMessage's mail attachments
               tell me to say "point 8"
               tell me to say (get count of theAttachments)
               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 to say "point 10"
                       tell me to say originalName
                       if originalName contains ":" then
                           set savePath to my remplace(originalName, ":", "/")
                       end if
                       set savePath to attachmentsFolder & originalName
                       tell me to say "point 11"
                       tell me to say savePath
                       tell me to close access (open for access savePath) # THE TIP
                       tell me to 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
                       *)

                       tell me to set savePath to savePath as «class furl»
                       tell me to say "point 13"
                       save theAttachment in savePath
                       tell me to say "point 14"
                   end try
               end repeat # theAttachment
           end repeat # theMessage
       end tell # Mail
       
   end perform mail action with messages
end using terms from

#=====
(*
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

#=====

and here is the script used to mimic the one above.

Applescript:


tell application "Mail"
   activate
   set theMessages to (get the selection)
end tell

say "point 1"
tell application "Finder"
   tell me to say "point 2"
   tell me to set attachmentsFolder to (path to documents folder as text) & "4attachments:"
   tell me to say "point 3"
   if not (exists folder attachmentsFolder) then
       tell me to say "the folder " & attachmentsFolder & " doesn't exist!"
       error "the folder " & attachmentsFolder & " doesn't exist!"
   end if
   tell me to say "point 4"
end tell
say "point 5"
tell application "Mail"
   set selectedMessages to theMessages
   tell me to say "point 6"
   repeat with theMessage in selectedMessages
       tell me to say "point 7"
       set theAttachments to theMessage's mail attachments
       tell me to say "point 8"
       tell me to say (get count of theAttachments) --> say 1
       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 to say "point 10"
               tell me to say originalName --> say "bouquins.rtf"
               if originalName contains ":" then
                   set savePath to my remplace(originalName, ":", "/")
               end if
               set savePath to attachmentsFolder & originalName
               tell me to say "point 11"
               tell me to say savePath --> say "SSD 500:Users:myHome:Documents:4attachments:bouquins.rtf"
               tell me to close access (open for access savePath) # THE TIP
               tell me to 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
                       *)

               tell me to set savePath to savePath as «class furl»
               tell me to say "point 13"
               save theAttachment in savePath
               tell me to say "point 14"
           end try
       end repeat # theAttachment
   end repeat # theMessage
end tell # Mail


#=====
(*
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

#=====

This late version behaves flawlessly so I'm hitting a wall : why is the instruction

Applescript:

set theAttachments to theMessage's mail attachments

failing when the script is triggered by the rule ?

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 13 janvier 2017 12:47:45

Offline

 

#14 2017-01-13 10:49:21 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

I found this info about having Applescript execute properly under Mail rules :

http://www.macandiostips.com/Mac_and_iO … Rules.html

The article is from 2013, so its way back under earlier OS X versions I guess. But the writer has a point, in the middle part... Quoting:

"But I have found that the most problems appear when running scripts are combined in a rule that also moves the message to another mailbox. It is almost like the moving will appear asynchronous such that the script fails to find it and therefore cannot parse the necessary info it needs to run right."

--

I removed all my other rule actions (mark email as read and move email to a different email-folder).  Then it actually seems to work. Leaving just the "Run applescript" action.

I can live with marking the emails as read manually myself, or if it would be possible to include that in the applescript it self.. at some point :

Applescript:

set read status of selectedMessages to true

Niklas

Offline

 

#15 2017-01-13 11:26:37 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Thanks but the script which I am testing is the only item triggered by the rule. This one moves nothing, changes no attribute. It only triggers the script.
I studied an other track to do the job but it failed.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 13 janvier 2017 18:26:30

Last edited by Yvan Koenig (2017-01-13 12:29:33 pm)

Offline

 

#16 2017-01-14 10:05:06 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

@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

Applescript:

(*
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.

Applescript:

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

Last edited by Yvan Koenig (2017-01-14 11:29:41 am)

Offline

 

#17 2017-01-14 11:33:34 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Im on Sierra 10.12.2 now.  And Apple Mail.app version 10.2 (3259).

Im currently able to save the attachments in a folder (dropbox folder if that matters) using a rule that triggers this script :

Applescript:

using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       set attachmentsFolder to "Users:niklas:Dropbox:folder1:folder2:"
       --set attachmentsFolder to (path to documents folder as text) & "4attachments:"
       --set attachmentsFolder to (path to desktop as text) & "4attachments:"
       --return attachmentsFolder
       tell application "System Events"
           if not (exists folder attachmentsFolder) then error "the folder " & attachmentsFolder & " doesn't exist!"
       end tell
       tell application "Mail"
           set theMessages to the selection
           set selectedMessages to theMessages
           repeat with theMessage in selectedMessages
               repeat with theAttachment in theMessage's mail attachments
                   # CAUTION: if the Hfs name of an attachment contain a slash, Mail replace it by a colon
                   try
                       set originalName to name of theAttachment
                       if originalName contains ":" then
                           set savePath to my remplace(originalName, ":", "/")
                       end if
                       set savePath to attachmentsFolder & originalName
                       
                       close access (open for access savePath) # THE TIP
                       # 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
               *)

                       tell me to set savePath to savePath as «class furl»
                       save theAttachment in savePath
                   end try
               end repeat # theAttachment
           end repeat # theMessage
           set read status of theMessages to true
       end tell # Mail
   end perform mail action with messages
end using terms from

#=====
(*
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

#=====


But it only seems to work when having just one action connected to the Mail Rule, that is "Run Applescript" so Im not able to move the message to a specific email folder or even mark it as read..

(This is just my thoughts, but it appears when adding more actions to the rule in mail, Mail.app sort the actions uncorrectly, i.e moving the first action (that I would like to be) "Run Applescript" to the bottom if I add "Mark email as read" and "Move to different folder". So thats why Mail.app looses the references to the mail that applescript is supposed to be working with later on..  But I don't know how to confirm that.)

Im too novice with Applescript, been using it for about a month or two, I've just googled and picked up bits and pieces from here and there and put together my script smile  But its a lot of fun smile  Maybe there is a way to solve this with the unique message id's, I wouldn't know how to do that yet.

Niklas





Yvan Koenig wrote:

@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.

I'm wondering if it's not better to use a rule to move the messages into a dedicated mailbox and trigger a script which would run an external script to do the job.

The problem would be : how to pass the references to the messages ?
I know how to pass strings but can't coerce message reference to string.
Is it a way for a script to call a message of Mail when it know only it's unique message id ?

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 14 janvier 2017 17:05:02

Offline

 

#18 2017-01-14 12:27:48 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

I will try to run the system in English to see if the oddity striking here is linked to the French localisation.
The problem has nothing common with the inability to write in a custom folder which is solved by the
close access (open for access savePath) # THE TIP instruction.
Here, when the script is triggered by a rule it fails when it reach the instruction :
repeat with theAttachment in theMessage's mail attachments
It's to try to see what is failing that I split the instruction in two parts:
set theAttachments to theMessage's mail attachments
repeat with theAttachment in theAttachments.

When testing in Script Debugger I discovered that  repeat with theAttachment in theMessage's mail attachments has no raw format (chevrons one).
This is why I edited as :
set theAttachments to every attachment of theMessage
which, when I ask for raw syntax appears as :
set theAttachments to every «class atts» of theMessage

Everything behaves well when the script is called by:

Applescript:

tell application "Mail"
set test_list to selection
tell me to perform mail action with messages (test_list)
end tell

When triggered by a rule which does no other task, the script execute the say "point 7" instruction but fails to execute the instruction
set theAttachments to every attachment of theMessage.
The fact that the script fails if you ask the rule to do what you described before isn't too surprising.
Changing the read status isn't a problem but when the rule move a message in a mailbox, it seems that the script receive the original location, not the new one.

In the message #16 you may find the instructions to add to your code so that it change the read status and moves the message in a dedicated mailbox.

Applescript:

end repeat # theAttachment # existing instruction

set read status of theMessage to true
-- tell me to say "point 15 message " & pass
move theMessage to mailbox "aaa/aac" # EDIT to fit your needs
-- tell me to say "point 16 message " & pass

end repeat # theMessage # existing instruction

I'm a bit skeptical because the instruction
set attachmentsFolder to "Users:niklas:Dropbox:folder1:folder2:"
doesn't define a valid path. The name of the volume is missing !

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 14 janvier 2017 19:27:38

Last edited by Yvan Koenig (2017-01-14 12:37:54 pm)

Offline

 

#19 2017-01-14 01:06:04 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

It's perfectly foolish.
I re-tested with the code borrowed from your late message and the attachment was not moved.

I re-booted in English and the same code behaved flawlessly so I thought that there is a bug dedicated to French localization.

I re-booted in French and to be sure I made an other attempt. This time the attachments were correctly saved.

I must assume that something was odd in a file which was repaired when I ran in English.

I took time to test the script with the added instructions listed before.
It worked flawlessly. Of course you will have to edit the instruction moving to a mailbox because I doubt that "aaa/aac" exists on your machine.


Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 14 janvier 2017 20:05:54

Offline

 

#20 2017-01-15 10:00:16 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

At last I found why your script seems to work.
It's not working upon the list of messages massed by the rule, it's working upon what is selected in Mail.

Applescript:

tell application "Mail"
set theMessages to the selection # THE INSTRUCTION WHICH FOOLED US
set selectedMessages to theMessages

Remove the instruction  grabbing the selection and you will not get the attachments saved.
If we want to save files attached to selected messages, a script triggered by a rule isn't the good scheme.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 15 janvier 2017 17:00:03

Offline

 

#21 2017-01-18 03:27:26 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

I stand corrected.

My script with Mail.app rules does not work - all the time.

It doesn't save the attachment as its supposed to. Its not stable.

Back to the drawing board smile

Niklas

Offline

 

#22 2017-01-18 04:30:31 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

The script does its duty when it is called from the Scripts Editor. It fails when it's called by a Mail's rule.
It's not the only one behaving this way.
There is at least one other thread about this kind of problem.
My understanding is that it's clearly an Apple problem.

The tip :
   close access (open for access savePath)
solves only the oddity due to the inability to save in most of the folders.
This time the problem is that when triggered by a rule, Mail fails to execute several actions upon the attached files.
I'm afraid that we will have to live with that for "some" time.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mercredi 18 janvier 2017 11:30:26

Offline

 

#23 2017-01-30 07:40:17 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

I have solved this problem using an Automator script, running once every night (with the help of iCal / Calendar.app)

Until the guys at Apple fix this issue.

Niklas

Offline

 

#24 2017-02-17 06:46:52 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 2957

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

With the current version of beta 10.12.4 the tip :
close access (open for access savePath)
is no longer needed (but it doesn't hurt)


Yvan KOENIG running Sierra 10.12.3 in French (VALLAURIS, France) vendredi 17 février 2017 13:46:44

Offline

 

#25 2017-02-17 10:02:01 am

NickeZ28
Member
From: Sweden
Registered: 2016-11-01
Posts: 15

Re: Mail.app save attachment - stopped working after Sierra 10.12.2 update

Nice.. does that mean they are fixing this issue for the coming OS X release ?

Niklas

Offline

 

Board footer

Powered by FluxBB

[ Generated in 0.481 seconds, 9 queries executed ]

RSS (new topics) RSS (active topics)