-- Markus Frischknecht 17/9/1999
-- <mailto:mfrischknecht@access.ch>
-- <http://www.markus.frischknecht.net/>

-- Script to handle x-eudora-setting:9603 etc in Eudora versions < 4;
-- should be saved as application, without startup screen and not as stay-open app;
-- then in Eudora click on <x-eudora-settings:xyz> and choose
-- the script application as the handler for this protocol

-- USE AT YOUR OWN RISK! You can render your Eudora preferences unusable with
-- this script, so make sure you apply it to the right settings.

-- Commented out in this version: MIME quoted-printable translation

-- what Eudora sends: standard Get Url event with direct parameter, i.e.
-- «event GURLGURL» "x-eudora-setting:9603=25"
-- (note that the <> around URL are not included since they are the delimiters)
-- in case of problems with special characters:
-- « and » are the French guimets, i.e. << and >> in one character; on a US or UK
-- keyboard, these are the option-backslash and option-shift-backslash


on «event GURLGURL» URLstring
	try
		
		-- process string: parse into URL type, pref number and option "=string"
		
		-- first comparison might be ignored since eudora only triggers script for this URL type;
		-- but since this script could be installed as handler for any type of
		-- URL, better make sure before we start playing with Eudora's settings:
		
		set GURLtype to text 1 thru 17 of URLstring
		
		if GURLtype is not "x-eudora-setting:" then -- case insensitive by default
			display dialog "The URL " & URLstring & return & ¬
				"is not recognised as a setting for Eudora!" buttons {"OK"} default button "OK"
			return -- quit immediately
		end if
		
		-- it's x-eudora-setting, so get rest of string
		
		set newValue to text 18 thru -1 of URLstring
		
		-- get pref number and (if present) new value for it; separated by "="
		set savedDelimiters to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"="}
		set entries to text items of newValue
		
		-- restore delimiter
		set AppleScript's text item delimiters to savedDelimiters
		
		-- if now only one item in list, no new value was given for this setting
		-- else if there are two items, first is setting number, second is new value
		
		try
			set prefnumber to first item of entries as number -- NB: might trigger an error (no valid number)
		on error
			display dialog "Error: the expression \"" & first item of entries & ¬
				"\" can't be converted into a number." with icon stop buttons {"OK"} default button "OK"
			return
		end try
		
		
		-- if Eudora already has a value for this setting, display it;
		-- else tell user that this pref doesn't exist yet (i.e. maybe
		-- Eudora 3 doesn't support it)
		
		-- first set up double-tell to avoid having to identify Eudora
		tell application "Finder"
			try
				-- Mac OS 7.5 to 8.5 at least
				set eudora 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
		
		
		tell application "Eudora Pro 3.1.3" to tell eudora
			set currentValue to setting prefnumber -- current value in Eudora
			-- display dialog currentValue
			
		end tell
		
		if currentValue = "" then -- value was previously unused
			set emptySetting to true
		else
			set emptySetting to false
		end if
		
		-- set up dialog and display it	
		
		if (count entries) = 1 then
			-- if no new value given, put Eudora's value into editable field of dialog
			if emptySetting then
				set dialogtext to "Setting " & prefnumber & " is not yet defined in Eudora." & return & ¬
					"Set it to:"
			else
				set dialogtext to "Setting " & prefnumber & " is currently defined in Eudora." & ¬
					return & "Edit its value:"
				
			end if
			
			-- and display dialog
			
			set choice to display dialog dialogtext buttons {"Cancel", "OK"} ¬
				default answer currentValue default button "OK"
			
		else
			-- two elements in list: setting number, value for this setting;
			-- if new value given, put that into editable text field and give 
			-- Eudora's value in text above
			
			
			if emptySetting then
				set dialogtext to "Setting " & prefnumber & " is not yet defined in Eudora." & ¬
					return & "Set it to:"
				
			else
				set dialogtext to "Setting " & prefnumber & " is defined in Eudora as:" & return & ¬
					currentValue & return & "Change it to:"
				
			end if
			
			-- and display dialog
			set choice to display dialog dialogtext buttons {"Cancel", "OK"} ¬
				default answer (second item of entries) default button "OK"
			
			-- furthermore: test if really just two elements in "entries" list;
			-- too many entries are possible, but they are just ignored here; a=b*c=d will be
			-- interpreted as a, b*c, d; setting a is set to "b*c"
		end if
		
		-- if OK clicked, tell Eudora to store this value; if cancel is clicked,
		-- we don't get here anyway since AS quits script silently
		
		set adjustedValue to text returned of choice
		
		-- maybe necessary: convert new value from MIME printed-quotable to text,
		-- using an osax such as DecodeQP osax:
		-- set adjusted to decode quoted printable adjusted
		
		if adjustedValue is not currentValue then -- changed compared to previous value
			tell application "Eudora Pro 3.1.3" to tell eudora
				set setting prefnumber to adjustedValue -- current value in Eudora
				-- display dialog currentValue
				
			end tell
		end if
		
		
	on error errMsg number errnumber
		-- tell user what went wrong, but only if not caused by click on cancel (or cmd-period)
		if (errnumber is not -128) then
			if number of characters of errMsg > 230 then
				set errMsg to text 1 thru 230 of errMsg -- if too long, cut rest
			end if
			display dialog "x-eudora-setting: error " & errnumber & ", " & errMsg with icon stop buttons {"Abort"} default button "Abort"
		end if
		
	end try
	
end «event GURLGURL»