• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: does property exists in a record
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: does property exists in a record


  • Subject: Re: does property exists in a record
  • From: email@hidden
  • Date: Fri, 10 Aug 2001 15:48:38 -0400

On Fri, 10 Aug 2001 23:40:23 +1000, From: Adam Hinshaw <email@hidden>

> im currently stumped with records.
>
> i want to be able to test that a property exists first before trying to set
> it.

There are two ways that come to mind.

1. Try to get the record, and catch the error that occurs if it doesn't exist.

2. Concatenate the record with one containing the property in question and a
default value, and check what you get in the result.

Here's an example of the first method:

try
p of x
on error -1728
missing value
end try
--> result: property p of record x if it exists, otherwise the
constant 'missing value'

The second method uses the fact that when you concatenate two records, the
result record favors the left record. For example,

set a to {x:1, y:1}
set b to {y:2, z:2}
a & b
--> result: {x:1, y:1, z:2}

So, to use that, do this

p of (x & {p:missing value})
--> result: property p of record x if it exists, otherwise the
constant 'missing value'

But in the big picture, I'd worry about an approach to a script in which you
don't know what the properties are. AppleScript's records are not like Perl
hashes or associative arrays like you find in Javascript. They are more like
Pascal records. Their property names are static, and can't be called out
indirectly using a string in a variable, except by using the most terribly
kludgy methods (essentially, compiling on the fly, and inducing errors so you
can get a text representation of the record). Given the choice of using those
kludges and using a scripting addition, I'll use the scripting addition every
time.

Specifically, I'll use Eric Grant's "Sigma's Coercions". You just say, "p as
user list" and you convert

{a:1, b:2, c:42}

to
{"a", 1, "b", 2, "c", 42}

To convert back, coerce the list to a "user record"

(This reminds me of APL's "shape" operators.)

But maybe the record is not the data representation you want. You may want a
list of name-value pairs, or separate lists, one of names and one of values.

{ {"a", 1}, {"b", 2}, {"c", 3} }
{ {name:"a", value:1}, {name:"b", value:2}, {name:"c", value:3} }
{"a", "b", "c"} , {1, 2, 3}

--
Scott Norton Phone: +1-703-299-1656
DTI Associates, Inc. Fax: +1-703-706-0476
2920 South Glebe Road Internet: email@hidden
Arlington, VA 22206-2768 or email@hidden


  • Follow-Ups:
    • Re: does property exists in a record
      • From: Adam Hinshaw <email@hidden>
  • Prev by Date: Re: Applescript newbie asking for advice and direction
  • Next by Date: Re: Quark Page Setup Question
  • Previous by thread: Re: does property exists in a record
  • Next by thread: Re: does property exists in a record
  • Index(es):
    • Date
    • Thread