Text location within a AppleScript List

From the Mac Automation Scripting Guide, listing 21-20 (https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/ManipulateListsofItems.html#//apple_ref/doc/uid/TP40016239-CH48-SW1) is an example handler to find the location of a sting 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

If I use this Handler in the following test AppleScript (Note: I have multiple "log" calls to identify various variables and their class. See results):

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 -- "is" or "is equal to" or "=" are all equivalent
	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

Results in the following:

tell application "Script Editor"
	choose from list {"Time Machine NAS", "Share Drives NAS"} with prompt "Choose NAS drive/s to wake up" with multiple selections allowed
		--> {"Time Machine NAS"}
end tell

(Pass # 1)

(text, - the class of selectedNAS-, Time Machine NAS)

(text, - theItem-, Time Machine NAS)

(0, - the item position in the list) -- I.E., no match found

However:

If I change the Handler to: on getPositionOfItemInList(theItem as text, theList)

or if item position of theList is equal to theItem as text then return position

or if item position of theList contains theItem then return position or if (offset of theItem in (item position of theList)) is equal to 1 then return position

Alternatively, if the call to the handler is changed to:

getPositionOfItemInList(selectedNAS as text, NAS_Names) or set selectedNAS to selectedNAS as text before the call to the handler

Then the Handler works fine and finds the text string in the list, see the example below:

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 as text 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 selectedNAS to selectedNAS as text
	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

Results in:

tell application "Script Editor"
	choose from list {"Time Machine NAS", "Share Drives NAS"} with prompt "Choose NAS drive/s to wake up" with multiple selections allowed
		--> {"Time Machine NAS"}
end tell

(Pass # 1)

(text, - the class of selectedNAS-, Time Machine NAS)

(text, - theItem-, Time Machine NAS)

(1, - the item position in the list) -- I.E., match found

My question is, why is it necessary to coerce what appears to be a text string to text? The result from "choose from list" is a list. However, the result of the "repeat with -- in" appears to be a text string, not a list according to the log output of its class. So why, for this handler to work correctly, does the result from the "repeat with" have to be coerced to text with the "as text"?

Is this just a strange quirk of AppleScript? Is the "repeat with -- in" not extracting a text string but something else that is not identified by the "class of" command, hence needing to be coerced?

Even though I have this handler working, I would love to know the answer. Thanks in advance.

An update on this post. I got a response from another website [https://www.macscripter.net/t/index-of-an-item-in-an-applescript-list/74205). The results from the "repeat with -- in" is a reference to the item not the actual item. Hence, the code should be getPositionOfItemInList(contents of selectedNAS, NAS_Names)

"repeat with x in xList..." returns a reference of array. So, some command accept a reference and other command does not accept it.

We have to change the reference into real data.

"as string" would be a good way of writing to abbreviate the explanation. However, it may be a way of writing that gives a sense of incongruity to those who emphasize principles.

We have to convert or access reference by using "contents of".

set aList to {"1", "2", "3", "4", "5"}

repeat with i in aList
	set j to contents of i
	say j
end repeat

You can find such a description in AppleScript example blogs...

http://piyocast.com/as/search_gcse?q=contents%20of

Text location within a AppleScript List
 
 
Q