Saving mail attachments in Mail.app

I am trying to create a rule action which saves a mail attachement to a specified folder when the mail rule is satisfied. I have the following script (which seems to do nothing).

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			set attachment_name to text returned of the name of the first mail attachment of theMessages
			save first mail attachment of theMessages in ":Users:cjohnson:Desktop:Scanned and Recognized Documents:attachment_name"
		end tell
	end perform mail action with messages
end using terms from

These will be .pdf files which are further processed by Acrobat via a folder action, which is already up and running. Any help is greatly appreciated. I don’t fully understand some of the terms such as “theMessages,” which is taken from Apple’s sample script. Does this need to be defined or is it ccovered under the “using terms from application “Mail”” line?

If you read through this thread, nearly all of your questions will be answered.

Please post back to this thread if you continue to have difficulties.

Thank you Craig for pointing me to this. I see your script posted on 2006-01-23 and I think with some modifications, it will do the trick. I, however, am having a little trouble underrstanding the variables in the script (if that is what they are). For examble, the use of the term “The_Messages.” Does this variable need to be set first, or does applescript implicitly understand it?

The variable [The_Messages] is set in the early portion of the script:

using terms from application "Mail" on perform mail action with messages The_Messages set today_folder to CheckOrMakeFolder((current date)'s short date string) tell application "Mail" to repeat with This_Message in The_Messages
The second line here is the real beginning of the handler, and the variable [The_Messages] is set to a list of messages that Mail.app is sending to this handler. This is usually a list of one message, but it is important to remember that whenever using the [perform mail action with messages] handler, it treats whatever mail message it receives as a list. That is way you see the beginning of the repeat here on line 4. Even if the [The_Messages] variable is set to a list of one, it cannot be treated appropriately unless you either loop through the list (as this script does), or simply assign the variable [This_Message] to item 1 of The_Messages.

Good luck, and stop back by if you need more clarification.

This works for me - it scans all inboxes and sent mailboxes, and saves all the attachments to the folder “Attachments” on the Desktop. If attachments have the same filename, it adds a number to the end until it is unique.

tell application "Finder" to set pathToAttachments to (path to desktop folder as string) & "Attachments:"
tell application "Mail"
	set theMasterList to {}
	repeat with theMailbox in (inbox's mailboxes & sent mailbox's mailboxes)
		set theMasterList to theMasterList & (messages of theMailbox whose mail attachments is not {})
	end repeat
	
	repeat with theMessage in theMasterList
		set suffix to 0
		repeat with theAttachment in theMessage's mail attachments
			if MIME type of theAttachment is "image/jpeg" then
				set theFileName to pathToAttachments & (theMessage's id as string) & space & theAttachment's name
				try
					save theAttachment in theFileName
				on error
					set suffix to suffix + 1
					set theFileName to theFileName & " - " & (suffix as string)
					save theAttachment in theFileName
				end try
			end if
		end repeat
	end repeat
end tell

If you wanted to code it as a rule on incoming mail, just remove the first code block and surround the second one with the “on perform mail action with messages theMasterList”:


using terms from application "Mail"
	on perform mail action with messages theMasterList
		tell application "Finder" to set pathToAttachments to (path to desktop folder as string) & "Attachments:"
		repeat with theMessage in theMasterList
			set suffix to 0
			repeat with theAttachment in theMessage's mail attachments
				set theFileName to pathToAttachments & (theMessage's id as string) & space & theAttachment's name
				try
					save theAttachment in theFileName
				on error
					set suffix to suffix + 1
					set theFileName to theFileName & " - " & (suffix as string)
					save theAttachment in theFileName
				end try
			end repeat
		end repeat
	end perform mail action with messages
end using terms from

Here is what I came up with:

  1. Rule Action:
using terms from application "Mail"
	on perform mail action with messages The_Messages
		tell application "Mail" to repeat with this_message in The_Messages
			set attachement_file to first mail attachment of this_message
			set attachment_name to name of the first mail attachment of this_message
			save attachement_file in "Users:cjohnson:Desktop:Scanned and Recognized Documents:" & attachment_name
		end repeat
	end perform mail action with messages
end using terms from
  1. Folder Action:
on adding folder items to this_folder after receiving these_items
	repeat with i from 1 to number of items in these_items
		set this_item to item i of these_items
		set the item_info to info for this_item
		--OCR Commands
		activate application "Adobe Acrobat 7.0 Professional"
		tell application "Adobe Acrobat 7.0 Professional"
			open this_item
		end tell
		tell application "System Events"
			tell application process "Acrobat"
				-- GUI Scripting statements:
				--Statement 1				
				click the menu item "Start..." of menu 1 of menu item "Recognize Text Using OCR" of the menu "Document" of menu bar 1
				delay 4
				--Statement 2				
				click radio button "All pages" of group 1 of group 2 of group 1 of window "Recognize Text"
				delay 4
				--Statement 3
				click button "OK" of group 1 of window "Recognize Text"
				--Save
				tell application "Adobe Acrobat 7.0 Professional"
					save the front document
					close the front document
				end tell
			end tell
		end tell
	end repeat
end adding folder items to

Any suggestions on making more lean and mean are greatly appreciated.

I’m not experienced in Acrobat scripting, but I did check to see if there was a direct command to do OCR - there isn’t. It does have its own set of of commands to invoke menu items, though - that might be faster to use those.

Did the OCR triggered by GUI scripting actually work? Seems as if it should - although you will miss error messages that it puts up on every page that it finds an excuse why the page can’t be OCR-ed.

Yes, the script does actually work. I did look into the menu item commands to begin with, but was unable to find the appropriate internal menu names. I looked through the Adobe publication “Acrobat and PDF Library API Reference.” A lot of what is in that document looked like gibberish to me. Could it be that Adobe discourages scripting of its OCR function because it sells a separate server product that handles big OCR jobs?