Re: Fwd: How the heck do you get records to work?
Re: Fwd: How the heck do you get records to work?
- Subject: Re: Fwd: How the heck do you get records to work?
- From: Nigel Garvey <email@hidden>
- Date: Fri, 23 Jan 2004 10:01:14 +0000
Kurt Godden wrote on Thu, 22 Jan 2004 17:31:33 -0500:
>
> Sorry for the newbish question, but I'm having a heck of a time
>
> figuring out how to access data from a record by referencing the key
>
> with a variable. What I want to do is something like this:
>
> set foo to {one:"1", two:"2"}
>
> set bar to "one"
>
> set baz to bar of foo
>
>
>
> where baz then gets set to "1". But of course, the interpreter always
>
> takes 'bar' as a literal and tells me it cannot find 'bar' within the
>
> foo record. (value of bar) doesn't work either.
As BJ and Chris have pointed out, records weren't designed to be used
this way and it's best not to try. However, it can be done - pretty
slowly - like this:
set foo to {one:"1", two:"2"}
set bar to "one"
try
bar of foo
on error msg
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "{"
set scrpt to bar & " of {" & text from text item 2 to -1 of msg
set AppleScript's text item delimiters to "}"
set scrpt to text 1 thru text item -2 of scrpt & "}"
set AppleScript's text item delimiters to astid
set baz to (run script scrpt)
end try
baz --> "1"
Also - I don't know if this can still be done in Panther - there's a bug
in the clipboard commands that can be exploited. :-) This is obviously
not recommended:
set foo to {one:"1", two:"2"}
set bar to "two"
set the clipboard to foo
set fool to the clipboard
repeat with i from 1 to (count fool) by 2
if item i of fool is bar then
set baz to item (i + 1) of fool
exit repeat
end if
end repeat
baz --> "1"
NG
_______________________________________________
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.