• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Mail and Attachments
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mail and Attachments


  • Subject: Re: Mail and Attachments
  • From: Neil Faiman <email@hidden>
  • Date: Sun, 7 Aug 2005 15:17:46 -0400

On Jun 26, 2005, at 12:16 AM, David Glass wrote:

Is there any way to save a message's attachment(s) to a programmatically specified folder for further processing via AppleScript?

Here is a script that Andreas Amann posted somewhere a few years ago. It's probably more general than you need, but it illustrates the necessary techniques, and you can customize it to do just what you want.


And while you're at it, take a look at Andreas's excellent Mail Scripts web site at <http://homepage.mac.com/aamann/Mail_Scripts.html>.

Regards,

    Neil Faiman

property theAttachmentPath : (path to "desk") as string

on run
tell application "Mail"
set theSelection to selection
if (count of theSelection) > 0 then
set attachmentCount to my saveAttachments(theSelection)
activate -- make sure Mail is frontmost app after running the script...
if attachmentCount > 0 then
display dialog (attachmentCount as string) & " attachment(s) found in selected message(s) and saved to folder " & return & theAttachmentPath buttons {"OK"} default button 1
else
display dialog "No attachments found in selected message(s)." buttons {"OK"} default button 1
end if
else
display dialog "Please select one or more messages in Mail before running this script." buttons {"OK"} default button 1
end if
end tell
end run


on perform_mail_action(info)
    tell application "Mail"
        set selectedMessages to |SelectedMessages| of info
        my saveAttachments(selectedMessages)
    end tell
end perform_mail_action

on saveAttachments(messageList)
set attachmentCount to 0
tell application "Mail"
repeat with eachMessage in messageList
-- check for attachments
set theHeaders to all headers of eachMessage
set AppleScript's text item delimiters to "multipart/mixed"
if number of text items in theHeaders > 1 then
-- we have a multipart/mixed message so we might have attachments...
set theSource to source of eachMessage
set AppleScript's text item delimiters to ""
set tempFileName to sender of eachMessage & " Attachments"
set tempFile to theAttachmentPath & tempFileName
open for access file tempFile with write permission
set eof file tempFile to 0
write theSource to file tempFile as string
close access file tempFile
tell application "Finder" to set fileCount to count of every file in alias theAttachmentPath
tell application "StuffIt Expander" to expand {alias tempFile} into alias theAttachmentPath with delete originals
-- now try to clean up files we don't want...
-- this can be different due to pref settings in Stuffit Expander :-)
tell application "Finder"
set useSubFolder to true
set theFolders to get (every folder in alias theAttachmentPath whose name is (tempFileName & " Folder"))
if (count of theFolders) = 1 then
-- option 1: files in newly created subfolder:
set thePath to theAttachmentPath & tempFileName & " Folder"
else
-- option 2: no subfolder, files directly on Desktop:
set thePath to theAttachmentPath
set useSubFolder to false
end if
delete (every file in alias thePath whose name starts with tempFileName & ".attachment")
if (count of every file in alias thePath) = 0 then
delete alias thePath
end if
if useSubFolder then
set attachmentCount to attachmentCount + (count of every file in alias thePath)
else
set attachmentCount to attachmentCount + (count of every file in alias thePath) - fileCount + 1
end if
end tell
end if -- multipart/mixed message
end repeat
end tell
return attachmentCount
end saveAttachments

Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Fwd: Mail and Attachments
      • From: Neil Faiman <email@hidden>
  • Prev by Date: Re: Finder Info to get real audio media source URL
  • Next by Date: Re: Finder Info to get real audio media source URL
  • Previous by thread: Re: Mail and Attachments
  • Next by thread: Fwd: Mail and Attachments
  • Index(es):
    • Date
    • Thread