• 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
script to extract email attachments
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

script to extract email attachments


  • Subject: script to extract email attachments
  • From: David Reed <email@hidden>
  • Date: Sat, 9 Apr 2005 20:41:03 -0400


I'm attempting my first applescript (I'm an experienced programmer in many other languages including C, C++, and Python). I'm attempting to write a script to extract email attachments into a certain directory based on the sender and the subject of the email. I've set up a rule in Mail.app to run the applescript and it works fine if I select a message and run the rule manually but it doesn't work automatically like my other rules in Mail.app that move messages to folders as the messages are pulled off the server.


Any ideas?

I've tried to copy the script below, but I don't know how it will come through.

Thanks,
Dave


-- based on code found at: -- http://discussions.info.apple.com/webx?email@hiddenl9DW0l.3@.68a78adf/1

-- modified by David Reed 4/9/2005
-- StufIt Expander preferences need to be set to "Create Surrounding Folder" "Never"
-- for it to work with multiple attachments in one email


-- seem to have to run Rules from Mail.app manually to get it to extract

-- property theAttachmentPath : (path to "desk") as string property theAttachmentPath : "Macintosh HD:Users:dreed:Labs"

using terms from application "Mail"
on perform mail action with messages theMessages
set attachmentCount to 0
tell application "Mail"
repeat with eachMessage in theMessages
-- 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 theSender to {extract name from sender of eachMessage, extract address from sender of eachMessage}
set theAddress to word 1 of item 2 of theSender
set theSubject to subject of eachMessage
set theClass to get text 1 through 5 of theSubject
set theClassPath to theAttachmentPath & ":" & theClass
set theDir to theAttachmentPath & ":" & theClass & ":" & theAddress

set tempFileName to "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"
set fileCount to count of every file in alias theAttachmentPath

if folder theClassPath exists then
-- do nothing
else
make new folder in folder theAttachmentPath with properties {name:theClass}
end if

if folder theDir exists then
-- do nothing
else
make new folder in folder theClassPath with properties {name:theAddress}
end if
end tell

tell application "StuffIt Expander"
expand {alias tempFile} into {alias theDir} with delete originals
quit
end tell
-- 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
end perform mail action with messages
end using terms from

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:
    • Re: script to extract email attachments
      • From: Brian <email@hidden>
  • Prev by Date: Re: Applescript-users Digest, Vol 2, Issue 229
  • Next by Date: Re: script to extract email attachments
  • Previous by thread: Re: Applescript-users Digest, Vol 2, Issue 229
  • Next by thread: Re: script to extract email attachments
  • Index(es):
    • Date
    • Thread