s22 Tech

Useful code.

AppleScript code for MacOS Mail.app

Individually check Mail accounts at different times

As of MacOS Catalina, Mail.app still does not allow you to check individual accounts at specific times.  It's either all or nothing.  This script will allow you to check some of your mail accounts more frequently than others.  You'll need to run the "Get Account Names" script first, and then paste the results into the "Check Mail" script.

Tested on MacOS 10.6.2 to 10.15.7

This script is offered 'as-is' and is unsupported.


(*
PURPOSE:
Checks individual Apple Mail accounts at different intervals that you determine.

INSTRUCTIONS:
Set Mail's preferences to "Check for new mail: Manually".
Save this script as an Application and select the Stay Open option.
*)


global theMinimumInterval, theAccountList, theMinuteCount, ScriptName, isMailRunning

if app_is_Running("Mail") then
set isMailRunning to "Yes"
else
set isMailRunning to "No"
end if

set scriptPath to path to me

-- Get full name of this script.
tell application "Finder"
set scriptNameFull to name of scriptPath
set theExt to name extension of scriptPath
end tell

-- Get the name of this script, minus the extension
if theExt is not "" then
set ScriptName to (text items 1 thru -(2 + (length of theExt)) of scriptNameFull) as string
else
set ScriptName to scriptNameFull
end if

-- Hide this script's icon in the Dock
tell application "System Events"
set visible of process ScriptName to false
end tell

tell application "Mail"
activate
if isMailRunning = "No" then
delay 8 -- wait for Mail to redraw the screen before checking for new mail (to prevent possible crash?)
check for new mail
end if
set theMinimumInterval to 0
--paste result of account names script here and set intervals for each account
set theAccountList to ¬
{¬
{account "info" of application "Mail", 15}, ¬
{account "orders" of application "Mail", 45}, ¬
{account "support" of application "Mail", 60}, ¬
{account "personal" of application "Mail", 90}, ¬
{account "G_Mail" of application "Mail", 20}, ¬
{account "Yahoo_Mail" of application "Mail", 30} ¬
}
--end paste area

set theCount to 0
repeat with eachAccount in theAccountList
set theInterval to item 2 of eachAccount
if theInterval ≠ 0 then
set theCount to theCount + 1
if (theCount > 1) then
set theMinimumInterval to my gcd(theInterval, theMinimumInterval)
else
set theMinimumInterval to (theInterval as integer)
end if
end if
end repeat
if theMinimumInterval = 0 then
display dialog "No accounts have been set to check (values must be greater than 0)."
return
end if
end tell

set theMinuteCount to 0
on idle
set theMinuteCount to theMinuteCount + theMinimumInterval
repeat with eachAccount in theAccountList
set theAccount to item 1 of eachAccount
set theInterval to item 2 of eachAccount
if theInterval ≠ 0 then
set theMod to theMinuteCount mod theInterval
if (theMod = 0) then
tell application "Mail" to check for new mail for theAccount
end if
end if
end repeat
return (theMinimumInterval * 60)
end idle


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(appName)
tell application "System Events" to (name of processes) contains appName
end app_is_Running
Here is the "Get Account Names" script.

--get list of accounts to assign to theAccountList variable
tell application "Mail"
set theAccountList to "set theAccountList to ¬" & return & "{¬" & return as string
set theAccounts to accounts
set theAccountCount to count of theAccounts
set theSeperator to ","
set theCount to 0
repeat with eachAccount in theAccounts
set theCount to theCount + 1
if theCount = theAccountCount then
set theSeperator to ""
end if
set theString to name of eachAccount as string
set theLine to "{account \"" & theString & "\" of application \"Mail\", 0 }" & theSeperator & " ¬"
set theAccountList to theAccountList & theLine & return
end repeat
set theAccountList to theAccountList & "}" & return
display dialog "Account List:" default answer theAccountList
end tell
Here is an alternate version of "Get Account Names". It allows you to append the check mail frequency to the end of your Mail account names, so when you run the AppleScript, the time frequency will be populated automatically.

--get list of accounts to assign to theAccountList variable
tell application "Mail"
set theAccountList to "set theAccountList to ¬" & return & "{¬" & return as string
set theAccounts to accounts
set theAccountCount to count of theAccounts
set theSeperator to ","
set theCount to 0
repeat with eachAccount in theAccounts
set theCount to theCount + 1
if theCount = theAccountCount then
set theSeperator to ""
end if
set theNAME to name of eachAccount as string
set theTIME to text ((offset of "/" in theNAME) + 1) thru -1 of theNAME

set theLine to "{account \"" & theNAME & "\" of application \"Mail\", " & theTIME & "}" & theSeperator & " ¬"
set theAccountList to theAccountList & theLine & return
end repeat

set theAccountList to theAccountList & "}" & return
display dialog "Account List:" default answer theAccountList
end tell