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


Click here to return to the 'Synchronize IMAP accounts outside the main mailbox' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Synchronize IMAP accounts outside the main mailbox
Authored by: aixccapt99 on Jun 14, '04 10:10:23PM

Here's a cleaned-up version of the AppleScript, targeted for cron use. I added a check to see if Mail is running, so it won't be opened if it's not already. I also fixed the delay code -- it was not actually waiting 5 seconds.

Save this as Synchronize all IMAP.scpt (paste it into Script Editor) in the Mail Scripts folder (~/Library/Scripts/Mail Scripts/). That way you can initiate it manually if you want, and we'll point cron to it below. You can add a key command, for example control-n (like Get New Mail), by naming the file ...IMAP___ctrl-n.scpt. See other hints for the details of this key-equivalent ability.

on run
	tell application "System Events" to set mailRunning to ((name of processes) contains "Mail")
	
	-- only do anything when Mail is already running
	if (mailRunning) then
		
		tell application "Mail" to set theVersion to get version
		-- Much of this script relies on new AppleScript terminology that
		-- was not available in earlier versions of Mail
		if (theVersion < 1.2) then
			display dialog "This script will only run ¬
				with Mail version 1.2 or greater."
		else
			tell application "Mail" to set everyIMAPAccount to every imap account
			if ((count of everyIMAPAccount) is equal to 0) then
				display dialog "This script is only helpful for IMAP ¬
				accounts (including .Mac accounts), and it appears ¬
				you don't have any set up."
			else
				
				repeat with theAccountToSynchronize in everyIMAPAccount
					tell application "Mail" to synchronize with theAccountToSynchronize
					-- We poll Mail every 4 seconds to see if all background activity
					-- has died down. That way we know when synchronization is done 
					-- and the next account can be synchronized. 
					my waitForNoBackgroundActivity()
				end repeat
				
			end if -- at least one IMAP mailbox
		end if -- version ok
		
	end if -- mail is running
end run


-- 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
			end if
			
			delay 4
			
		end tell
	end repeat
end waitForNoBackgroundActivity

To make it run regularly, edit your crontab to add this line:

*/30    *       *       *       *       /usr/bin/osascript "/Users/username/Library/Scripts/Mail 
Scripts/Synchronize All IMAP.scpt"

That's all one line in your crontab -- Mail Scripts is a single folder with a space in the name. The quotes are important to preserving the spaces. You can change the number 30 to a different value -- it represents the interval at which this script will run. Every 30 minutes is enough for me -- Mail only checks every 15 for new mail, and I figure I can do a full synchronize at twice that interval.



[ Reply to This | # ]