• 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: Position of an element in a list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Position of an element in a list


  • Subject: Re: Position of an element in a list
  • From: "Nigel Garvey" <email@hidden>
  • Date: Wed, 2 Aug 2017 23:45:09 +0100

Jim Underwood wrote on Wed, 2 Aug 2017 20:38:26 +0000:

>I really like the ASObjC version.  The only limitations seem to be:
>
>  1.  Requires 10.11+
>  2.  Is Case Sensitive (unlike normal AppleScript)

It can be made case-insensitive with a bit more complexity, but the
version here seems a bit perverse. If the search value's text and
case-insensitivity is specified, the script uses a predicate to find the
first item in the list which matches the value case-insensitively and
then uses indexOfObject to get the index of _that_.  :)  I don't know if
it's better than simply looping through the list or not. It may be good
with long lists. It could also be expanded to handle diacritical
insensitivity.

  use AppleScript version "2.5" # requires El Capitan or higher
  use framework "Foundation"
  use scripting additions

  on indexOf:aValue inList:theList withCaseInsensitivity:caseInsensitive
    local |?|, theArray, theIndex # useful for Script Debugger

    set |?| to current application
    set theArray to |?|'s NSArray's arrayWithArray:theList
    set aValue to (|?|'s NSArray's arrayWithObject:(aValue))'s firstObject()
    if ((caseInsensitive) and (aValue's isKindOfClass:(|?|'s NSString))) then
      set caseInsensitivePredicate to |?|'s NSPredicate's
predicateWithFormat:("self =[c] %@") argumentArray:({aValue})
      set insensitiveMatches to theArray's
filteredArrayUsingPredicate:(caseInsensitivePredicate)
      if (insensitiveMatches's |count|() > 0) then set aValue to
insensitiveMatches's firstObject()
    end if

    set theIndex to theArray's indexOfObject:aValue
    if theIndex = |?|'s NSNotFound then
      return 0
    else
      return (theIndex + 1) # +1 because ASObjC counts starting from 0
    end if
  end indexOf:inList:withCaseInsensitivity:

  set theList to {5, 2, 6, 9, 4, "TTT", 6, "TT", 3, weekday of (current date)}
  its indexOf:"ttt" inList:theList withCaseInsensitivity:true


NG

 _______________________________________________
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: Position of an element in a list
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Re: Position of an element in a list
  • Next by Date: Re: Position of an element in a list
  • Previous by thread: Re: Position of an element in a list
  • Next by thread: Re: Position of an element in a list
  • Index(es):
    • Date
    • Thread