Re: Wierd error
Re: Wierd error
- Subject: Re: Wierd error
- From: "Arthur J. Knapp" <email@hidden>
- Date: Tue, 23 Sep 2003 15:25:14 -0400
>
Date: Tue, 23 Sep 2003 00:15:40 -0500
>
Subject: Wierd error
>
From: John Fowler <email@hidden>
>
Dear sirs,
Umm... there are women on this list. Not that any of them ever call
me or anything.... ;-)
>
Here is the handler that suddenly behaves wierdly:
>
on Trim(aString)
I wasn't able to duplicate your error, but I have some general
comments:
...
>
set AppleScript's text item delimiters to ""
>
set theStringList to (every text item in aString)
This works, but is unnessesary and can be done in one line without
having to worry about the text item delimiters:
set theStringList to every character of aString
-- OR:
set theStringList to characters of aString
-- OR:
set theStringList to aString's characters
...
>
repeat with anItem in theStringList
>
set newString to newString & anItem
>
end repeat
This is very unnessesary. Simple ensure that the text item
delimiters are set to an empty string and use the "as string"
syntax:
set newString to theStringList as string
>
on TrimTheStringList(theStringList)
...
>
if ((ASCII number of item 1 of theStringList) is greater than 64)
>
and ((ASCII number of item 1 of theStringList) is less than 91) then
Wow, I'm surprised that the above even compiles. The ASCII commands
take a direct parameter, which means that the "of" shouldn't be there:
if ( ( ASCII number ( item 1 of theStringList ) ) > 64 )
and ( ( ASCII number ( item 1 of theStringList ) ) < 91 ) then
Let me throw some handlers at you:
(* Rather than bother with the slow ASCII commands from Standard
* Additions, AppleScript algorithms often do better to use the
* "contains" or "is in" syntax to check a character's value.
*
* Note this property only requires one "case," as AppleScript's
* normal comparison operators are case-insensitive by default.
*)
property ksAlphanumeric : "abcdefghijklmnopqrstuvwxyz0123456789"
on SideTrimToAlphanumeric_v01(s) -- yeah, the name needs some work. ;-)
set len to s's length
if (len = 0) then return ""
repeat with i from 1 to len
if (s's character i is not in ksAlphanumeric) then
set i to i + 1
else
exit repeat
end if
end repeat
if (i > len) then return ""
repeat with j from len to i by -1
if (s's character j is not in ksAlphanumeric) then
set j to j - 1
else
exit repeat
end if
end repeat
return s's text i thru j
end SideTrimToAlphanumeric_v01
(* Version 1 above needs to check the string's length in two
* places so that it doesn't choke on a string with no
* alphanumeric characters. We can avoid this check by simply
* catching "out of bounds" situations in a try-statement.
*)
on SideTrimToAlphanumeric_v02(s)
try
repeat while (s's character 1 is not in ksAlphanumeric)
set s to s's text 2 thru -1
end repeat
repeat while (s's character -1 is not in ksAlphanumeric)
set s to s's text 1 thru -2
end repeat
on error
return "" -- presumably, s contained no alphanumerics
end try
return s
end SideTrimToAlphanumeric_v02
(* Finally, here is a version that uses a method similar to
* yours: working with a list of characters:
*)
on SideTrimToAlphanumeric_v03(s)
set a to s's characters
repeat 2 times
try
repeat while (a's item 1 is not in ksAlphanumeric)
set a to a's rest
end repeat
on error
return "" -- presumably, s contained no alphanumerics
end try
set a to a's reverse
end repeat
return a as string
end SideTrimToAlphanumeric_v03
{ Arthur Knapp, of <
http://www.STELLARViSIONs.com>
a r t h u r @ s t e l l a r v i s i o n s . c o m
"Love is like oxygen..."
}
_______________________________________________
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.