• 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
Best Method to Filter Lists?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Best Method to Filter Lists?


  • Subject: Best Method to Filter Lists?
  • From: Jim Underwood <email@hidden>
  • Date: Fri, 10 Jun 2016 02:52:11 +0000
  • Thread-topic: Best Method to Filter Lists?

Maybe I missed it (after much searching), but I haven't found a good, generic, handler to filter lists, using native AppleScript.

From List Manipulation Routines (MacOSXAutomation.com):

Working with lists is one of the most common procedures performed in scripts. However, the current version of AppleScript does not support the use of every...whose clauses when targeting lists. For example, the following statement will not work:


set the
 name_list to {"Bob", "Sal", "Wanda", "Sue"}
set every item of the name_list where it is "Bob" to "Carl"
--> Can't set {"Bob", "Sal", "Wanda", "Sue"} whose it = "Bob" to "Carl".



Here is the script I have come up with.  Can anyone suggest a better method?




(*

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

FILTERING LISTS


List Manipulation Routines

http://www.macosxautomation.com/applescript/sbrt/sbrt-07.html


Working with lists is one of the most common procedures performed in scripts. 

However, the current version of AppleScript does not support 

the use of every...whose clauses when targeting lists. 


For example, the following statement will not work:


### DOES NOT WORK ###


set the name_list to {"Bob", "Sal", "Wanda", "Sue"}

set every item of the name_list where it is "Bob" to "Carl"


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

*)


use AppleScriptversion "2.4" -- Yosemite (10.10) or later

use scripting additions


set myList to {"@Test", "other", "@end", "else"}


--text of every item in myList that begins with "@"   ### FAILS


set startsList to my filterList(myList, "@", "starts with")

set endsList to my filterList(myList, "end", "ends with")

set containsList to my filterList(myList, "the", "contains")


on filterList(pList, pMatchStr, pMatchType)

  

  ### Since lists don't support "whose" or "where" clauses,

  #   have to roll my own.

  

  local foundItems, oItem

  

  set foundItems to {}

  

  repeat with oItem in pList

    

    if (pMatchType = "starts with") then

      if (oItem starts with pMatchStr) then

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchType = "ends with") then

      if (oItem ends with pMatchStr) then

        set end of foundItems to text of oItem

      end if

      

    else if (pMatchType = "contains") then

      if (oItem contains pMatchStr) then

        set end of foundItems to text of oItem

      end if

      

    end if

  end repeat

  

  return foundItems

  

end filterList




 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Best Method to Filter Lists?
      • From: Christopher Stone <email@hidden>
    • Re: Best Method to Filter Lists?
      • From: Yvan KOENIG <email@hidden>
  • Prev by Date: Re: HTML parsing
  • Next by Date: Re: HTML parsing--need to be able to click on parsed elements
  • Previous by thread: Re: HTML parsing--need to be able to click on parsed elements
  • Next by thread: Re: Best Method to Filter Lists?
  • Index(es):
    • Date
    • Thread