How can I convert a String to List?

So I need to covert a string captured in the clipboard from BBEdit and convert it to a list. The string will vary in what is contained and what the length is…

Here’s an example.

{“19”, “30”, “2”, “463”, “26”, “478”, “23”, “46”, “468”, “122”, “112”, “185”, “186”, “128”, “132”, “145”, “152”, “406”, “412”, “419”, “423”, “426”, “174”, “13”, “66”, “70”, “74”, “83”, “92”, “96”, “100”, “222”, “461”, “107”, “110”, “230”, “191”, “198”, “15”, “12”, “14”, “471”, “18”, “199”, “212”}

My only other choice is to do a repeat from i to 500, to cover any additions, by using a list I can cut the script from it current 20 minutes, to 5 (hopefully) :D.

Thanks

Depending on how you want to separate the items of your list in the string, set the “Applescript text item delimiters” to that character and coerce the list to a string.
Like this:

set my_list to {"A", "B", "C"}
set Applescript's text item delimiters to ", " -- that's "comma space"
set list_2_string to my_list as text  -- "A, B, C"

The Applescript text item delimters are an extremely powerful feature in Applescript. If you want to substitute every “{” in a block of text with “{” you can use the text item delimiters

set this_string to "[A-Z]{1,2}"
set Applescript's text item delimiters to "{"
set text_list to text items of this_string  --{"[A-Z]", "1,2}"}
set Applescript's text item delimiters to "\{"
set this_string to text_list as text --"[A-Z]\{1,2}"

I gave you bad advice. I was in a hurry and should have said to always save the current Applescript text item delimiters and then restore them in the event of an error or when task is complete.

set old_delimts to  Applescript's text item delimiters
set this_string to "[A-Z]{1,2}" 
try
    set Applescript's text item delimiters to "{" 
    set text_list to text items of this_string  --{"[A-Z]", "1,2}"} 
    set Applescript's text item delimiters to "\{" 
    set this_string to text_list as text --"[A-Z]\{1,2}" 
on error errMess number errNum
    set Applescript's text item delimiters to old_delimts
end
set Applescript's text item delimiters to old_delimts


I opted for a slight variation, but it works great.
Seeing as we can’t coerce a string to a multi-item list this works quite well with minimal lines of code. So for the following string to list.

19, 30, 2, 463, 26, 478, 23, 46, 468, 122, 112, 185, 186, 128, 132, 145, 152, 406, 412, 419, 423, 426, 174, 13, 66, 70, 74, 83, 92, 96, 100, 222, 461, 107, 110, 230, 191, 198, 15, 12, 14, 471, 18, 199, 212

20 (sometimes 30) minutes runtime to 2 minutes…
:lol:


set new_array to (get the clipboard) as string
	
set AppleScript's text item delimiters to ", "
set the list_items to every text item of the new_array
set AppleScript's text item delimiters to ""
set the max_x to the count of the list_items

Thanks
Ron

Hi,

If there are just just positive numbers in the string, you can use words:

set the_string to “19, 30, 2, 463, 26, 478, 23, 46, 468, 122, 112, 185, 186, 128, 132, 145, 152, 406, 412, 419, 423, 426, 174, 13, 66, 70, 74, 83, 92, 96, 100, 222, 461, 107, 110, 230, 191, 198, 15, 12, 14, 471, 18, 199, 212”
return words of the_string

gl,