The “Mac Automation Scripting Guide” (Mac Automation Scripting Guide: Manipulating Lists of Items)
Listing 21-20 has an example handler for locating the position of a text string in an AppleScript list:
on getPositionOfItemInList(theItem, theList)
repeat with a from 1 to count of theList
if item a of theList is theItem then return a
end repeat
return 0
end getPositionOfItemInList
Using this handler in a test AppleScript listed below does not work:
on getPositionOfItemInList(theItem, theList)
log (the class of theItem) & "- theItem-" & theItem
repeat with position from 1 to count of theList
if item position of theList is equal to theItem then return position
end repeat
return 0
end getPositionOfItemInList
set NAS_Names to {"Time Machine NAS", "Share Drives NAS"} -- Initialise the Disk names
set selectedNAS_List to choose from list NAS_Names with prompt "Choose NAS drive/s to wake up" with multiple selections allowed
set pass to 0
repeat with selectedNAS in selectedNAS_List
set pass to pass + 1
log "Pass # " & pass
log (the class of selectedNAS) & "- the class of selectedNAS-" & selectedNAS
log getPositionOfItemInList(selectedNAS, NAS_Names) & "- the item position in the list"
end repeat
Resulting in the following output from the logs:
(*Pass # 1*)
(*text, - the class of selectedNAS-, Time Machine NAS*)
(*text, - theItem-, Time Machine NAS*)
(*0, - the item position in the list*) <== NOTE the item is not found
However the following does work:
on getPositionOfItemInList(theItem as text, theList)
log (the class of theItem) & "- theItem-" & theItem
repeat with position from 1 to count of theList
if item position of theList is equal to theItem then return position
end repeat
return 0
end getPositionOfItemInList
set NAS_Names to {"Time Machine NAS", "Share Drives NAS"} -- Initialise the Disk names
set selectedNAS_List to choose from list NAS_Names with prompt "Choose NAS drive/s to wake up" with multiple selections allowed
set pass to 0
repeat with selectedNAS in selectedNAS_List
set pass to pass + 1
log "Pass # " & pass
log (the class of selectedNAS) & "- the class of selectedNAS-" & selectedNAS
log getPositionOfItemInList(selectedNAS, NAS_Names) & "- the item position in the list"
end repeat
With the resulting log output:
(*Pass # 1*)
(*text, - the class of selectedNAS-, Time Machine NAS*)
(*text, - theItem-, Time Machine NAS*)
(*1, - the item position in the list*) <== NOTE the item is now found!
Also:
If the handler is “on getPositionOfItemInList(theItem, theList)” and the line in the repeat is
“on getPositionOfItemInList(theItem as text, theList)” or “if theItem contains item position of theList then return position” or “if (offset of theItem in (item position of theList)) is equal to 1 then return position”
the item in the list is found.
If the Handler is as given in the original (i.e., no “as text” etc) and in the calling script the call to the handler is “getPositionOfItemInList(selectedNAS as text, NAS_Names)” or “set selectedNAS to selectedNAS as text” is done before the call to the handler, the script works just fine.
My question is why is, what appears to be a Class of Text having to be coerced to a Class of Text for this handler to work? The “repeat with – in” appears to be extracting a Text String from the AppleScript list, so for this example to work it has to be coerced again to Text before being passed to the Handler or coerced inside the handler.
I have got this Handler to do what I’m after, but I would love to know why the need for coercion.