[AppleScript] Mail application to download attachment

Discussion in 'Mac Programming' started by lazenca, Jan 14, 2009.

  1. lazenca macrumors newbie

    Joined:
    Jan 14, 2009
    #1
    Hi I am using AppleScript for the Mail app.
    This is the workflow I wanted:

    - Check any incoming messages with the subject that contains the word "SOA"
    - Place them into the mailbox called SOA
    - Save the attachment into my hard disk and in a folder of mine

    This is the Mail rule I set in preference
    [​IMG]

    The AppleScript I used:

    Code:
    using terms from application "Mail"
    	on perform mail action with messages theMessages
    		tell application "Mail"
    			repeat with theMessage in theMessages
    				if name of theMessage's mailbox is "SOA" then
    					try
    						set numAttachments to number of theMessage's mail attachments
    						if numAttachments is 1 then
    							set theAttachment to first item of theMessage's mail attachments
    							set savePath to "Macintosh HD:Users:benson:Public:Drop Box:SOA:"
    							set attachmentFileName to savePath & theAttachment's name
    							
    							tell application "Finder"
    								try
    									delete the attachmentFileName
    								end try
    							end tell
    							
    							save theAttachment in attachmentFileName
    							
    							
    							if the mailbox "soa" exists then
    								move theMessage to the mailbox "SOA"
    							end if
    						end if
    					on error errMsg number errNumber
    						display dialog errMsg
    					end try
    				end if
    			end repeat
    		end tell
    	end perform mail action with messages
    end using terms from
    The problem is, whenever any email with the subject SOA is being received, it automatically goes into mailbox SOA but doesnt save the attachment into my hard disk.

    I will then need to manually right click on the message(s) and choose "Apply Rule" for it to save the attachment file into my own folder.

    Appreciate help. Thanks alot!:apple:
     
  2. lazenca thread starter macrumors newbie

    Joined:
    Jan 14, 2009
  3. samaral3079 macrumors newbie

    Joined:
    Jan 4, 2012
    #3
    same issue

    I have a similar script and it will only work when apply rule is selected did you find any solution?
     
  4. Mark FX macrumors member

    Mark FX

    Joined:
    Nov 18, 2011
    Location:
    West Sussex, UK
    #4
    I think the problem is that you are moving the message before the attachment has been moved.
    Try moving the attachment first, then moving the message second, you could probably rearrange the rule tasks to try this.

    Regards Mark
     

Share This Page