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

An AppleScript to periodically check all IMAP folders Apps
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).
    •    
  • Currently 3.00 / 5
  You rated: 3 / 5 (5 votes cast)
 
[19,148 views]  

An AppleScript to periodically check all IMAP folders | 17 comments | Create New Account
Click here to return to the 'An AppleScript to periodically check all IMAP folders' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
An AppleScript to periodically check all IMAP folders
Authored by: jspahr on Jan 21, '04 11:18:49AM

I use the following script:

[code]

tell application "System Events"
set procCount to name of every process whose name is "Mail"
end tell

if procCount is {"Mail"} then

tell application "Mail"
synchronize with account "Spahr Panix"
end tell

end if

[/code]

It is run from the unix crontab like so:

[code]
*/20 * * * * /usr/bin/osascript /path/to/Mail_Sync.scpt
[/code]

so it runs every 20 minutes, only if Mail is running.



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: jaysoffian on Jan 21, '04 12:09:29PM

I have over 10,000 messages across my two IMAP accounts. Synchronizing takes well over 10 minutes per account. I wouldn't want to do that periodically. Setting the account offline/online ONLY updates the message counts.



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: applesjgtl on Aug 30, '08 01:16:18AM
I'm trying to write a script to launch Adium upon launching iChat, so I modified your AppleScript to do so. Now I would like to add it to the UNIX crontab. Can you explain how, and in what program, is this done? Is it done in Terminal? When I entered your code (the second piece) into terminal, it gave me an error.
    What am I doing wrong?
And yes, I did adjust the path and name to fit my script.

