applescript to send new email

Discussion in 'Mac Programming' started by juju01, Jan 20, 2013.

  1. juju01, Jan 20, 2013
    Last edited: Jan 20, 2013

    macrumors newbie

    Joined:
    Mar 27, 2008
    #1
    I am looking to generate a new email in response to email from twitter about a new follower. The twitter notification typically has the following format for the subject:

    "Name (@twitter_username) is now following you on Twitter!"

    I am looking for a mail script which does the following:

    1. extract the twitter username from the subject - the @username
    2. send an email with the username in the body. I am using a specific text for the subject of the new email.

    I have a script which does all the above except extracting the username from the subject line. Here is the script on Pastebin

    Will appreciate any help to tweak it to add the username extracting feature!

    Thanks
     
  2. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #2
    One way of doing it would be something like this :

    Code:
    tell application "Mail"
    	set theMessages to every message of mailbox "INBOX" of account "GMX" whose subject contains "Twitter"
    	repeat with eachMessage in theMessages
    		set theSubject to subject of eachMessage
    		set thefirstoffset to offset of "(" in theSubject
    		set thesecondoffset to offset of ")" in theSubject
    		set twitter_username to characters (thefirstoffset + 1) thru (thesecondoffset - 1) of theSubject as string
    		set theNewMessage to make new outgoing message with properties {subject:"Notify", content:"Welcome new twitter follower" & space & twitter_username, visible:true}
    		tell theNewMessage
    			activate
    			make new to recipient at end of to recipients with properties {address:"email@someone.com"}
    			--send
    		end tell
    	end repeat
    end tell
     
  3. thread starter macrumors newbie

    Joined:
    Mar 27, 2008
    #3

    Thank you! This looks great. The first line is a throwing me off a bit. I am using a mail rule to trigger the script to run - only when a new message arrives. So with this code, do I use this in a mail rule ? Does the code above fire only when new messages arrive for the specified mail account? - which is what I want to do. Thanks again.
     
  4. macrumors 6502a

    Joined:
    Mar 17, 2012
    Location:
    Belgium
    #4
    Unfortunately I hardly use Mail nor do I use Twitter so I had to send myself a few mails with the subject line you mentioned to test on. The first line was used to get me a list of message ids from a specific mailbox and account to work with and isn't needed when using a rule.
     

Share This Page