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

    juju01 macrumors newbie

    Mar 27, 2008
    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!

  2. kryten2 macrumors 6502a

    Mar 17, 2012
    One way of doing it would be something like this :

    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
    			make new to recipient at end of to recipients with properties {address:""}
    		end tell
    	end repeat
    end tell
  3. juju01 thread starter macrumors newbie

    Mar 27, 2008

    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. kryten2 macrumors 6502a

    Mar 17, 2012
    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