records and usrflists
records and usrflists
- Subject: records and usrflists
- From: Richard 23 <email@hidden>
- Date: Thu, 15 Feb 2001 06:35:50 -0800
For what it's worth I found a method to get the user defined fields from a
record, also referred to as "ASUserRecordFields", using the clipboard.
-- ---------------------------------------------------------
-- Preprocessed by Convert Script 1.0d2
-- ---------------------------------------------------------
--
---------------------------------------------------------------------------
--
-- trick AppleScript into returning the contents of the
ASUserRecordFields key
-- from the AERecord of which AppleScript records are a superset. A list
is
-- returned which alternates between user defined key names (text) and
values.
-- returned is a list alternating between user defined key (string) and
values.
--
---------------------------------------------------------------------------
--
-- uses the clipboard for data coercion and restores to initial value
when done.
--
---------------------------------------------------------------------------
--
on ExtractUsrf(theRecord)
set theVal to the clipboard
set usrflist to (set the clipboard to theRecord) as list
set the clipboard to theVal
return usrflist
end ExtractUsrf
--
---------------------------------------------------------------------------
--
-- haven't found a practical way to deal with the undefined value. It
cannot
-- be stored in a value, attempts to get it result in a "no result" error.
-- The clipboard returns the empty list for an empty clipboard, but
setting it
-- to {} results in an empty list, not an empty clipboard!
--
---------------------------------------------------------------------------
--
-- attempt to handle undefined value properly, return result of set
on set the clipboard to theVal
if theVal = {} then set theVal to current application
continue set the clipboard to theVal
return the clipboard
end set the clipboard to
--
---------------------------------------------------------------------------
--
-- a semi-practical application of the ExtractUsrf handler
--
---------------------------------------------------------------------------
--
on GetValueByKey(theRec, theKey)
set {theKey, theList} to {theKey as string, ExtractUsrf(theRec)}
tell theList to repeat with theIdx from 1 to length by 2
if item theIdx contains theKey then return item (theIdx + 1)
end repeat
end GetValueByKey
-- ---------------------------------------------------------
This may only be a barnyard oddity since it fails if any predefined labels
are used. However wrapping them labels in braces makes them user defined.
Here are some examples:
ExtractUsrf({jan:1, feb:2, mar:3, CaseIsNotPreserved:true})
--> {"jan", 1, "feb", 2, "mar", 3, "caseisnotpreserved", true}
ExtractUsrf({jan:1, feb:2, mar:3, type class:no})
--> {}
set theMonthRecord to {jan:1, feb:2, mar:3, apr:4, |may|:5}
GetValueByKey(theMonthRecord, "apr")
--> 4
GetValueByKey(theMonthRecord, May)
--> 5
set theMonthRecord to {|January|:1, |February|:2, |March|:3, |April|:4,
|May|:5}
GetValueByKey(theMonthRecord, February)
--> 2
Oh well, it's a start. But GetValueByKey using this method may be just as
inefficient as run script, so does not provide satisfactory hash lookups.
R23