An AppleScript to periodically check all IMAP folders

Jan 21, '04 09:56:00AM

Contributed by: jaysoffian

I use the Apple Mail.app with my own IMAP server. At the IMAP server end, I sort incoming messages into folders. Unfortunately, when Mail.app checks for mail, it doesn't update the new message counts in each of these folders; rather, it only updates the INBOX message count.

I wrote the following AppleScript that I fire up whenever I start mail. It runs in the background, and once every five minutes it toggles each online IMAP account offline/online. This causes Mail.app to update the new message counts for each folder. Works great for me, your mileage may vary.

Here's the script:


on checkMail()
  tell application "Mail"
    set everyIMAPAccount to every imap account
    repeat with eachIMAPAccount in everyIMAPAccount
      tell eachIMAPAccount
        -- cycle online status of each enabled and online account,
        -- causing mail to update the status for each folder
        if (enabled and include when getting new mail) then
          set include when getting new mail to false
          set include when getting new mail to true
        end if
      end tell
    end repeat
  end tell
end checkMail

-- Loops until the background activity of Mail has stopped
on waitForNoBackgroundActivity()
  repeat
    tell application "Mail"
      if (background activity count is equal to 0) then
        exit repeat
        delay 5
      end if
    end tell
  end repeat
end waitForNoBackgroundActivity

on run
  checkMail()
end run

on idle
 waitForNoBackgroundActivity()
 checkMail()
 return 300
end idle
To use, fire up Script Editor, cut and paste in the code, then save it with File Format: Application; Options: [ ] Run Only, [ ] Startup Screen, [X] Stay Open. Then just fire it up whenever you start Mail.app (it will start Mail.app for you if you don't).

Comments (17)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040108123952524