Applescript help

Discussion in 'Mac Apps and Mac App Store' started by dangerfish, May 5, 2014.

  1. dangerfish macrumors 6502

    Joined:
    Aug 28, 2007
    #1
    I am looking to set up an Applescript/Automator action that will automatically save any pdf email attachments to my desktop (or other folder). I believe the first step is to set up an email rule as shown below. But I don't know anything about Applescript. Is there a simple script that would run and save pdf email attachments to my desktop? This would be on 10.9.

    TIA
     

    Attached Files:

  2. Bruno09 macrumors 68020

    Joined:
    Aug 24, 2013
    Location:
    Far from here
    #2
  3. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #3
    Try this :

    Code:
    using terms from application "Mail"
    	on perform mail action with messages theMessages for rule theRule
    		set theOutputFolder to "LeopardFirewire:Users:test:Desktop:Stuff:"
    		tell application "Mail"
    			repeat with theMessage in theMessages
    				if theMessage's mail attachments is not {} then
    					repeat with theAttachment in theMessage's mail attachments
    						set theFileName to theOutputFolder & theAttachment's name
    						try
    							save theAttachment in theFileName
    						on error errnum
    						end try
    					end repeat
    				end if
    			end repeat
    		end tell
    	end perform mail action with messages
    end using terms from
    
    Note : You need to change theOutputFolder to your desktop or other folder. To get the right path to your desktop folder run this in another script editor window and paste your result into the script.
     

    Attached Files:

  4. Bruno09 macrumors 68020

    Joined:
    Aug 24, 2013
    Location:
    Far from here
    #4
    To dangerfish :

    you will need to save the script into : ~/Library/Application scripts/com.apple.mail in order to have it show up in the drop down menu when you create the rule in Mail.
     
  5. dangerfish thread starter macrumors 6502

    Joined:
    Aug 28, 2007
    #5
    Thank you everyone! I have it working. I couldn't get Bruno's to work but Kryten's did.
    Now for the bonus, is there a way to automatically print the pdf to my default printer once it is saved to the desktop?
     
  6. Bruno09 macrumors 68020

    Joined:
    Aug 24, 2013
    Location:
    Far from here
    #6
    I can't have this working, nothing happens. Could you please help ?

    The Mail rule is : If Attachment = picture ----> Run Applescript "mail2"
    Name of the Rule : "images" (=pictures).

    The script "mail2" is saved in ~/Library/Application Scripts/com.apple.mail

    Thanks in advance.

    Code:
    using terms from application "Mail"
    	on perform mail action with messages theMessages for rule images
    		set theOutputFolder to "MBPHD:Users:bruno:Documents:Attachments:"
    		tell application "Mail"
    			repeat with theMessage in theMessages
    				if theMessage's mail attachments is not {} then
    					repeat with theAttachment in theMessage's mail attachments
    						set theFileName to theOutputFolder & theAttachment's name
    						try
    							save theAttachment in theFileName
    						on error errnum
    						end try
    					end repeat
    				end if
    			end repeat
    		end tell
    	end perform mail action with messages
    end using terms from
     
  7. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #7
    Try :

    Code:
    on perform mail action with messages theMessages for rule theRule
    or without the for rule argument.

    Code:
    on perform mail action with messages theMessages
     
  8. Bruno09 macrumors 68020

    Joined:
    Aug 24, 2013
    Location:
    Far from here
    #8
    Thank you for your help.

    Tried both and still no luck.

    I am certainly missing something :(
     
  9. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #9
    MBPHD is the name of your hard disk? Try this with selecting a message in Mail with a picture attachment.

    Code:
    using terms from application "Mail"
    	on perform mail action with messages theMessages for rule theRule
    		set theOutputFolder to "MBPHD:Users:bruno:Documents:Attachments:"
    		tell application "Mail"
    			repeat with theMessage in theMessages
    				if theMessage's mail attachments is not {} then
    					repeat with theAttachment in theMessage's mail attachments
    						set theFileName to theOutputFolder & theAttachment's name
    						--try
    						save theAttachment in theFileName
    						--on error errnum
    						--end try
    					end repeat
    				end if
    			end repeat
    		end tell
    	end perform mail action with messages
    end using terms from
    
    (*
    Next block is for running the script in Script Editor with some mail messages selected in the Mail application. If everything works remove the entire block and attach script to rule.
    *)
    -----------------ONLY NEEDED FOR TESTING FROM WITHIN SCRIPT EDITOR--------
    
    tell application "Mail"
    	set myMessages to selection
    	tell me to perform mail action with messages myMessages
    end tell
    
    -----------------REMOVE WHEN TESTING IS DONE !!!!!!-----------------------
     
  10. Bruno09, May 6, 2014
    Last edited: May 6, 2014

    Bruno09 macrumors 68020

    Joined:
    Aug 24, 2013
    Location:
    Far from here
    #10
    This one gives me an error :

    "Applescript error : parameter "class pmar" missing for "event emalcpma" "

    number -1701 partial result «handler «event emalcpma»» from «class pmar»

    And yes, MBPHD is my boot disk.

    EDIT : after a reboot, the scripts runs but fails to save the attachments.

    I selected one mail in Mail, with 5 pictures attached, and ran the script from Applescript editor.

    Result :

    count every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
    --> 5
    get name of item 1 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
    --> "IMG_6833.jpeg"
    save item 1 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6833.jpeg"
    --> error number -10000
    get name of item 2 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
    --> "IMG_6835.jpeg"
    save item 2 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6835.jpeg"
    --> error number -10000
    get name of item 3 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
    --> "IMG_6832.jpeg"
    save item 3 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6832.jpeg"
    --> error number -10000
    get name of item 4 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
    --> "IMG_6840.jpeg"
    save item 4 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6840.jpeg"
    --> error number -10000
    get name of item 5 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier"
    --> "IMG_6842.jpeg"
    save item 5 of every mail attachment of message id 27700 of mailbox "Valérie-Olivier" in "MBPHD:Users:bruno:Documents:Attachments:IMG_6842.jpeg"
    --> error number -10000
    end tell
     
  11. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #11
    Sorry it doesn't work for you. I just ran a quick test and it still works for me however without the for rule argument.

    Code:
    on perform mail action with messages theMessages
     

    Attached Files:

  12. Bruno09, May 6, 2014
    Last edited: May 6, 2014

    Bruno09 macrumors 68020

    Joined:
    Aug 24, 2013
    Location:
    Far from here
    #12
    Still don't work, even without the for rule argument.

    Error 10000

    count every mail attachment of message id 30757 of mailbox "INBOX" of account "bruno"
    --> 1
    get name of item 1 of every mail attachment of message id 30757 of mailbox "INBOX" of account "bruno"
    --> "IMG_0071.jpeg"
    save item 1 of every mail attachment of message id 30757 of mailbox "INBOX" of account "bruno" in "MBPHD:Users:bruno:Documents:Attachments:IMG_0071.jpeg"
    --> error number -10000
    end tell

    @ kryten2 : which OS are you running, please ?
    (I am running Mavericks).



    EDIT : finally found the answer in an Apple Support Communities thread :

    From Mountain Lion (Lion ?) :

    "The script works for me. Just needs to be saved to correct spot in library and you can save your files ONLY to Downloads folder (whatever subfolder you want to create must be in Downloads) it will not save it into documents."

    Changed the destination folder to Downloads and it worked instantly :D

    Thank you for your help.
     
  13. kryten2, May 6, 2014
    Last edited: May 6, 2014

    kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #13
    Test was done on Leopard. I'll test on Mavericks too and edit my post with the result.

    Edit : On Mavericks you need to replace :

    Code:
    save theAttachment in theFileName
    with

    Code:
    save theAttachment in file theFileName
    Test was done on Mavericks saving to a folder on the Desktop and to a folder in the Documents folder. Both worked.
     

    Attached Files:

  14. dangerfish thread starter macrumors 6502

    Joined:
    Aug 28, 2007
    #14
    Thanks Kryten,

    I too was able to reliably save to the downloads folder but it was sporadic when trying to save to the desktop. Now it saves to the desktop just fine.

    Is there something we could add to the script to tell it to print the file to the default printer without the dialog box?

    Thanks!
     
  15. dangerfish thread starter macrumors 6502

    Joined:
    Aug 28, 2007
    #15
    I figured out the print syntax myself. Now when an email with a pdf attachment arrives, it is saved to my desktop and printed to my printer. I added the following text to the script:

    tell application "Officejet Pro L7600"
    print file theFileName
    end tell

    Thanks for all the help
     
  16. kryten2 macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #16
    You're welcome. Nice of you to figure this one out yourself. I don't have a printer but did a bit of searching and the code you posted came up a few times. Thank you for sharing it back.
     
  17. Bruno09, May 7, 2014
    Last edited: May 7, 2014

    Bruno09 macrumors 68020

    Joined:
    Aug 24, 2013
    Location:
    Far from here
    #17
    Thank you : this makes the script work indeed, I can save to whatever folder I want, MANUALLY (from Applescript Editor), when using :

    tell application "Mail"
    set myMessages to selection
    tell me to perform mail action with messages myMessages
    end tell

    However, the Mail rule does not work.

    Here is my Mail rule :

    "Attachment" is "Image"
    "Run Applescript" : "PJ images"

    [​IMG]

    And here is the script :

    Code:
    using terms from application "Mail"
    	on perform mail action with messages theMessages -- for rule theRule
    		set theOutputFolder to "MBPHD:Users:bruno:Desktop:"
    		tell application "Mail"
    			repeat with theMessage in theMessages
    				if theMessage's mail attachments is not {} then
    					repeat with theAttachment in theMessage's mail attachments
    						set theFileName to theOutputFolder & theAttachment's name
    						try
    							save theAttachment in file theFileName
    						on error errnum
    						end try
    					end repeat
    				end if
    			end repeat
    		end tell
    	end perform mail action with messages
    end using terms from

    EDIT : pfuuuuuuu..... finally figured out what the issue is :

    Drum rolls....

    DOES NOT work with POP accounts

    WORKS FINE with IMAP accounts !!

    Thank you for your help.

    Note : there is no need to use the disk name :
    set theOutputFolder to "Users:bruno:Folder:" works as well.
     
  18. kelli303 macrumors newbie

    Joined:
    Jul 24, 2014
    #18
    Can someone help me out with a script that notifies me when a PayPal button has been added to a webpage? I have created a script that refreshes the webpage for me but the PayPal button is not at the top of the page it is in the middle, so I am unable to see if the PayPal button has been added. Thanks!
     

Share This Page