0

I looked and looked but couldn't find an answer to this seemingly simple question (I'm also new to AppleScript). Basically, I just want a script that sets up a Rule and moves messages to mailbox "Foo" (a Mailbox in my IMAP account). Here's what I have:

tell application "Mail"
set newRule to make new rule at end of rules with properties {name:"Foo Internal",   enabled:true}
tell newRule
    make new rule condition at end of rule conditions with properties {rule type:any recipient, expression:"internal_foo@foo.com", qualifier:does contain value}
    set move message to mailbox "Foo"
end tell
end tell

I've tried various combinations of this line...

set move message to mailbox "Foo"

...including specifying the IMAP account, setting it to a variable, and so on. I'm not a programmer but I really want to script these rules cause I setup rules all the time at my job. Any help?

Kara
  • 6,115
  • 16
  • 50
  • 57
jonsie_araki
  • 221
  • 3
  • 8

1 Answers1

0

Your script fails because of two things:

  1. it omits to set the should move message property of the rule to true, which is needed for a move message action to actually “stick”;
  2. it does not define the object hierarchy of the target mailbox correctly: you will need both the account and the application context, as you are inside a tell block targeting the rule, not Mail.

The following code:

tell application "Mail"
    set newRule to make new rule at end of rules with properties {name:"Foo Internal", enabled:true, should move message:true}
    tell newRule
        make new rule condition at end of rule conditions with properties {rule type:any recipient, expression:"internal_foo@foo.com", qualifier:does contain value}
        set move message to (mailbox "Foo" of account "Bar" of application "Mail")
    end tell
end tell

will work in Mail 5.2 (stock as of OS X 10.7.4).

kopischke
  • 3,393
  • 1
  • 21
  • 40
  • Thanks for the feedback. When I use this code in my script, I don't see any errors when it runs, but the rule still says "No mailbox selected." Any idea? – jonsie_araki Jul 19 '12 at 03:40
  • I assume you have made sure both mailbox and account exist and are spelled correctly? Also, make sure to delete any existing "Foo internal" rule before running the script. Finally – and I should have asked that before –: what version of Mail and OS X are you running (the above will run just fine on Mail 5.2 / OS X 10.7.4)? – kopischke Jul 19 '12 at 14:35
  • Thanks. Yes, I've triple checked that the mailbox exists under the proper account and that the spelling is correct. I also always remove the "Foo Internal" rule prior to re-running the script. I'm running Mail 5.2 on 10.7.4. Any other ideas? I'm *this* close to finishing this script that creates mailboxes based on client names and sets up rules, just need to get this last bit working. I'm testing just this rule creation part separately by the way, so there's no chance that anything else is conflicting with it. I really appreciate your help. – jonsie_araki Jul 19 '12 at 15:57
  • My bad – missed one clause (see amended answer). I didn’t notice that because new rules default to the action last set, so if you set the the move action manually once, as I did, the next script created rules will display with that same action … though it has not actually been set by the script. Fixed. – kopischke Jul 19 '12 at 17:06
  • Awesome - you rule. Thank you! – jonsie_araki Jul 19 '12 at 17:44