Re: Handler defies logic
Re: Handler defies logic
- Subject: Re: Handler defies logic
- From: kai <email@hidden>
- Date: Thu, 25 Sep 2003 23:36:31 +0100
on Thu, 25 Sep 2003 08:26:10 -0500, Rich Carroll wrote:
>
I am experiencing a strange error. If I recursively call this handler a
>
second time, it holds information from the first iteration. For example, if
>
you run it and enter an invalid string like ".p" it will catch the "." as
>
invalid and invoke itself again prompting for a new string. If the second
>
time through you enter a valid string like "dd" it will iterate through the
>
second string, step out of the repeat loop, set the final "folderDescript"
>
variable, AND THEN GO BACK INTO THE REPEAT LOOP! So your result would be
>
"__ddp" Two underscores and all the valid characters. I'm out of ideas.
>
What am I missing? Thanks in advance for your help.
>
>
property validChars :--no break
>
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" --no break
>
as Unicode text
>
global folderDescript
>
global txt
>
>
on run
>
validateFolderDescript()
>
end run
>
>
on validateFolderDescript()
>
set txt to (text returned of (display dialog "Enter main --no break
>
folder description.
>
Maximum of 24 alphanumeric characters." default answer --no break
>
"myJobFolder")) as string
>
set folderDescript to ""
>
set chars to count txt
>
if chars is greater than 24 then
>
display dialog "Maximum of 24 characters"
>
validateFolderDescript() of me
>
end if
>
repeat with i in txt
>
if i is in validChars then
>
set folderDescript to folderDescript & i
>
else
>
display dialog "\"" & i & "\"" & " is a bad character.--no break
>
You may only use numbers or letters"
>
validateFolderDescript() of me
>
end if
>
end repeat
>
set folderDescript to "_" & folderDescript
>
end validateFolderDescript
>
The handler's logic seems fine, Rich - if we examine exactly what the script
does...
Whenever an 'error' condition (either a long name or bad character) is
encountered, the handler calls itself. However, the current iteration of the
handler has not yet been completed. So, after calling itself (and doing
whatever is specified in any subsequent iteration), the handler resumes
completion of the earlier iteration(s).
This isn't too noticeable if the error has been caused by a long string
(although the leading underscore character is repeated for each error).
The problem manifests itself more visibly when a bad character is entered -
especially since the repeat loop is also allowed to continue. In this case,
any good characters that follow a bad character are added to the results
returned from subsequent iterations.
Possibly the simplest way around this is to change each
'validateFolderDescript()' call (within the handler) to 'return
validateFolderDescript()'.
Your posted script may have been extracted from a larger one but, in the
context of the one shown here, there seems little to be gained from
declaring the variables 'folderDescript' and 'txt' as global. There's also
no need here to add 'of me' after each 'validateFolderDescript()', to coerce
the property 'validChars' to Unicode text - or to coerce a dialog's text
returned to a string. (But now I'm just gettin' picky.) :-)
To be honest, I doubt that a recursive handler is the best answer in this
kind of situation, since a simple repeat/try block could handle the job
quite adequately:
-------------------------------------------------------
(Any wrapped lines abutting the left edge of the window
should be reconnected to the end of the previous line)
-------------------------------------------------------
--=====================
property validChars :
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
on run
set folderDescript to validateFolderDescript()
-- do something with folderDescript
end run
on validateFolderDescript()
repeat
set txt to text returned of (display dialog "Enter main folder
description. Maximum of 24 alphanumeric characters." default answer
"myJobFolder")
try
if (count txt) is greater than 24 then error "Maximum of 24 characters"
repeat with i in txt
if i is not in validChars then error "\"" & i & "\"" & " is a bad
character. You may only use numbers or letters."
end repeat
exit repeat
on error e
display dialog e
end try
end repeat
"_" & txt
end validateFolderDescript
--=====================
---
kai
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.