Need Applescript to create task from mail message

Discussion in 'Mac Programming' started by schulzmc, Jan 8, 2008.

  1. schulzmc macrumors regular

    Joined:
    Aug 13, 2006
    #1
    I thought this would be easy but either I am a total idiot or it's harder than it looks. (I'm guessing the first.)

    I need a script that I can execute from within Mail that will take the email that was just identified in the Mail rule and create a new task based on the subject name of the email. In other words, if the subject begins with "tt" the rule will run this script and the result will be a new task with whatever text followed "tt" in the subject as the title for the task.

    Can anyone help? Thanks!
     
  2. Macgenie macrumors newbie

    Joined:
    Jan 8, 2008
    Location:
    Suffolk, UK
    #2
    Hang on in there !

    Hi schulzmc - your problem IS harder than it looks but I think I am nearly there, will send a reply soon (unless you have already cracked it yourself ...)
    :)
     
  3. schulzmc thread starter macrumors regular

    Joined:
    Aug 13, 2006
    #3
    I haven't - so any help would be GREATLY appreciated!
     
  4. Macgenie macrumors newbie

    Joined:
    Jan 8, 2008
    Location:
    Suffolk, UK
    #4
    Hi schulzmc - I had posted a similar reply to this a little while ago but I think I missed a message from MacForum saying that I was apparently not logged on so my first shot may not have been posted - if you see two replies what the heck - they both say similar things ...

    I tried several ways to make this work but each attempt crashed Mail - the problem arises when the script tries to generate a new message. If you use the code sample shown below (all this does is to strip out the ""tt" from the subject of the email and put the remaining string into a dialog box for you to see) you can see that the email is held back until you click the OK button. (I tested this by sending an email to myself with "tt" in the subject).

    If you remove the dialog display and try to create a new message it crashes Mail. Mail still crashes even if you put the new message creation code at the end of the script, - I think this is because the script suspends Mail - but then the script tries to make a new message - it can't suspend Mial and try to make it create a new message at the same time!

    My script:

    using terms from application "Mail"
    on perform mail action with messages theMessages for rule theRule
    tell application "Mail"
    repeat with eachMessage in theMessages
    set myString to subject of eachMessage
    set i to count of myString
    set subjectString to text from word 2 to i of myString
    display dialog subjectString
    end repeat
    end tell
    end perform mail action with messages
    end using terms from

    You might have to get round this by splitting the job in two i.e. trigger a script from the rule that catches each message with "tt" in the subject and puts them into a mailbox. You could then open one of the messages and then run a separate script (using the script folder utility on the top menu bar) to take the subject, strip out the "tt" and make a new message and insert the remaining string into the new subject.

    Other things you might try is to use the "reply" or "forward" methods to create new messages in the script above but I suspect these will cause Mail to crash as well. If you deviate from the "using terms from ...." & "on perform mail action etc ... " format the script never gets triggered so scope for invention is a bit limited.

    Applescript can be downright annoying at times - you would never guess the syntax used above from the dictionary for Mail, I gleaned some of this from the Apple website and the O'Rielly book "Applescript - The definitive Guide" - I thought I had this nailed but then Mail got all grumpy and that was that.

    If you manage to get this working I would be interested to see the script / method. :)
     

Share This Page