Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Getting help with Apple Scripts and Smart Mailboxes in the Mail.app

Hello I need to prioritize different customer accounts daily. Each account based off sender's e-mail address is forwarded to the correct mail box ex. Example.com(IMAP) > Customers > A > Alpha Corp and Example.com(IMAP) > Customers > B > Bravo Corp. Everyday my manager sends me a list of customer's to prioritize and I edit my Smart Mailbox to have the new mailboxes per "Message is in Mailbox" "Name of Mailbox" and I have to do that 20+ times in the morning. Because of the different levels of mailboxes and the number of clients I have to sort this gets kind of crazy. Is there a way to use Apple Scripts or Automator to do this for me once I get my list?


two resources I've found were


http://stackoverflow.com/questions/2143420/using-applescript-to-create-a-smart-m ailbox

http://hints.macworld.com/article.php?story=2007013108412588


But neither of them are really what I'm looking for and there isn't an easy way to change them to add the correct companies and folders in there. May a search based off a text document or something? Please help I'm losing my sanity here.

Posted on Apr 13, 2016 3:10 PM

Reply
4 replies

Apr 17, 2016 3:30 PM in response to Mateo the Tech

So far I've come up with:


set CustomerNames to {"Example, A"}

set CustomerNumber to 1

display dialog "What are the Customer's Names?"

set CustomerNames to text returned of result

tell application "Mail" to activate


tell application "System Events"

click menu item "Edit Smart Mailbox…" in menu “Mailbox” in menu bar 1 of process "Mail"

delay 1

repeat CustomerName to count of CustomerNames

click menu item “add criterion”

make selection from the popupbutton "Message is in Mailbox"

make selection from the popupbutton item 1 of CustomerNames

end repeat

keystroke return

end tell

but it stops at "Mailbox" telling me there is a Syntax Error. I know this only works with one name but I figured I would get it to go through one at least before I had it parse through an entire list. Any ideas? I'm noticing that it doesn't tell it which Smart Mailbox to edit as well.

Apr 17, 2016 11:16 PM in response to Mateo the Tech

but it stops at "Mailbox" telling me there is a Syntax Error


I assume you mean on the line:


click menu item "Edit Smart Mailbox…" in menu “Mailbox” in menu bar 1 of process "Mail"

If so, that's because you have smart quotes around "Mailbox", which chokes Script Editor. Remove them and replace them with plain quotes and you'll be OK


Note: you'll also have to do the same with the the '... menu item "add criterion", but that won't matter until you fix your repeat statement


repeat CustomerName to count of CustomerNames

which makes no sense. Maybe you mean:


repeat with CustomerName in CustomerNames


where CustomerName iterates through each item in the list, or maybe


repeat with CustomerName from 1 to count of CustomerNames


where CustomerName indicates the index to the iteration (although I wouldn't use this variable name).


You'll also need to fix your 'make selection' statements since these don't seem to be valid AppleScript:


make selection from the popupbutton "Message is in Mailbox"

make selection from the popupbutton item 1 of CustomerNames

Apr 19, 2016 5:58 AM in response to Camelot

Thanks Camelot. It won't let me do the "add citerion" because it doesn't see that menu, I assume because I didn't select the mailbox. So now I have:


set CustomerNames to {"Example, A"}

set CustomerNumber to 1

display dialog "What are the Customer's Names?" default answer ""

set CustomerNames to text returned of result

tell application "Mail" to activate

set selected mailboxes to "Worklist"

end tell



tell application "System Events"

click menu item "Edit Smart Mailbox…" in menu "Mailbox" in menu bar 1 of process "Mail"

delay 1

repeat with CustomerNames from 1 to count of CustomerNames

click menu item "add criterion"

set the popupbutton to menu 13

set the popupbutton item CustomerNumber of CustomerNames

set CustomerNumber to CustomerNumber add 1

end repeat

keystroke return

end tell


This result tells me that on set selected mailboxes line that "A identifier can’t go after this identifier" but you need to have a mailbox to set as selected right?

Apr 21, 2016 3:52 PM in response to Mateo the Tech

So I've given up on editing the smart mailbox but I'm more than willing to delete the old one and create a new one. I'm having an issue selecting menus from the Mail drop downs. Here is what I have so far. Could someone help me please?


set CustomerNames to {"Example, A"}

set CustomerNumber to 1

display dialog "What are the Customer's Names?" default answer ""

set CustomerNames to text returned of result

tell application "Mail" to activate

tell application "System Events"

tell process "Mail"

tell menu bar 1

tell menu bar item "Mailbox"

tell menu "Mailbox"

click menu item "New Smart Mailbox…"

end tell

end tell

end tell

end tell

keystroke "w" using shift down

keystroke "orklist2"

keystroke tab

keystroke "a"

delay 2

keystroke tab

key code 31 using control down -- Down Arrow

delay 2

tell process "Mail"

click menu item "Message is in mailbox"

keystroke tab

set the popupbutton to "CustomerNames"

click menu item "add criterion"

end tell



end tell


tell application "System Events"

repeat with CustomerNames from 1 to count of CustomerNames

click menu item "add criterion"

set the popupbutton to menu 13

set CustomerNames to CustomerNames

end repeat

keystroke return

end tell

Getting help with Apple Scripts and Smart Mailboxes in the Mail.app

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.