Add sequential numbering to a list of items

Let’s assume I have a list of items associated to a variable “my_list”. Inside that list are returns used as separators.

So today “my_list” would return something like:

dog

cat

mouse

But what I want it to do now is output the following:

  1. dog

  2. cat

  3. mouse

In other words, I want to recompile “my_list” so that it adds sequential numbering to its list items. There must be a simple way to repeat through the list, and add a number + 1, but I am not having much success. So in the meantime, I figured I would post the question. And I have searched, but nothing seemed to point to this particular request.

Any help would be greatly appreciated.

Thanks,
Jeff

Jeff -


set my_list to {"dog", "cat", "mouse"}
repeat with i from 1 to count of my_list
	set ReplaceTxt to i & ". " & item i of my_list as string
	set item i of my_list to ReplaceTxt
end repeat
return my_list
...{"1. dog", "2. cat", "3. mouse"}

…Mick

Thank you Mick536,
It’s very close, but if I were to display that returned list as a dialog it would yield the following:

dog
2.
.
cat
3
.
mouse

Not sure why?

Hi Mick.

Because the value of i is an integer, concatenating the other stuff to it produces a list, which you then coerce to text. When coercing lists to text, you have to be careful about the current value of AppleScript’s text item delimiters. They might not be at their default setting of {“”}:

set AppleScript's text item delimiters to "¢"

set my_list to {"dog", "cat", "mouse"}
repeat with i from 1 to count of my_list
	set ReplaceTxt to i & ". " & item i of my_list as string
	set item i of my_list to ReplaceTxt
end repeat
return my_list
--> {"1¢. ¢dog", "2¢. ¢cat", "3¢. ¢mouse"}

Here, it would be better to coerce i to string first, so that the rest is cocatenated to it as text:

set AppleScript's text item delimiters to "¢"

set my_list to {"dog", "cat", "mouse"}
repeat with i from 1 to count of my_list
	set ReplaceTxt to (i as string) & ". " & item i of my_list
	set item i of my_list to ReplaceTxt
end repeat
return my_list
--> {"1. dog", "2. cat", "3. mouse"}

Nigel,
Thank you very much as well. that was the essential piece of the puzzle I was looking for. Excellent!