Page 1 of 1
variable names in other variables
Posted: Fri May 15, 2009 12:34 am
by WaltBrown
Sorry, I saw the answer to this or a stack here somewhere and now cannot find it.
How can I get the value of say a property whose name is in a variable, as in:
put "backcolor" into tString
answer tString with "OK"
This returns "backcolor", but what I want is it to return the backcolor property value, like "white" or "#FFFFFF".
Thanks, Walt
Posted: Fri May 15, 2009 1:41 am
by acidjazz
Walt,
Let's say it's a field you're interested in...
Code: Select all
on mouseUp
put the backgroundcolor of field 1 into tString
answer tString
end mouseUp
If there is no background color set, the answer box will be completely blank. If there is a color, you'll get the RGB values for the color.
Of course, you don't need the intervening variable to get the values...
Code: Select all
on mouseUp
answer the backgroundcolor of field 1
end mouseUp
Cheers,
Mark P.
Posted: Fri May 15, 2009 6:29 am
by Janschenkel
The
do command and the
value function can be used to dynamically execute commands or evaluate expressions.
Code: Select all
on mouseUp
-- example of value function
put 9 into x
put "sqrt(x)" into y
answer value(y)
-- example of do command
put "answer value(y)" into z
do z
end mouseUp
So in your case it could be something like:
Code: Select all
on mouseUp
put "backColor" into tPropertyName
put ("answer the " & tPropertyName & " of me") into tCommand
do tCommand
end mouseUp
The
merge function can also save some time and make assembling such dynamic expressions and command a lot mpore readable:
Code: Select all
on mouseUp
put "backColor" into tPropertyName
do merge("answer the [[tPropertyName]] of me")
end mouseUp
For some examples of what you can accomplish using the merge function, read these newsletter articles:
HTH,
Jan Schenkel.
Posted: Fri May 15, 2009 7:03 am
by acidjazz
Jan,
Very clever. It makes more sense to "do" it dynamically. (Sorry, I couldn't help myself)

Mark P.
Posted: Fri May 15, 2009 11:42 am
by SparkOut
Just a word about "the Power of Merge" - these are a series of incredibly useful articles which will help you get to grips with the concept of streamlining the dynamic code replacement. Very good stuff - only the online newsletter page has rendered the html entity " as a literal in the code boxes, and not applied the view as a " character.
This makes it a little confusing as Jan mentions that one of the advantages is to make the code more readable - but the rendering of " as " in the newsletter page actually makes it seem worse. Please don't get confused by this - it's not bad writing or confusing information from Jan, if you replace " with " then it will all become a lot clearer.
Posted: Fri May 15, 2009 1:31 pm
by Janschenkel
Wrt the " in the article: according to Heather, it was a deficiency in the code-to-html converter they used for tyhat article; but as this is in the process of conversion to a CMS, it should be rectified eventually.
Jan Schenkel.
Posted: Fri May 22, 2009 12:18 am
by WaltBrown
Thanks! Great stuff!