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

Synchronize IMAP accounts outside the main mailbox Apps
As far as I can tell, there's no way to get Apple Mail to automatically check for new mail in IMAP mailboxes besides the main mailbox. You can do this manually by choosing 'Synchronize Account' under the Mailbox menu, but who wants to do things manually?

I was hoping the new version of Mail with 10.2 would fix this, but it didn't, so here's my solution.

Save this little shell script below as an executable after entering the name(s) of your IMAP account(s) into the code:
#!/bin/bash

# replace "account1", etc., with the names
# of your imap accounts
accounts=(account1 account2 account3)

for acct in ${accounts[@]}; do
/usr/bin/osascript <<EOF
tell application "Mail"
synchronize with imap account "$acct"
end tell
EOF
done
If you're not comfortable with the Terminal, you can do it like this. In the terminal, type
cat > ~/synchronize_imap.sh
Paste in the script text from above, then press control-D. After that, type
chmod 744 ~/synchronize_imap.sh
Then you need to tell cron to execute this script on a regular basis. I chose to check every 5 minutes. Here is how to edit your crontab manually using emacs. In the terminal type:
set EDITOR emacs
crontab -e
In the window which opens, paste in this text, changing [user] to your user name:
*/5  *  *  *  *   [user]    ~/synchronize_imap.sh
Press control-X, control-C to save and exit the editor. Alternatively, I believe there are some GUI apps to configure cron (such as Cronnix).
    •    
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)
 
[9,350 views]  

Synchronize IMAP accounts outside the main mailbox | 6 comments | Create New Account
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.
Apple Mail Bug
Authored by: genecutl on Sep 08, '02 12:08:52PM

I'm the author of this hint. I just found a bug in Apple Mail. If you use the apple script command to synchronize an IMAP mailbox while a different mail account is selected in the application, then the synchronize option will become disabled for that IMAP account in the future until you restart Mail.

I'd like to see if someone else can confirm this bug.

Given this, you probably only want to run this applescript if you just have one IMAP account, and keep that account selected in Mail.



[ Reply to This | # ]
Only with caching on...
Authored by: anwnn on Sep 10, '02 07:58:52PM

Unfortunately, this only works if you have message caching turned on. The problem I have i with this is the mere fact that I have numerous mailboxes, and messages. Even with a recent sync, this will take nearly 2 minutes to do on my setup, and it's to a local server, nevermind doing it remotely with my iBook.

The old trick used to be taking mail on/offline, but this seems to work rather unreliably now.

are we the only people that get mail in a place other than the inbox?



[ Reply to This | # ]
it works for me
Authored by: Nicolas on Sep 11, '02 03:14:01PM

I've mail redirected in different mailboxes on the imap server. The count of the main mailbox is not affected, but the one of the target mailbox is. I had to create a fake account to be able to see my whole imap structure, which is IMO a bug™.

Nicolas



[ Reply to This | # ]
Synchronize IMAP accounts outside the main mailbox
Authored by: luhmann on Mar 16, '03 02:12:36PM

This applescript (based on Apple's Check My IMAP Quota script) seems to do the job. It should be possible to link this applescript to cron.

tell application "Mail" to set theVersion to get version
-- Much of this script relies on new 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"
else
tell application "Mail" to set everyIMAPAccount to every imap account
-- While quota is a concern for POP users, your POP inbox may not
-- accurately reflect what is stored on the server.
if ((count of everyIMAPAccount) is equal to 0) then
display dialog "This script is only for use with IMAP accounts (which includes .Mac accounts)"
else
set accountNameList to {}
set theResult to accountNameList
repeat with eachAccount in everyIMAPAccount
set theAccountToSynchronize to eachAccount
tell application "Mail" to synchronize with theAccountToSynchronize
-- We poll Mail every 5 seconds to see if all background activity
-- has died down. That way we know when synchronization is finished.
my waitForNoBackgroundActivity()
end repeat
end if
end if

-- 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



[ Reply to This | # ]
Synchronize IMAP accounts outside the main mailbox
Authored by: krishna on May 22, '03 11:28:35PM

Your code was just what I needed to get the background activity count thing right (being an old Unix wonk and an applescript newbie). I created a tcsh alias to open mail, synch it, wait for it to complete, then close the mail app.

I expect that this (plus 'unison', which I'm currently using to synch my home directory) will make working remotely a lot simpler. Much appreciated



[ Reply to This | # ]
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 | # ]