(*
This script uses the email account names stored in the accounts.data file. I name my accounts in Mail like so:
personal/15
business/5
The number is the interval in minutes for how often I want the account to be checked.
The script splits the account name and grabs the number, then checks each account using that interval. Keeping the accounts list in a separate file allows the list to be updated on the fly, if I was to change any interval in Mail.
*)
global got_accounts, the_minute_count, the_minimum_interval, the_accounts_list, first_run, new_accounts_list
global accounts_file
set the_minimum_interval to 1
set got_accounts to "No"
set the_minute_count to 0
set first_run to "Yes"
----------------------------------------------------------------
-- Create "the_accounts_list" from the accounts.data file.
----------------------------------------------------------------
set accounts_file to "/Applications/Check_Mail.app/Contents/Resources/Scripts/accounts.data" as POSIX file
set the_accounts_list to {}
set the_accounts to paragraphs of (read accounts_file)
repeat with each_account in the_accounts
if length of each_account is greater than 0 then
set end of the_accounts_list to each_account as list
end if
end repeat
---------------------------------------------
on idle
if App_is_Running("Mail") then
-- Check all accounts on the first run.
if first_run = "Yes" then
tell application "Mail"
activate
check for new mail
end tell
set first_run to "No"
end if
if got_accounts = "No" then
my Get_Accounts()
set got_accounts to "Yes"
end if
set the_minute_count to the_minute_count + the_minimum_interval
tell application "Mail"
repeat with each_account in the_accounts_list
set the_account to item 1 of each_account
set the_name to the_account as rich text
set the_array to my The_Split(the_name, "/")
set the_interval to item 2 of the_array
if the_interval ≠ 0 then
set theMod to the_minute_count mod the_interval
if (theMod = 0) then
try
check for new mail for account the_account
on error
-- If any email account names were changed in Mail.
my Update_eAccounts()
end try
end if
end if
end repeat
end tell
return (the_minimum_interval * 60)
else
return 60
end if
end idle
on Get_Accounts()
set the_minimum_interval to 0
set the_count to 0
tell application "Mail"
repeat with each_account in the_accounts_list
set the_account to item 1 of each_account
set the_name to the_account as rich text
set the_array to my The_Split(the_name, "/")
set the_interval to item 2 of the_array
if the_interval > 0 then
set the_count to the_count + 1
if (the_count > 1) then
set the_minimum_interval to my GCD(the_interval, the_minimum_interval)
else
set the_minimum_interval to (the_interval as integer)
end if
end if
end repeat
end tell
if the_minimum_interval = 0 then
display dialog "No accounts have been set to check (values must be greater than 0)."
return
end if
end Get_Accounts
on Update_eAccounts()
-- Get account names from Mail and create a list.
-- The time frequency is grabbed from the digits after the "/" in the account name.
-- This function is called only when an email account name is changed in Mail.
set new_accounts_list to ""
set the_cnt to 0
tell application "Mail"
set the_accounts to accounts
set the_account_count to count of the_accounts
repeat with each_account in the_accounts
set the_cnt to the_cnt + 1
set the_account_name to name of each_account as string
if the_account_name contains "/" then
set the_freq to rich text ((offset of "/" in the_account_name) + 1) thru -1 of the_account_name
if the_freq > 0 then
if the_cnt = the_account_count then
-- Don't add a newline to the last line of data file.
set new_accounts_list to new_accounts_list & the_account_name
else
set new_accounts_list to new_accounts_list & the_account_name & "\n"
end if
end if
end if
end repeat
end tell
set the data_file to open for access file accounts_file with write permission
write new_accounts_list to data_file
close access data_file
my Get_Accounts()
end Update_eAccounts
on The_Split(the_string, the_delimiter)
set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to the_delimiter
set the_array to every text item of the_string
set AppleScript's text item delimiters to old_delimiters
return the_array
end The_Split
on GCD(a, b) -- Greatest Common Divisor**
repeat until b = 0
set x to b
set b to a mod b
set a to x
end repeat
return a
end GCD
on quit
continue quit
end quit
on App_is_Running(app_name)
tell application "System Events" to (name of processes) contains app_name
end App_is_Running