Re: mail.app save all attachments
Re: mail.app save all attachments
- Subject: Re: mail.app save all attachments
- From: Andreas Amann <email@hidden>
- Date: Mon, 29 Dec 2003 19:16:42 -0800
The following script works - both when run as well as when triggered
from a rule in Mail (requires 10.2.3 or higher, including 10.3.x).
Requires StuffIt Expander (for decoding the MIME parts)
And, of course, there will be some spurious return characters added by
the mailer...
Andreas
--
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
good day!
i'm looking for a proper script that can be attached to a rule in
mail.app that, on rule match, saves all the attachments of the mail to
a specified folder and then deletes the mail.
i haven't really found any useful info via google/apple.com on that.
any hints/suggestions/solutions would be appreciated,
thanks a lot,
stefan.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.