• 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: Shane Stanley <email@hidden>
  • Date: Thu, 3 Aug 2017 14:10:51 +1000

On 3 Aug 2017, at 10:33 am, Jim Underwood <email@hidden> wrote:
>
> Here's the revised script with an option for Lists that are ALL strings.
> It works fine now.

Having suggested that approach, I'm now going to suggest it's not a great idea.
For small lists it's faster than the extra work in Nigel's script, but it
disappears as the list size increases. I don't think there's enough gain to
warrant the restriction of strings only, when Nigel's version doesn't have that
limitation.

I suspect a better approach is to check the number of items in the list. If
it's below a certain threshold, then use the plain AppleScript, otherwise use
Nigel's approach, something like this:

use AppleScript version "2.5"
use framework "Foundation"
use scripting additions

on indexOf:aValue inList:theList
        set theLimit to 100 -- arbitrary limit
        set listCount to count of theList
        if listCount < theLimit then
                repeat with i from 1 to listCount
                        if item i of theList = aValue then
                                return i
                        end if
                end repeat
                return 0 -- not found
        else
                set theArray to current application's NSArray's
arrayWithArray:theList
                if class of aValue is text and "a" = "A" then
                        -- only need to do this for text and ignoring case
                        set caseInsensitivePredicate to current application's
NSPredicate's predicateWithFormat:("self =[c] %@") argumentArray:({aValue})
                        set insensitiveMatches to theArray's
filteredArrayUsingPredicate:caseInsensitivePredicate
                        if (insensitiveMatches's |count|() = 0) then return 0
-- not found
                        set aValue to insensitiveMatches's firstObject()
                end if
                set theIndex to theArray's indexOfObject:aValue
        end if
        if theIndex = current application's NSNotFound then
                return 0
        else
                return (theIndex + 1)
        end if
end indexOf:inList:



--
Shane Stanley <email@hidden>
<www.macosxautomation.com/applescript/apps/>, <latenightsw.com>


 _______________________________________________
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: Jim Underwood <email@hidden>
References: 
 >Re: Position of an element in a list (From: "Nigel Garvey" <email@hidden>)
 >Re: Position of an element in a list (From: Shane Stanley <email@hidden>)
 >Re: Position of an element in a list (From: Jim Underwood <email@hidden>)
 >Re: Position of an element in a list (From: Shane Stanley <email@hidden>)
 >Re: Position of an element in a list (From: Jim Underwood <email@hidden>)

  • Prev by Date: Do Shell Script returns empty string but works in Terminal
  • Next by Date: Re: Do Shell Script returns empty string but works in Terminal
  • 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