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: Thu, 3 Aug 2017 00:13:06 +0000
- Thread-topic: Position of an element in a list
Shane, I must be doing something wrong.
When I change the script to use your below statement, I get this error:
________________________________
"two" doesn’t understand the “lowercaseString” message.
________________________________
Running Script Debugger 6.0.5 (6A205) on macOS 10.11.6.
The modified script:
________________________________
use AppleScript version "2.5" # requires El Capitan or higher
use framework "Foundation"
use scripting additions
on indexOf:aValue inList:theList listType:listType
local |⌘|, theArray, theIndex # useful for Script Debugger
set |⌘| to current application
set theArray to |⌘|'s NSArray's arrayWithArray:theList
if (listType = "string") then
(*
PER SHANE, if the entire list is text/strings, then you can use this
instead
to make it Case Insensitive
*)
set theIndex to (theArray's valueForKey:"lowercaseString")'s
indexOfObject:(aValue's lowercaseString())
else
--- VALID for ALL Data Types, but is CASE SENSITIVE for Text ---
set theIndex to theArray's indexOfObject:aValue
end if
if theIndex = |⌘|'s NSNotFound then
return 0
else
return (theIndex + 1) # +1 because ASObjC counts starting from 0
end if
end indexOf:inList:listType:
set allStrList to {"one", "TWO", "Three"}
log (its indexOf:"two" inList:allStrList listType:"string")
________________________________
Best Regards,
Jim Underwood
aka JMichaelTX
From: AppleScript-Users
<applescript-users-bounces+jmichael=email@hidden<mailto:applescript-users-bounces+jmichael=email@hidden>>
on behalf of Shane Stanley
<email@hidden<mailto:email@hidden>>
Date: Wednesday, August 2, 2017 at 6:35 PM
To: "ASUL (AppleScript)"
<email@hidden<mailto:email@hidden>>
Subject: Re: Position of an element in a list
On 3 Aug 2017, at 8:45 am, Nigel Garvey
<email@hidden<mailto:email@hidden>> wrote:
the version here seems a bit perverse
But in a very attractive way :-)
It could be good for long lists, although in practice they tend to consist of
items of the same class, in which case you can also use:
set theIndex to (theArray's valueForKey:"lowercaseString")'s
indexOfObject:(aValue's lowercaseString())
Obviously that fails if the list items aren't all strings.
--
Shane Stanley <email@hidden<mailto:email@hidden>>
<www.macosxautomation.com/applescript/apps/<http://www.macosxautomation.com/applescript/apps/>>,
<latenightsw.com<http://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