Skip navigation

AppleScript- filtering a list of records by a property value possible?

1471 Views 5 Replies Latest reply: Apr 25, 2012 6:04 AM by Thomas Camilleri RSS
Thomas Camilleri Level 2 Level 2 (310 points)
Currently Being Moderated
Apr 24, 2012 5:13 AM

How can a list of records like the one below (except each list has 82 items) be filtered by a field's value so that the resulting list contains only records with a given property value?

 

e.g.

 

set recordList to (*input follows*) {{fnum:1, fname:"Admin", fval:"", isCustom:true},{fnum:8, fname:"Cataloged", fval:"4/18/12 4:04 PM", isCustom:false}}

 

I tried:

 

items of recordList where isCustom is true

 

but instead of the desired result( below) I got "variable isCustom not defined."

 

{{fnum:1, fname:"Admin", fval:"", isCustom:true}} --desired result

 

Is there a better way to do this than loop through the list and build a new list by evaluating the labeled pair of each record in the list?

 

Thanks in advance for any insights.

  • Pierre L. Level 4 Level 4 (3,860 points)

    Here's what the AppleScript Language Guide (page 206) says about filtering:

     

    Capture d’écran 2012-04-24 à 09.49.45.png

     

    Message was edited by: Pierre L. (page 206, not 166)

  • Pierre L. Level 4 Level 4 (3,860 points)

    My pleasure.

  • Hiroto Level 5 Level 5 (4,815 points)

    Hello Thomas Camilleri,

     

    Short answer is no.

    Filter reference form is only applicable to application objects and processed by the application.

     

    Long answer is that you can define some handlers to make code concise though you cannot avoid iteration through the list. After all, even the filter reference form invokes iteration behind the scene, so the ultimate question here would be how well we can hide the iteration from the scene.

     

    E.g., something like the codes below. Inevitable iteration is hidden in general-purpose handlers map() or match() and you may only define the filter1() or pattern1() handlers for your specific purpose. SCRIPT 2 would be better fit for your object.

     

    Regards,

    H

     

    -- SCRIPT 1

    set rr to {{key1:10, key2:true}, {key1:14, key2:false}, {key1:16, key2:true}}

    set rr1 to map(filter1, rr)'s records

    return rr1 -- {{key1:16, key2:true}}

     

    on filter1(x)

        if x's key2 and x's key1 > 11 then return x

        return missing value

    end filter1

     

    on map(func, aa)

        script o

            property xx : aa's contents

            property yy : {}

            property f : func

            repeat with x in my xx

                set end of my yy to my f(x's contents)

            end repeat

            return my yy's contents

        end script

        tell o to run

    end map

    -- END OF SCRIPT 1

     

     

    -- SCRIPT 2

    set rr to {{key1:10, key2:true}, {key1:14, key2:false}, {key1:16, key2:true}}

    set rr2 to match(pattern1, rr)

    return rr2 -- {{key1:16, key2:true}}

     

    on pattern1(x)

        x's key2 and x's key1 > 11

    end pattern1

     

    on match(pat, aa)

        script o

            property xx : aa's contents

            property yy : {}

            property f : pat

            repeat with x in my xx

                set x to x's contents

                if f(x) then set end of my yy to x

            end repeat

            return my yy's contents

        end script

        tell o to run

    end match

    -- END OF SCRIPT 2

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.