How to define a new class to use 'where' filters

It seems that using ‘where’ or ‘whose’ to filter or search a list is much more efficient than manually searching with a repeat command, but these searches only seem to work with classes:

tell app "Address Book"
   get every person whose name is "John Doe"
end tell

works fine since person is a class, but the following doesn’t work:

set alist to {color:"red", itsname:"johnny"}
copy  {color:"orange", itsname:"freddy"} to end of alist
set mything to first item in (get every alist where myname is "freddy"}

doesn’t compile because it says it expects a class where “alist” is in the last line. So if we try to make a class like this:

set alist to {class:things, itsname:"johnny"}
copy  {class:things, itsname:"freddy"} to end of alist
set mything to first item in (get every things where myname is "freddy"}

it still doesn’t work , with the same error (“things” is an identifier, not a class, according to the compiler). Even if we use the default list class type ‘record’, it doesn’t work:

set alist to {color:"red", itsname:"johnny"}
copy  {color:"orange", itsname:"freddy"} to end of alist
set mything to first item in (get every record where myname is "freddy"}

this time cause it says it expects a “,” where the “}” is in the last line!?

I’ve even tried making a class using the “script myclass” command but that doesn’t allow filters to be applied on it either.

Any ideas how to create a valid new class that can have filters applied to it? Do you have to define a filter method somehow?

thanks in advance!

It’s not that a ‘person’ is a ‘class’ — items, lists, and records are ‘classes’ too — but that the application “Address Book” implements ‘whose/where’ filters, and ‘person’ is one of the “Address Book” classes to which they can be applied. These filters aren’t implemented in the basic AppleScript language, so you can’t use them on AppleScript classes. You have to use a repeat.

The other reason your code examples don’t work is that {color:“red”, itsname:“johnny”} isn’t a list but a record. You can’t copy things to the end of it. Records can be concatenated, but I think your intention is to build a list of records:

set alist to {{color:"red", itsname:"johnny"}} -- a list containing a record
set end of alist to {color:"orange", itsname:"freddy"}

repeat with thisRecord in alist
  if thisRecord's itsname is "freddy" then
    set mything to thisRecord's contents
    exit repeat
  end if
end repeat