Re: Position of an element in a list
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