• 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: Yvan KOENIG <email@hidden>
  • Date: Tue, 1 Aug 2017 15:52:15 +0200

Using answers which fail upon this or that particular case was useful in old
times.
Now that we may use ASObjC, it seems that it's time to drop these answers.

Here is a piece of code borrowed to Shane Stanley's Everyday AppleScriptObjC :

use AppleScript version "2.4" # requires Yosemite or higher
use framework "Foundation"
use scripting additions

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

        set |⌘| to current application
        set theArray to |⌘|'s NSArray's arrayWithArray:theList
        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:

set theList to {5, 2, 6, 9, 4, "TTT", 6, current date, 3}
its indexOf:6 inList:theList
log result --> 3

its indexOf:"4" inList:theList
log result --> 0

its indexOf:"TTT" inList:theList
log result --> 6

its indexOf:(current date) inList:theList
log result --> 8


Yvan KOENIG running Sierra 10.12.6 in French (VALLAURIS, France) mardi 1 août
2017 15:47:33





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