Re: Coercion of string to a record property label
Re: Coercion of string to a record property label
- Subject: Re: Coercion of string to a record property label
- From: Harald E Brandt <email@hidden>
- Date: Tue, 9 Oct 2001 22:58:35 +0200
Why must life be so immensely complicated??
It makes me even strongly question the strength of records in AppleScript!
I do thank you all smart guys who answered me with various tricky solutions, but I really wonder why AppleScript itself doesn't offer any solutions, such as (just making something up here):
(aRegion as label) of myRecord
--where aRegion is a string, such as "EU"
--and the record contains a property EU:something
Obviously there are many people wanting to do similar things.
E.g, I received one fun solution where this task was accomplished by actually parsing the error message you get if you crudely just do: 'aRegion of myRecord'. Although fun, Apple can't possibly mean their language is supposed to be (mis)used that way to retrieve properties in a record in case the labels come from variables!? The same doubts hold for the exquisitely complicated solution below, which obviously is a thousand light-years away from the attitude of "Natural English" Applescript!
In my case, I think I will do an ugly sequence of if-clauses!
Exquisitely primitive, but it compiles, doesn't depend on error message formats, and... it seems the way Apple wants it.....
Best Regards.
/Harald
+--> List Guy wrote 01-10-09:
>
[BEGIN BRILLIANT SOLUTION]
>
>
Calculating a Property Label
>
"Neal A. Crocker" <email@hidden>
>
>
------------------------------------------------------------------
>
set z to "red"
>
set color_record to {red:1.254, yellow:56.401, blue: 23.9}
>
get makereference(z,color_record)
>
------------------------------------------------------------------
>
>
I started playing around with the "run script" command to try and make a
>
function that returns a reference to a field in a record given the name of
>
the field as a string and the record as arguments. I eventually figured it
>
out and I've included it below. Please read the notes below the script.
>
It's tricky to compile! (By the way, the "target" argument of the
>
"makereference" function can be a script with a "bar" property rather than a
>
record with a "bar" field.)
>
>
-------------------------------------------------------------------
>
on makereference(propname, target)
>
script refscript
>
--{class:reference,+class form;:+constant ****usrp;, +class
>
want;:+class prop;, +class seld;:propname, +class from;:target}
>
{class:reference, +class form;:+constant ****usrp;, +class
>
want;:property, +class seld;:propname, from:target}
>
end script
>
run script refscript
>
end makereference
>
>
set blah to makereference("bar", {bar:2})
>
{blah, blah + 1} -- {bar of {bar:2}, 3}
>
-------------------------------------------------------------------
>
>
The script "refscript" inside the function "makereference" has a commented
>
line and an uncommented line. The uncommented line won't compile because
>
script editor chokes on the use of the words "from" and "property".
>
However, to compile the script, the uncommented line can be deleted and the
>
commented line can be uncommented. Then the script will compile. The
>
formerly commented line will be converted by script editor to look like the
>
uncommented which was deleted. If you plan to compile a script containing
>
this function more than once, it is best to make a copy of the commented
>
line for reuse.
>
>
[END BRILLIANT SOLUTION]
+-
______heb________________________________________________________________
Harald E Brandt email@hidden PGP/MIME aware
http://heb.bragit.com
References: | |
| >EU as text (From: List Guy <email@hidden>) |