-- Original from John Delacour's Scripts plug-in for Eudora;
-- this version contains changes by Markus Frischknecht (mailto:mfrischknecht@access.ch).
-- Feel free to use and change this script in whatever form you want.

-- This script checks mail only for a selected subset of personalities.
-- It will ask the user to select the set when the control key is pressed
-- when the script runs. Otherwise (without the control key) it will check for
-- new mail for the stored set of personalities. This script will only
-- run with Eudora Pro (Eudora Light doesn't implement personalities).

-- Needs the following osaxen (both available in the info-mac archive):
--    Choose From List (from GTQ Library, by Gregory Quinn)
--    Jon's Commands (by Jon Pugh)
-- NOTE: only checking for mail is done here, not sending! This can easily be changed
-- in the script

-- Algorithm:
--   count number of personalities
--   go through all of them, build list of those that check mail on manual check
--   set all of these to "do not check"
--   set the one personality of interest to "do check"
--   connect with checking mail
--   finally, restore setting of personalities in list, i.e. all those that do check on manual


-- If control key is down when the script opens, it will ask user to choose
-- which personalities to check mail for (saved in property, i.e. remembered between runs).


property checkThisPers : {} -- global variable for storing personalities to check

if "Control" is in (keys pressed) then
	-- ask for personality to check with this script
	set getMailNow to false
else
	set getMailNow to true
end if


-- set up double tell mechanism, a generic way for identifying programs
-- under any System from Mac OS 7.5 to 8.5 (and probably later)
tell application "Finder"
	try
		set EU to (process 1 whose creator type is "CSOm") as «class psn »
	on error
		-- Eudora is not running, so abort
		beep
		return
	end try
end tell


set s to 183 -- setting 183: if "n" then check on manual, if "y" then don't check
set persCount to 0
set pNames to {}
set restoreList to {}


tell application "Eudora Pro 3.1.3" to tell EU
	-- activate	-- not used here because the script is usually started with EU in front
	
	if getMailNow then
		
		if checkThisPers = {} then
			-- nothing set yet, display error message and quit
			display dialog ¬
				"No personality to check is selected." & return & ¬
				"Please re-run this script with control key down to select one." buttons {"OK"} default button "OK"
			return
		end if
		
		-- first store current setting for each personality
		set persCount to count personalities
		
		repeat with i from 1 to persCount
			try
				set thisPers to (get name of personality i)
				
				set howSet to (get setting s of personality i)
				
				-- store those that need restoring at end in a list
				if howSet is "n" then
					set end of restoreList to thisPers
					set setting s of personality i to "y"
				end if
			on error
				beep
				exit repeat
			end try
		end repeat
		
		-- then only set the desired pers. to check on manual
		
		repeat with i in checkThisPers
			set howSet to i as text
			set setting s of personality howSet to "n"
		end repeat
		
		-- now connect to server and check for mail
		try
			connect with checking -- but no sending and no waiting for idle!
			
			-- at this point, mail is downloaded, so
			-- reset settings for all personalities we changed;
			
			-- first clear setting (i.e. set to "y") from the list we just checked
			-- because some entries in this list might have been set to "y"
			-- before, so reset these before restoring original set with
			-- setting "n" (slightly complicated, but works...)
			
			repeat with i in checkThisPers
				set howSet to i as text
				set setting s of personality howSet to "y"
			end repeat
			
			-- then restore initial settings for "n"
			repeat with i in restoreList
				set howSet to i as text
				set setting s of personality howSet to "n"
			end repeat
			
		on error
			-- on error: restore settings as at the start; same as above
			
			repeat with i in checkThisPers
				set howSet to i as text
				set setting s of personality howSet to "y"
			end repeat
			
			-- then restore initial settings for "n"
			repeat with i in restoreList
				set howSet to i as text
				set setting s of personality howSet to "n"
			end repeat
			
		end try
		
		
	else
		-- control key was down on starting script, so
		-- get personality which should be checked (one or several)
		set persCount to count personalities
		
		repeat with i from 1 to persCount
			try
				set thisPers to (get name of personality i)
				set end of pNames to thisPers
			on error
				beep
				exit repeat
			end try
		end repeat
		
		
		-- osax to display list, show all current personalities
		
		set newSet to choose from list pNames prompt ¬
			"These personalities are going to be checked the next time you run this script. Command-click to highlight the personalities that should check." initially select checkThisPers with multiple selections and empty lists
		
		if class of newSet is boolean then
			if not newSet then
				-- if newSet is false, "cancel" button was clicked: quit
				return
			end if
		end if
		if checkThisPers = newSet then return -- no change in selection: quit
		
		-- save new list of personalities to check
		set checkThisPers to newSet
	end if
end tell