Re: AppleScript mystery
Re: AppleScript mystery
- Subject: Re: AppleScript mystery
- From: Paul Berkowitz <email@hidden>
- Date: Wed, 21 Apr 2004 07:20:01 -0700
On 4/20/04 10:49 PM, "David Crowe" <email@hidden> wrote:
>
Can anybody explain why:
>
>
set theString to "abc def"
>
repeat with aCharacter in theString
>
display dialog "> " & (class of aCharacter)
>
return
>
end repeat
>
>
Produces the output "> string", yet:
>
>
>
set theString to "abc def"
>
repeat with aCharacter in theString
>
display dialog (class of aCharacter)
>
return
>
end repeat
>
>
Produces the output "TEXT"?
'display dialog' requires a string to display. It will not display other
types, although it will sometimes try coercing other types (like numbers) if
it can.
In the first case, the " > " to the left of the concatenation operator &
coerces the right side (class of aCharacter) to a string as well. The
resulting concatenated string "> string" is dosplayed without problem.
In the second case, you're throwing a 'class' type, not a string, at
'display dialog'. (We'll get to that in a moment.) If. instead you give it a
string by explicit coercion:
set theString to "abc def"
repeat with aCharacter in theString
display dialog ((class of aCharacter) as string)
return
end repeat
--> "string"
or implicit coercion via concatenation:
set theString to "abc def"
repeat with aCharacter in theString
display dialog ("" & (class of aCharacter))
return
end repeat
--> "string"
you'll get the result you're looking for. You can even omit the parentheses
I've included to make things clearer.
Otherwise, AppleScript does what it can.
If you type
<<class TEXT>>
in a script editor - only use real guillemets made by option-\ and
shift-option-\ on English-language keyboards, which get mangled by this
stupid list server so I can't type them here - and compile it, you'll see it
compile to 'string' (no quotation marks). That's the raw code - which is
what the compiler sees. That's what's getting coerced to a string ("TEXT")
that display dialog can use when you don't do the proper coercion to string
in the command yourself. It can't be called a bug because you're ignoring
the requirement of display dialog to give it a string. At worst it can be
called an "unexpected coercion".
(And why are you sending us a repeat loop with an immediate 'return'? Quick
adaptation of your real script, I guess?) In future just coerce any
non-string (or non-Unicode text in Panther) to string first when using
display dialog.
--
Paul Berkowitz
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.