Wednesday, January 19, 2022

#1 2021-05-20 07:59:34 am

rick_260
Member
Registered: 2012-06-28
Posts: 72

Help Modifying Mac Mail Script - select .PDF attachments only

Mail Version 12.4.
OS Mojave

i have a working script that uploads attachments from an email to specific location on a network. the issue is that i need the script to only upload .PDF's and ignore other attachment extensions ex: .jpg, .png etc. currently it will upload all attachments.

Applescript:



using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       set attachmentsFolder to "my:path" 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

thank you in advance

Last edited by rick_260 (2021-05-20 08:02:04 am)

Offline

 

#2 2021-05-20 04:07:06 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 1055

Re: Help Modifying Mac Mail Script - select .PDF attachments only

Try replacing "repeat with theAttachment in theMessage's mail attachments" with

 repeat with theAttachment in theMessage's mail attachments whose its name ends with ".pdf"

Offline

 

#3 2021-05-21 04:34:05 am

rick_260
Member
Registered: 2012-06-28
Posts: 72

Re: Help Modifying Mac Mail Script - select .PDF attachments only

there wasn't any errors, but unfortunately the change to the script did not give any results
ie neither .pdf or .jpeg was uploaded to destination.

Offline

 

#4 2021-05-21 08:30:59 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: Help Modifying Mac Mail Script - select .PDF attachments only

rick_260 wrote:

there wasn't any errors, but unfortunately the change to the script did not give any results
ie neither .pdf or .jpeg was uploaded to destination.


All your statements are inside try block. So, you script hide the errors anyway. I think, the problem of your script is general, not tided with extensions - the destination folder and destination file should exist before attempts to save to it. The following script shows workaround to avoid this issue, but it isn't tested by me:

Applescript:


