Re: Working With Records
Re: Working With Records
- Subject: Re: Working With Records
- From: Deivy Petrescu <email@hidden>
- Date: Thu, 15 Jan 2004 18:05:59 -0500
Records never seemed to work the way I wanted them and for that reason
avoided them. I need to understand them now and try to make use of
them. The following example results in an error "Can't get RecordNumber
of {FirstRecord:400, SecondRecord:800}
set RecordNumber to "FirstRecord"
set TheRec to {FirstRecord:400, SecondRecord:800}
get RecordNumber of TheRec
I know that if I write "get FirstRecord of TheRec" will return 400 and
work correctly, however my case is that I wont know what record will be
needed and thus need to use the variable RecordNumber to get the
correct record needed.
Can anyone shed a glimmer of light for me?
Thanks
Well, we'll try to shed some light.
I'll post two techniques that I've used, one is a
follow up of a clever observation by Richard23 ,
who apparently disappeared into thin air.
I developed this script based on the brilliant
observation by R23 that, if there is an error
message already coerced to text, why don't we use
it ?
So here:
<script>
property zRec : {}
property zLabel : ""
set zRec to {foo:3, bar:4, choo:5, pie:90}
set zLabel to "foo"
getRecusingLabel(zLabel, zRec)
on getRecusingLabel(keyName, someRec)
try
keyName of someRec
on error errnum
set AppleScript's text item delimiters to keyName
set t2 to text item 2 of errnum
set AppleScript's text item delimiters to ","
if (count of text items of t2) = 1 then
set AppleScript's text item delimiters to "}"
end if
set t2 to text item 1 of t2
set AppleScript's text item delimiters to ""
set zkey to characters 2 thru -1 of t2 as string
return zkey
end try
end getRecusingLabel
--> 3
</script>
This second script was developed by Olof Helman. And I post it "ipsis literis"
<script>
to usrf(theList)
script
{+class usrf;:theList}
end script
run script the result
end usrf
to extract_usrf(theRecord, fieldName)
script Fred
property r : theRecord
to extract() -- supply a dummy method
end extract
end script
set Amy to run script "script George
property r : {}
to extract()
return " & fieldName & " of r
end extract
end
George"
set Fred's extract to Amy's extract
return (Fred's extract())
end extract_usrf
set usrfList to usrf({"a", 1, "b", 2})
-->{a:1, b:2}
-- now use the string "b" to get b
extract_usrf(usrfList, "b")
--> 2
</script>
Notice that the second script also transforms a list into a record.
If you have any questions ask.
Use the one that suits you best. I have to say,
I've never timed either method to know which one
is faster.
--
Regards
Saudagues
Deivy
http://www.dicas.com
_______________________________________________
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.