[comp.sys.mac.hypercard] scope rules for scripts

brux@cs.utexas.edu (Mark Brucks) (01/04/91)

I want to send a message from card "two" to card "one", and have
card "one" update information in its fields.  Here is an simple
card script for card "one" (the card receiving the doit message):

on doit arg1, arg2
  put arg1 & arg2 after card field "field 1" of card "one"
  put card field "field 1" of card "one" & "foobar" into card field "field 2"
end doit

The first line of the script works perfectly.  However, the second
line generates an error message "No such card".  What gives?

I realize that the current card doesn't change when a message is
sent, so that means that references to field "field 1" in the above
script default to fields of card "two" (the card sending the message).
That is why I have referenced card "one" explicitly, even though the
script belongs to card "one".  This kind of scoping seems a bit
bizarre to me, and I can't find any explanation in the HC Script
Lang. Guide.  This notion of the current card not changing when
a message is sent seems to suspend the usual scoping rules.  Is
this right?  If so, is there some mysterious advantage to this
that I haven't discovered?

Anyone care to explain this to an experienced programmer who
is new to HyperTalk.  Thanks.

smelly@polari.UUCP (Tom Benedict) (01/05/91)

Regarding field and card references in HC: 
  I have not tryied your specific script, but you might try adding some
parens as per this example:

-- This is your code --
== on doit arg1, arg2
==  put arg1 & arg2 after card field "field 1" of card "one"
==  put card field "field 1" of card "one" & "foobar" into card field "field 2"
== end doit
 
-- Change line 3 to --
   put (card field "field 1" of card "one") & "foobar" into card field "field
2"
 
I don't know what the technical term is for this type of naming, but HC seems
to be looking for 'card "one" & "foobar"..' instead of 'card "one"'  !!
 
If I was a 'real' programmer I could explain it in computer science terms, but
unfortunately I'm just a HyperHacker...
 
I hope this helps. I know I used to run into this problem a lot, but now I just
keep adding parens until it works!
 
Tom Benedict
 

jdevoto@Apple.COM (Jeanne A. E. DeVoto) (01/06/91)

In article <16488@cs.utexas.edu> brux@cs.utexas.edu (Mark Brucks) writes:
>on doit arg1, arg2
>  put arg1 & arg2 after card field "field 1" of card "one"
>  put card field "field 1" of card "one" & "foobar" into card field "field 2"
>end doit
>
>The first line of the script works perfectly.  However, the second
>line generates an error message "No such card".  What gives?

This looks like a parsing-precedence problem to me. HyperTalk is interpreting
the second line as "put such and such a field of the card named "onefoobar"
into such and such a card field" -- in other words, the concatenation is
done before the object-name resolution. This can be fixed relatively easily:

  put (card field "field 1" of card "one") & "foobar" into card field "field 2"

>I realize that the current card doesn't change when a message is
>sent, so that means that references to field "field 1" in the above
>script default to fields of card "two" (the card sending the message).
>That is why I have referenced card "one" explicitly, even though the
>script belongs to card "one".  This kind of scoping seems a bit
>bizarre to me, and I can't find any explanation in the HC Script
>Lang. Guide.  This notion of the current card not changing when
>a message is sent seems to suspend the usual scoping rules.  Is
>this right?  If so, is there some mysterious advantage to this
>that I haven't discovered?

Basically, if a card is not specified, the current card is assumed. The
current card is the one you last went to, not the one where the current
handler is located.

The function "the target" returns the object to which the message whose
handler is currently executing was originally sent; the special object
descriptor "me" is the object whose handler is currently executing. For
instance, if the user clicks a button which has no script, but the mouseDown
message is caught by a handler in the background script, "the target" would
return the name of the button while "me" would be the name of the background.
In the case of your handler above, "the name of me" would return the same
thing as "the target".
-- 
========= jeanne a. e. devoto ========================================
 jdevoto@apple.com     |  You may not distribute this article under a
 jdevoto@well.sf.ca.us |  compilation copyright without my permission.
______________________________________________________________________
 Apple Computer and I are not authorized      |        CI$: 72411,165
 to speak for each other.                     |