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: Jim Underwood <email@hidden>
- Date: Wed, 2 Aug 2017 20:38:26 +0000
- Thread-topic: Position of an element in a list
I really like the ASObjC version. The only limitations seem to be:
1. Requires 10.11+
2. Is Case Sensitive (unlike normal AppleScript)
I've been testing the below script/handler, and it seems to work in all cases.
So far, I have not found any limitations.
If you know of any limitations for this script, or why the ASObjC version would
be better, please post.
________________________________
--- SIMPLE VERSION ---
set myDateStr to "Aug 2, 2017"
set myDate to date myDateStr
set myDate2Str to "Sep 1, 2017"
set myDate2 to date myDate2Str
# Index: 1 2 3 4 5 6 7 8 9
10
set myList to {"TTT", 1, 67, "TT", 8, myDate, "Aug 1, 2017", {1, 2}, {"1",
"2"}, "67"}
set idxText to my getIndex("TT", myList)
-->4
set idxNumAsText to my getIndex("67", myList)
-->10
set idxNumAsTextNone to my getIndex("68", myList)
-->0
set idxNum to my getIndex(67, myList)
-->3
set idxDate to my getIndex(myDate, myList)
-->6
set idxDateNone to my getIndex(myDate2, myList)
-->0
set idxListNum to my getIndex({1, 2}, myList)
-->8
set idxListText to my getIndex({"1", "2"}, myList)
-->9
set idxListNumNone to my getIndex({1, "2"}, myList)
-->0
set idxListTextNone to my getIndex({"1", "1"}, myList)
-->0
if (idxText > 0) then
--- do something with idx
else
--- throw error or notify user or other action
end if
--~~~~~~~~~~~~~~ END OF MAIN SCRIPT ~~~~~~~~~~~~~~
on getIndex(pItem, pList)
local idx, i
set idx to 0
set lenList to length of pList
repeat with i from 1 to lenList
if (pItem = (contents of item i of pList)) then
set idx to i
exit repeat
end if
end repeat
return idx
end getIndex
________________________________
RESULTS:
[cid:74355E01-6E14-4249-8064-86F4650A026C]
Best Regards,
Jim Underwood
aka JMichaelTX
From: Yvan KOENIG <email@hidden<mailto:email@hidden>>
Date: Tuesday, August 1, 2017 at 8:52 AM
To: "ASUL (AppleScript)"
<email@hidden<mailto:email@hidden>>
Subject: Re: Position of an element in a list
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