Re: coerce record to string - does this work in other System languages?
Re: coerce record to string - does this work in other System languages?
- Subject: Re: coerce record to string - does this work in other System languages?
- From: "Mark J. Reed" <email@hidden>
- Date: Thu, 25 Mar 2010 19:31:00 -0400
That might be clearer with the dynamic script formatted more like regular code:
on getKey(someRecord, keyName)
set getter to (run script ¬
"script" & return & ¬
"on open(aRec)" & return & ¬
"return " & keyName & " of aRec" & return & ¬
"end open" & return & ¬
"end script")
tell getter to open(someRecord)
end getKey
But again, this is not AppleScript's strong suit. If you need to do
much of this sort of thing you're better off in a different
language...
On Thu, Mar 25, 2010 at 7:11 PM, Mark J. Reed <email@hidden> wrote:
> On Thu, Mar 25, 2010 at 6:54 PM, Luther Fuller <email@hidden> wrote:
>> The problem, now, is to get the value of a named record item.
>
> My pluck handler from the other thread does that, but across a list of records:
>
> item 1 of pluck("a", { {a:1, b:2, c:3} })
> 1
>
>> I tried using 'run script ...', but this does not work.
>
> It does, but you have to be tricky because you can't pass parameters
> to a 'run script', nor can the code inside the string access variables
> or properties from the surrounding context.
>
> The trick, which I believe I learned from has, is to use 'run script'
> to create a new script object containing a handler that takes a
> parameter, where the body of the handler incorporates the dynamic
> code. Then, back in normal non-tricky code, you just invoke that
> handler with the target object:
>
> on getKey(someRecord, keyName)
> set getter to (run script "script" & return & "on open(aRec)" & return & "re
> turn " & keyName & " of aRec" & return & "end open" & return & "end script")
> tell getter to open(someRecord)
> end getKey
>
> getKey({a:1, b:2, c:3}, "a") -- returns "1"
>
>
> --
> Mark J. Reed <email@hidden>
>
--
Mark J. Reed <email@hidden>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden