Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

10.4: Refresh Mail's Smart Mailboxes using AppleScript Apps
Many people (including me) use a Smart Mailbox in Mail to read their new email in one place without having to hunt through all the folders the mail may already have been filed to automatically. The Smart Mailbox can have a very simple definition: Match All where condition is Message is unread.

The problem with this is that, after some time, this smart mailbox gathers lots of already-read mails, and the only way to get rid of them is to switch to another mailbox and back again. Well, there is another way: Bring up the "Edit Smart Mailbox..." dialog (from the Mailbox menu) and immediately hit "OK" without changing anything. This makes reload the mailbox, and since you don't have to switch to another mailbox, it's faster, too.

It would be even nicer to have a shortcut for this, you think?

Start Script Editor and type in the following AppleScript:
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
  keystroke return
end tell
Note that you may have to increase the delay 1 in the script to delay 2, since depending on how long Mail needs to draw the dialog (having many criteria takes a longer time), the Return keystroke may come too quickly and be ignored. Save the code somewhere as an application bundle and use Quicksilver, Butler, or another suitable tool to assign a shortcut to it. Additionally, you may edit the info.plist file within the script's application bundle and add this...
        <true/> the first <dict> element. This will make the script run in the background without even showing up with an icon in the dock.

[robg adds: Here's another way to solve this one. Quit Mail and then open the Keyboard Shortcuts tab of the Keyboard & Mouse System Preferences panel. Click the plus sign at the bottom to add a new shortcut. Set the Application to Mail, and the Menu Title to Edit Smart Mailbox... (include the three dots), then define a keyboard shortcut (I used Shift-Control-E). Launch Mail, and you can update a Smart Mailbox using the shortcut followed by the Enter key (to clear the dialog). You could also use Butler or a similar tool to make a macro to turn it into a true one-keystroke solution.]
  • Currently 2.20 / 5
  You rated: 1 / 5 (5 votes cast)

10.4: Refresh Mail's Smart Mailboxes using AppleScript | 7 comments | Create New Account
Click here to return to the '10.4: Refresh Mail's Smart Mailboxes using AppleScript' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.4: Refresh Mail's Smart Mailboxes using AppleScript
Authored by: windrag on Feb 02, '07 08:31:04AM

What's wrong with the menu command Mailbox->Rebuild? I've assigned a keyboard shortcut for this in System Preferences (^⌘R). It's worked for me since Tiger first came out. Faster than running a script.

windrag | 700MHz iBook 16 VRAM G3 | Seagate 80GB HD | 640MB RAM | OS X 10.4.8

[ Reply to This | # ]
10.4: Refresh Mail's Smart Mailboxes using AppleScript
Authored by: operator207 on Feb 02, '07 11:06:27AM

Anyone wanting to do the post I am replying to, can do what Rob says, except put 'Rebuild' in instead of 'Edit Smart Mailbox...'

Worked for me, thanks for the tip!

[ Reply to This | # ]
10.4: Refresh Mail's Smart Mailboxes using AppleScript
Authored by: joh on Feb 04, '07 12:22:30PM

"Rebuild" is not the same. On IMAP-accounts it deletes the cached emails in the mailbox and reloads them from the server. At least Mail's help says that and on trying I found it to be true. Note that this will also remove all marks (like read/unread or flagged).

I may work on POP-accounts.

[ Reply to This | # ]
10.4: Refresh Mail's Smart Mailboxes using AppleScript
Authored by: phowell on Feb 04, '07 07:08:29PM

Right, but we're talking about Smart Mailboxes, not IMAP mailboxes. The whole problem that's trying to be solved is exactly a "rebuilding" of the smart mailbox.

Rebuild is the right way to do this one.

[ Reply to This | # ]
10.4: Refresh Mail's Smart Mailboxes using AppleScript
Authored by: joh on Feb 05, '07 12:43:56AM

I wouldn't have written an ugly script to handle that if "Rebuild" would do the same. On an IMAP account it did reload the messages from the server more than once (although not always). OK, it may also have been a bug in

If it works for you, fine. Much easier to bind it to a shortcut, too.

[ Reply to This | # ]
10.4: Refresh Mail's Smart Mailboxes using AppleScript
Authored by: sr105 on Mar 04, '10 10:16:32AM

My setup: Mail 4.2 on 10.6.2
Using "Rebuild" on a Smart Mailbox does seem to work, but it also seems to try and connect to my not-in-use Outgoing servers as well for some reason. I think Rebuild might be buggy.

Also, having a shortcut for Rebuild seems dangerous if you have a very large >1GB IMAP mailbox because if you get in the habit of hitting that shortcut, you might do it while inside an IMAP mailbox. Then, I hope for your sake that you're connected to the IMAP server on a very high speed connection and have 30 min or more to spare.

It would be oh-so-nice if the menu item changed to "Rebuild Smart Mailbox" so we could avoid the error.

[ Reply to This | # ]
10.4: Refresh Mail's Smart Mailboxes using AppleScript
Authored by: grinnelljd on Feb 02, '07 08:08:49PM

I take a slightly different approach to this. I let all my incoming email go to my inbox, but I use a number of smart folders to display emails from various parties or on various subjects. The items in the smart folders can be thought of as pointers back to the original emails in the inbox.

I also use a second parameter to restrict the matches to those emails that have been received in the last 30 days. This keeps the lists in the smart folders from growing too long.

The advantage of managing my email this way is I now only need to archive two folders (Inbox and Sent). The smart folders help me to keep tabs on who is sending email to me. It also allows me to be very flexible in the assignment of smart folders as I can trash a smart folder that I no longer need since the actual email remains safe and sound in the inbox.

If I need to locate an email that was received more than 30 days ago (and has since rolled off the smart folder match) I can find it by searching the inbox for matching metadata.

[ Reply to This | # ]