using terms from application "Mail"
   on perform mail action with messages selectedMessages for rule theRule
       set parentFolder to "my"
       set folderName to "path"
       my makeNewAttachmentsFolder(parentFolder, folderName)
       set attachmentsFolder to parentFolder & ":" & folderName & ":"
       tell application "Mail"
           try
               repeat with theMessage in selectedMessages
                   repeat with theAttachment in (theMessage's mail attachments whose ((its name ends with ".pdf") or (its name ends with ".jpg")))
                       set originalName to name of theAttachment
                       my makeNewEmptyFile(attachmentsFolder, originalName)
                       save theAttachment in file (attachmentsFolder & originalName)
                   end repeat
               end repeat
           end try
       end tell
   end perform mail action with messages
end using terms from

on makeNewAttachmentsFolder(parentFolder, folderName)
   try
       tell application "Finder"
           make new folder at folder parentFolder with properties {name:folderName}
       end tell
   end try
end makeNewattachmentsFolder

on makeNewEmptyFile(attachmentsFolder, newFileName)
   try
       tell application "Finder"
           make new file at folder attachmentsFolder with properties {name:newFileName}
       end tell
   end try
end makeNewEmptyFile

Last edited by KniazidisR (2021-05-21 08:51:49 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-05-21 12:26:18 pm

rick_260
Member
Registered: 2012-06-28
Posts: 72

Re: Help Modifying Mac Mail Script - select .PDF attachments only

Applescript:

set parentFolder to "my"
set folderName to "path"

i assume the above is just my original

Applescript:

set attachmentsFolder to "my:path" as rich text

my folder path is "Volume:Folder"
(a folder within a volume) perhaps i expressed it wrong in my original script??

test email consisted of (2) .JPG's and (1) .PDF

the result of the script from the last post was:
no .PDF's uploaded and (1) .JPG uploaded without file type (name only)

Offline

 

#6 2021-05-21 04:57:39 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 1055

Re: Help Modifying Mac Mail Script - select .PDF attachments only

Hi. I can't tell if you're just withholding the true path for security or if it's actually malformed. This works as a standalone. If you watch the event log, you should be able to fix whatever's wrong in the rule after removing the try block.


Applescript:

set saveLocus to (path to desktop folder as text) & "TEST:" --assumes folder named 'TEST' on desktop
log saveLocus -- for verifying the HFS path
tell application "Mail"
   repeat with aMessage in (get selection) --assumes message selected
       repeat with isAttachment in (get aMessage's mail attachments whose its name ends with ".pdf") --assumes attachment exists, but won't error, if not
           tell isAttachment to save it in file (saveLocus & name)
       end repeat
   end repeat
end tell

Offline

 

#7 2021-05-22 12:20:13 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: Help Modifying Mac Mail Script - select .PDF attachments only

rick_260 wrote:


the result of the script from the last post was:
no .PDF's uploaded and (1) .JPG uploaded without file type (name only)


Following script is tested by me, and works as expected. A added ignoring case for extensions filtering (to save only pdfs):

Applescript:


using terms from application "Mail"
   on perform mail action with messages selectedMessages for rule theRule
       set parentFolder to (path to desktop folder) as string
       set folderName to "AttachmentsFolder"
       my makeNewAttachmentsFolder(parentFolder, folderName)
       set attachmentsFolder to parentFolder & folderName & ":"
       tell application "Mail"
           try
               ignoring case
                   repeat with theMessage in selectedMessages
                       repeat with theAttachment in (theMessage's mail attachments whose (its name ends with ".pdf"))
                           set originalName to name of theAttachment
                           my makeNewEmptyFile(attachmentsFolder, originalName)
                           save theAttachment in file (attachmentsFolder & originalName)
                       end repeat
                   end repeat
               end ignoring
           end try
       end tell
   end perform mail action with messages
end using terms from

on makeNewAttachmentsFolder(parentFolder, folderName)
   try
       tell application "Finder" to make new folder at folder parentFolder with properties {name:folderName}
   end try
end makeNewAttachmentsFolder

on makeNewEmptyFile(attachmentsFolder, newFileName)
   try
       tell application "Finder" to make new file at folder attachmentsFolder with properties {name:newFileName}
   end try
end makeNewEmptyFile

Last edited by KniazidisR (2021-05-22 12:25:50 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#8 2021-05-24 04:28:31 am

rick_260
Member
Registered: 2012-06-28
Posts: 72

Re: Help Modifying Mac Mail Script - select .PDF attachments only

Thank you KniazidisR!!! That worked great!!! Perfect thank you so much!!!!


Now i just have to figure out how make the PDF's got to a folder on the network. LOL

Offline

 

#9 2021-05-24 05:32:07 am

rick_260
Member
Registered: 2012-06-28
Posts: 72

Re: Help Modifying Mac Mail Script - select .PDF attachments only

this is what i came up with based on last script. it works. not sure its right but it does work lol. any improvements would be great appreciated

Applescript:


using terms from application "Mail"
   on perform mail action with messages theMessages for rule theRule
       set attachmentsFolder to "Volume:Folder:" as rich text
       tell application "Mail"
           set selectedMessages to theMessages
           try
               repeat with theMessage in selectedMessages
                   repeat with theAttachment in (theMessage's mail attachments whose (its name ends with ".pdf"))
                       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

Last edited by rick_260 (2021-05-25 07:21:31 am)

Offline

 

#10 2022-01-11 02:27:34 pm

jprokos
Member
Registered: 2019-09-02
Posts: 7

Re: Help Modifying Mac Mail Script - select .PDF attachments only

@KniazidisR

I have been trying various scripts for a week and nothing worked. I suspected this was a Security issue and not a script issue and I was correct. Something in your script finally triggered the Security window to pop up! I am so grateful.

Security Notification


KniazidisR wrote:


Following script is tested by me, and works as expected. A added ignoring case for extensions filtering (to save only pdfs):

Applescript:


using terms from application "Mail"
   on perform mail action with messages selectedMessages for rule theRule
       set parentFolder to (path to desktop folder) as string
       set folderName to "AttachmentsFolder"
       my makeNewAttachmentsFolder(parentFolder, folderName)
       set attachmentsFolder to parentFolder & folderName & ":"
       tell application "Mail"
           try
               ignoring case
                   repeat with theMessage in selectedMessages
                       repeat with theAttachment in (theMessage's mail attachments whose (its name ends with ".pdf"))
                           set originalName to name of theAttachment
                           my makeNewEmptyFile(attachmentsFolder, originalName)
                           save theAttachment in file (attachmentsFolder & originalName)
                       end repeat
                   end repeat
               end ignoring
           end try
       end tell
   end perform mail action with messages
end using terms from

on makeNewAttachmentsFolder(parentFolder, folderName)
   try
       tell application "Finder" to make new folder at folder parentFolder with properties {name:folderName}
   end try
end makeNewAttachmentsFolder

on makeNewEmptyFile(attachmentsFolder, newFileName)
   try
       tell application "Finder" to make new file at folder attachmentsFolder with properties {name:newFileName}
   end try
end makeNewEmptyFile



Model: iMac 19,1
AppleScript: 2.11
Browser: Safari 605.1.15
Operating System: macOS 10.15

Last edited by jprokos (2022-01-11 02:29:25 pm)


Filed under: mail, Attachments

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)