• 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: Jim Underwood <email@hidden>
  • Date: Thu, 3 Aug 2017 00:33:17 +0000
  • Thread-topic: Position of an element in a list

OK, thanks for the update, Shane.

Here's the revised script with an option for Lists that are ALL strings.
It works fine now.

________________________________

(*

From: Yvan KOENIG

Date: Tuesday, August 1, 2017 at 8:52 AM

To: "ASUL (AppleScript)" <email@hidden>

Subject: Re: Position of an element in a list


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


## MOD BY JMichaelTX


The only limitations seem to be:

- Requires 10.11+

- Is Case Sensitive for mixed type Lists (unlike normal AppleScript)

- But is NOT Case Sensitive for a ALL Strings List

    -- Added Code from Shane with IF test to handle this case


*)

use AppleScript version "2.5" # requires El Capitan or higher  ## MOD by
JMichaelTX

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



  ### MOD By JMichaelTX



  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 aValue to current application's NSString's stringWithString:aValue

    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:


--~~~~~~~~~~~~~~~~~~~~~~~~~~~ END OF Handlers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


--- TEST CASE INSENSITIVE ---

set allStrList to {"one", "TWO", "Three"}

log (its indexOf:"two" inList:allStrList listType:"string")

-->2


--- TEST ALL DATA TYPES ---

set theList to {5, 2, 6, 9, 4, "TTT", 6, current date, 3}

its indexOf:6 inList:theList listType:"mixed"

log result --> 3


its indexOf:"4" inList:theList listType:"mixed"

log result --> 0


its indexOf:"TTT" inList:theList listType:"mixed"

log result --> 6


its indexOf:(current date) inList:theList listType:"mixed"

log result --> 8


________________________________


Best Regards,

Jim Underwood
aka JMichaelTX


From: Shane Stanley
<email@hidden<mailto:email@hidden>>
Date: Wednesday, August 2, 2017 at 7:17 PM
To: "ASUL (AppleScript)"
<email@hidden<mailto:email@hidden>>
Subject: Re: Position of an element in a list

On 3 Aug 2017, at 10:13 am, Jim Underwood
<email@hidden<mailto:email@hidden>> wrote:

When I change the script to use your below statement, I get this error:
________________________________
"two" doesn’t understand the “lowercaseString” message.

I left out the full code. You would also have to convert aValue to an NSString
first:

setaValue to current application'sNSString's stringWithString:aValue


 _______________________________________________
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: Shane Stanley <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>)

  • Prev by Date: Re: Position of an element in a list
  • Next by Date: 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