[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: applesjgtl on Aug 30, '08 01:20:36AM
oh, also I would like to shorten the periodic checks to 2 minutes apart. My script is as follows. tell application "System Events" set procCount to name of every process whose name is "iChat" end tell if procCount is {"iChat"} then delay 45 tell application "Adium" to run delay 15 tell application "Adium" to quit else end if

[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: MattHaffner on Jan 21, '04 11:34:56AM

In Panther's Mail.app, there is a new checkbox under the Advanced account settings: "Automatically synchronize changed mailboxes". Does this not work continuously? I was under the impression that it did now...



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: Greedo on Jan 21, '04 11:59:16AM

In a word, nope.



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: luhmann on Jan 21, '04 07:18:00PM

If this isn't working right then it is a bug and should be reported to Apple.

Am I correct to say that the folders are being synchronzied, but simply not the message count?



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: jspahr on Jan 21, '04 12:17:33PM

Ah!, that's good to know.

I don't have that many messages. I only have 150MB allocated to mailbox space on the IMAP server, so I tend to keep things lean. I'm also on a powerbook, so I like have a local cache of all my new messages... and synchronizing takes 20-30 seconds for me.



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: markds on Jan 21, '04 12:33:17PM

Beat me too it. Was working on a related script for checking only those folders into which mailing list messages are filtered on the server. Can supply the syntax if anyone wants to reduce this "check all" script, to one targeted at a specific list of imap mailboxes.



[ Reply to This | # ]
An AppleScript to periodically check _specific_ IMAP folders
Authored by: ledgekindred on Jan 21, '04 02:46:10PM

Yes please yes! I use procmail to sort list and work mail into specific folders and would love to be able to update just those "current" folders without updating every folder. I've got nearly 4G of mail in some untold number of nested folders on my server (every email since about 1994) and I really would rather not synchronize all that to the powerbook nor wait for a "synchronize" to finish with the number of folders I have total.

I'm a relative Mac/Applescript neophyte (my first mac is this 15" Albook got shortly after they came out - as a long-time UNIX guy, I fell in love with OSX from using a friend's mac for a while and had to get one) so I wouldn't even know where to start on my own.



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: chrismalanga on Jan 21, '04 03:34:10PM
I am an AppleScript IDIOT so if this is basic, I apologize. Is there any way to have a sound play if there is new mail in any of the folders that are checked? This would be a huge benefit to me. If you have thoughts, please email me. Thanks!

[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: Greedo on Jan 22, '04 05:41:35PM

For those of you using the cron suggestion given in the first comment, you can change the crontab entry to only run during work hours. Thus, if you aren't at work and Mail is running, it won't bother updating. Might be useful for people who leave their Macs always logged in.

To run every 10 minutes, from 8am to 8pm, Monday to Friday, the cron entry is:

*/10 8-20 * * 1-5 /usr/bin/osascript /path/to/script.scpt



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: jaysoffian on Feb 24, '04 05:27:42PM
I've improved the script, made it hidden (so I don't have to see it), and it now starts as a login item. First, here's the new 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()
	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
Cut and paste into Script Editor. Then save it as an Application Bundle. Open up the terminal and navigate to whever you saved the application bundle. Inside the bundle, edit Contents/Info.plist. Add to the top of the plist:

        <key>NSUIElement</key>
        <string>1</string>
So the top of your Info.plist now looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
        <key>NSUIElement</key>
        <string>1</string>
Now edit your login items and add the script (application bundle) as a login item. Logout and log back in. The script should now start on login, it will kill itself automatically on logout. You won't see it running unless you look for it from the terminal using the "ps" command or via the Process Viewer. And whenever Mail.app is running, any online IMAP accounts will now have all their folders updated when new messages are received to those folders.

[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: cblackst on May 27, '04 11:56:33AM

Is there any way with the lastest script to have Mail.app actually display which folders have new messages without having to select the folder?

As I have it now, I have to select a folder to see the new email messages in it.

It's almost easier to just go offline/online with a keyboard shortcut



[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: vrillusions on May 11, '05 11:06:18AM
jaysoffian's script didn't seem to work for me (haven't tried it till I upgraded to tiger). So I just took his idea with the original code that works and just added a check to see if mail is running. here's the code:

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

-- Checks if Mail is running
on IsMailRunning()
	tell application "System Events"
		set procCount to name of every process whose name is "Mail"
	end tell
	if procCount is {"Mail"} then
		return true
	else
		return false
	end if
end IsMailRunning

on run
	if IsMailRunning() then
		checkMail()
	end if
end run

on idle
	if IsMailRunning() then
		waitForNoBackgroundActivity()
		checkMail()
	end if
	return 300
end idle
Save it as an application bundle and do the same thing as above. cd into the check_all_imap.app/Contents/Info.plist and add the following two lines:

        <key>NSUIElement</key>
        <string>1</string>
I've also noticed that if you modify the script and resave it, it moves that key further down, but it does still have it. Then just set it to start at login and it will do it's magic whenever mail is running, but only when mail is running. I've only tested this in 10.4 but don't see why it wouldn't work in 10.3

[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: vrillusions on May 11, '05 11:10:19AM
Heh, replied to a comment and not to the actual tip, reposting here... jaysoffian's script didn't seem to work for me (haven't tried it till I upgraded to tiger). So I just took his idea with the original code that works and just added a check to see if mail is running. here's the code:

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

-- Checks if Mail is running
on IsMailRunning()
	tell application "System Events"
		set procCount to name of every process whose name is "Mail"
	end tell
	if procCount is {"Mail"} then
		return true
	else
		return false
	end if
end IsMailRunning

on run
	if IsMailRunning() then
		checkMail()
	end if
end run

on idle
	if IsMailRunning() then
		waitForNoBackgroundActivity()
		checkMail()
	end if
	return 300
end idle
Save it as an application bundle and do the same thing as above. cd into the check_all_imap.app/Contents/Info.plist and add the following two lines:

        <key>NSUIElement</key>
        <string>1</string>
I've also noticed that if you modify the script and resave it, it moves that key further down, but it does still have it. Then just set it to start at login and it will do it's magic whenever mail is running, but only when mail is running. I've only tested this in 10.4 but don't see why it wouldn't work in 10.3

[ Reply to This | # ]
An AppleScript to periodically check all IMAP folders
Authored by: rishi7624 on Feb 13, '06 01:28:42AM

Hi

I am a complete newbie to Mac and have zero knowledge of Applescripts.

I want to ask one doubt-

Can i use this script for Powermail 5.2.3 ?

Will it work if I replace mail with "powermail 5.2.3"

I need a script which polls all folders and also makes a sound when new mail arrives.

Thank you



[ Reply to This | # ]