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

Regularly synchronize IMAP folders in Mail Apps
I met someone in a Macintosh chat room who said he needed a way to make Mail synchronize all the folders in his IMAP account. He had 30+ mail folders, and Mail was only updating the Inbox when asked to 'Check Mail.' He had checked the preference "Automatically synchronize changed mailboxes," but this was only happening when Mail was launched. I suggested making a tiny AppleScript to cause Mail to synchronize, as follows:
  1. Run "Script Editor" (it's usually in Applications -> AppleScript)
  2. Punch in this code:
  3. tell application "Mail"
      synchronize with account "Account Name"
    end tell
    Replace Account Name with the account name in Mail.app.
  4. Save as type "Application"
Now when you double-click your application in the Finder, Mail will synchronize your chosen account. You can, of course, run your application from the Terminal with open SyncMail.app, where SyncMail.app is the path/name you used when creating the application. You could also run it at regular intervals using cron or iCal. This is perhaps very simple and/or obvious -- but it was my first time using AppleScript, so I was pleased with it!

[robg adds: I haven't tested this, but you should also be able to save it as a simple script and put it in your user's Library -> Scripts -> Mail Scripts folder (create it if it doesn't exist), and then run it from the scripts menu icon within Mail.]
    •    
  • Currently 1.50 / 5
  You rated: 3 / 5 (4 votes cast)
 
[18,119 views]  

Regularly synchronize IMAP folders in Mail | 9 comments | Create New Account
Click here to return to the 'Regularly synchronize IMAP folders in Mail' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Regularly synchronize IMAP folders in Mail
Authored by: Black on Jan 20, '05 10:57:43AM

I could see making this a cron job (though you might want to actually check if Mail.app is actually running in the script if you did that). However, I can't see any point in making this a stand alone application or an entry in the script menu as it it already an entry in the 'Mailbox' menu...



[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: thype on Jan 20, '05 11:36:08AM

This is built into mail, go to Mailbox -> Synchronize "mailbox name".

Does the exact same thing.



[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: jaysoffian on Jan 20, '05 12:31:58PM

This is a repeat tip:

http://www.macosxhints.com/article.php?story=20040108123952524



[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: allanmarcus on Jan 20, '05 02:30:28PM

Silly me, I just wrote this same thing last week! The reason is that our IMAP server has rules, and filters mail on the server. This turns out to be great since all my mail is filtered, even if I'm not at my mac (we have web mail too).

So, here's my take on the app. I have a cron job that runs this script every 5 minutes. This script checks to see if Mail is running.

tell application "System Events"
	if exists process "Mail" then
		try
			tell application "Mail"
				set everyIMAPAccount to every imap account
				repeat with eachAccount in everyIMAPAccount
					set theAccountToSynchronize to eachAccount
					synchronize with theAccountToSynchronize
				end repeat
			end tell
		end try
	end if
end tell

Cron job:
0,10,20,30,40,50	*	*	*	*	/usr/bin/osascript /Users/allan/Documents/AppleScipts/SyncMail.scpt


[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: jonathanlacour on Jan 20, '05 05:03:33PM
The methods listed in the above comments are inefficient. I found a script somewhere that was much more efficient and edited it a bit. Here it is. Basically, you just need to cycle the status of an IMAP account to get it to update message counts. If you synchronize its going to take a long time and traverse your entire mail hierarchy. Create an applescript that runs when you log into your computer, and this should make things a lot snappier when dealing with your server-side filtering with IMAP:

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()
	tell application "Mail"
		repeat
			if (background activity count is equal to 0) then exit repeat
			delay 5
		end repeat
	end tell
end waitForNoBackgroundActivity

on run
	tell application "System Events"
		if exists (application processes whose name is "Mail") then
			my waitForNoBackgroundActivity()
			my checkMail()
		end if
	end tell
end run

on idle
	delay 120
	tell application "System Events"
		if not (exists (application processes whose name is "Finder")) then quit
		if exists (application processes whose name is "Mail") then
			my waitForNoBackgroundActivity()
			my checkMail()
		end if
	end tell
	return 0
end idle


[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: sydlow on May 01, '05 03:14:36AM

What IMAP mail server are you running? This approach of cycling doesn't work for me when connecting to Communigate server on Mail.app v2 on Tiger.



[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: marook on Jan 20, '05 05:34:15PM

One simple way to run this once in a while, would be to make a rule in Mail that triggers on 'Any Message' and then runs the script.

Then you would have mail to sync your folders every time you get mail - and only when mail is running.. ;-)

---
/Marook



[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: mnb on Feb 01, '05 04:41:27PM

The problem with that approach is two fold:

If you get no mail during a check, no synch will occur.

If you get 53 messages during a check, 53 synchs will occur.


There needs to be a way to perform a task on every mail check in order for this to work. Ideally, Apple would add a preference to synch with IMAP on every check and that would solve the problem.



[ Reply to This | # ]
Regularly synchronize IMAP folders in Mail
Authored by: StarvArt on May 05, '05 02:06:09PM

I'm pretty new to using IMAP, so this may be a simple case of ignorance, but when I tell Mail.app to Synchronize my one IMAP mail account, it downloads the entire contents of my account (all the hidden ".[filename]" files and everything!). Not too big of a deal, since it isn't my main account, but it is terribly annoying. All I wanted it to do was synchronize the mail itself!

Interestingly, the ~/Library/Mail folder for the account contains every file (and files in sub-folders) from that account with ".imapmbox" appended to the file name. For instance, I have files named, ".cshrc.imapbox," ".alias.imapmbox," "public_html/index.html.imapmbox," etc. I'm guessing this is NOT the way this is supposed to work!?!? Mail.app shows all the files in the Mailboxes drawer.

I was concerned that if I remove the files from ~/Library/Mail/IMAP... that the next synch would remove the file from my account, which, of course, it did. Not good. Fortunately, I put up a dummy file as a test, but this surely cannot be the correct function of the Synchronize task. As I said, I'm new to IMAP, so maybe this is how it is supposed to act, except I've not found any info anywhere that mentions this type of activity.

Apologies if this doesn't exactly fit in this thread, but it had the words "IMAP" and "synchronize" in the subject, so I figured it'd be worth a shot. Any assistance will be greatly appreciated!



[ Reply to This | # ]