item list missing something [Text items]

There’s something basic I’m just not getting (partial script follows):

set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to (",")

set the_fields to "site_code,orderno,orderdate,transdate,schddate"

display dialog ¬
	return & the number of items of the_fields & ¬
	return & the number of words of the_fields buttons {"cancel"} default button ¬
	"cancel" with icon stop

-- some stuff

set AppleScript's text item delimiters to old_delimiters

number of items returns 7
number of words returns 46

How should I refer to the_fields so that each “thing” is one element of the_fields?
There are 5 “things.” The first is “site_code”, the third is “orderdate”.

Thank you.

If you are actually setting the value of the_fields, you need to use a list instead of a string.

set the_fields to {"site_code", "orderno", "orderdate", "transdate", "schddate"}

display dialog return & the number of items of the_fields

repeat with item_ in the_fields
	display dialog item_ giving up after 2
end repeat

If you have no control of the_fields, the above code won’t help.

– Rob

Also, you want “number of text items”…

  • Dan

Also, if you dismiss the dialog with a ‘cancel’ button, the script will immediately stop at that point, so ~~~some stuff ~~~ and the line that restores the old delimiters won’t be executed.

So… just to recap, then:

set the_fields to "site_code,orderno,orderdate,transdate,schddate"
set old_delimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {","}
set field_items to text items of the_fields
set AppleScript's text item delimiters to old_delimiters
---- some other pre-dialog stuff? ----
display dialog (count field_items)
--> (dialog) --> 5
---- some more stuff (if user didn't cancel) ----