Panther’s Mail: Rebuilding mailboxes

Posted by Pierre Igot in: Macintosh
April 7th, 2004 • 12:19 am

I regularly use the “Rebuild” command in Mail’s “Mailbox” menu in order to rebuild the indexes of my mailboxes. This is because I have noticed that, sometimes, when Mail crashes (and Mail does still crash, unfortunately), after I relaunch it some of the messages that I thought I had moved to the trash are still in my In mailbox. I suspect this has to do with the fact that Mail doesn’t automatically rebuild the indexes of its mailboxes every time a message is moved around. I don’t know exactly when it does rebuild indexes, but I frequently choose to do it manually in order to avoid the problem with deleted messages resurfacing.

I have also used the hint described here to assign the cmd-ctrl-R shortcut to the “Rebuild” command, so that it’s fairly easy to use. Just select the mailbox whose index needs to be rebuilt and hit the shortcut and it’s done.

The trouble is that Mail is not smart enough to understand that the user might want to rebuild more than one mailbox at a time. If you select more than one mailbox, the “Rebuild” command becomes disabled and cannot be used. This even applies when you select the “In” mailbox, which is, in effect, a folder containing the “In” mailboxes for each account you might have, even though it also behaves like a mailbox itself and displays the combined contents of the mailboxes it contains. If you select the main “In” mailbox, the “Rebuild” command is disabled as well. In order to rebuild all your “In” mailboxes (and I have 20 e-mail accounts to manage, so that’s 20 “In” mailboxes), you need to select them one by one and rebuild them individually!

Not exactly convenient, I am afraid.


11 Responses to “Panther’s Mail: Rebuilding mailboxes”

  1. ssp says:

    I suspect you could use UI scripting to automate this task.

    That’s still evil but may be quicker.

  2. Pierre Igot says:

    Evil indeed :). I would consider UI scripting a last resort here — I might try it some time. The tricky thing is that this mailbox drawer thingie behaves in a rather strange way: It never really is the “focus” in the UI (the highlighting colour is always the faded grey, never the normal selection colour). And you cannot browse it with the keyboard. Scripting mouse movements and clicks is a bit too icky for my taste, considering that my Mail Viewer window is not always in the exact same position.

    Would be much easier if Mail’s AppleScript dictionary included a “rebuild” command, obviously.

  3. ssp says:

    Pierre, Perhaps UI Scripting is a little better than you think.

    You can actually query the UI elements present on screen in UI scripting and select them without needing to script mouse movements. (Hence the recent hype about potential Apple screen readers, I suppose).

    Of course a ‘rebuild’ script command would be better.

    … no ‘rebuild’ command would be the way it should be though – it’s not our job to keep Mail’s internal files in shape. ;)

    P.S. Try Apple’s UI Element Inspector to get an idea how powerful Apple’s assistive technologies are without getting your hands dirty. Then write a post asking why activating it is so complicated and noting that it doesn’t work in all but the standard system UI elements of your favourite Office suite (I haven’t tried that as I am working in a sane computing environment, but I’ll bet a bottle of wine….) or in iTunes.

  4. Gabriel Gilder says:

    Well, here’s a script that’ll rebuild all your mailboxes, in case you’re interested. Yes, it’s slow, yes, Mail is on crack and sometimes thinks you haven’t read any messages in a mailbox… but, it’s better than nothing. =)

    tell application “Mail”
    set myBoxes to every mailbox

    repeat with box in myBoxes
    set selected mailboxes of message viewer 1 to {box}
    activate

    tell application “System Events”
    tell process “Mail”
    tell menu bar 1
    tell menu bar item “Mailbox”
    tell menu “Mailbox”
    click menu item “Rebuild”
    end tell
    end tell
    end tell
    end tell
    end tell
    end repeat

    end tell

  5. Pierre Igot says:

    Thanks for the script. You can use the pmCode tag

    pre

    for computer code if you want to preserve the formatting.

    Unfortunately, this script doesn’t work on my machine. When I try to run it, it starts by selecting the first mailbox in my “On My Mac” mailbox folder, and then stops with the following error message:

    System Events got an error: NSReceiverEvaluationScriptError: 4

    I need a script that can rebuild all my IN mailboxes, which are the most likely to need rebuilding. Of course, a script that would rebuild whichever mailboxes are currently selected would be ideal.

  6. Gabriel Gilder says:

    Hi, I believe the error you’re getting is actually because you don’t have GUI scripting enabled. Go to System Preferences, Universal Access and make sure “Enable access for assistive devices” is checked. Then it should work fine.

    But, while I’m at it, here’s another script that will rebuild whatever mailboxes you have selected.

    tell application "Mail"
    	set myBoxes to selected mailboxes of message viewer 1
    	repeat with box in myBoxes
    		set selected mailboxes of message viewer 1 to {box}
    		activate
    		
    		tell application "System Events"
    			tell process "Mail"
    				tell menu bar 1
    					tell menu bar item "Mailbox"
    						tell menu "Mailbox"
    							click menu item "Rebuild"
    						end tell
    					end tell
    				end tell
    			end tell
    		end tell
    	end repeat
    end tell
    

    Hope that helps!

    -Gabriel

  7. Pierre Igot says:

    Thanks again… However, I am afraid I have to report that this isn’t working. The “Enable access for assistive devices” option has been checked on my machine for a very long time, so it’s not the culprit.

    With this new script you provided, if I select all my “In” mailboxes and invoke the script, Mail selects the first “In” mailbox, hangs for about 5 seconds, and then gives me the same error message as above.

  8. Gabriel Gilder says:

    Hi, I figured out what’s going on. The thing is, these scripts won’t run from Mail’s script menu, which I believe is how you’re trying to use them. Instead, they have to be run from the Script Editor (or made into standalone apps).

    As far as I can tell, this is because my script needs access to the menubar, but if you run it from the Script menu, the menubar is already “busy” and can’t respond to another request. That’s just a guess; if someone else can figure this out I’d be interested to know.

    Anyway, for now you’ll have to make do with the minor inconvenience of having to run them from the Script Editor. At least until Mail puts a “rebuild” command in its scripting dictionary, that is!

    -Gabriel

  9. Pierre Igot says:

    Thanks Gabriel! It is indeed working properly when running from the Script Editor.

  10. ssp says:

    Pierre: Probably you can still run the script from the system-wide script menu rather than Mail’s built-in one.

    Just a thought.

  11. Pierre Igot says:

    Yup, it works from the system-wide script menu too… Thanks for the tip!

Leave a Reply

Comments are closed.