List of lists from text in file

Hi,

I have some text that’s been formatted with a shell script in this form:
{{“a”, “b”, “c”}, {“d”, “e”, “f”}}

I want to make it into a list of lists (an array) but it seems applescript won’t do that. Is there any way to make a string like this into a 2 dimensional list?

Thanks

Hi,

Maybe this code does the trick?


on run
	try
		-- we are converting the output from the shell into an AppleScript property
		set shellstr to "{{\"a\", \"b\", \"c\"}, {\"d\", \"e\", \"f\"}}"
		set ascode to "property mylist : " & shellstr
		-- we are writing this code into an AppleScript file
		set scriptfilepath to my gettmpfilepath("scpt")
		set qtdscriptfilepath to quoted form of POSIX path of scriptfilepath
		do shell script "osacompile -e " & quoted form of ascode & " -o " & qtdscriptfilepath
		-- we are loading the saved AppleScript into a script object...
		set scriptobj to load script (scriptfilepath as alias)
		-- ...and retrieve the value 
		set mylist to scriptobj's mylist
		choose from list (item 1 of mylist)
		choose from list (item 2 of mylist)
		-- finally we delete the temporary script file
		try
			do shell script "rm " & qtdscriptfilepath
		end try
	on error errmsg number errnum
		tell me
			display dialog "Sorry, an error occurred:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon stop
		end tell
	end try
end run

-- I am returning a path to an unused temporary file
on gettmpfilepath(suffix)
	set tmpfolderpath to (path to temporary items folder from user domain) as text
	repeat
		set randnum to random number from 10000 to 99999
		set tmpfilepath to tmpfolderpath & randnum & "." & suffix
		try
			set tmpfilealias to tmpfilepath as alias
		on error
			exit repeat
		end try
	end repeat
	return tmpfilepath
end gettmpfilepath

Hi.

Is this what you mean?

set shellResult to "{{\"a\", \"b\", \"c\"}, {\"d\", \"e\", \"f\"}}"
set theList to (run script shellResult)

Okay, I guess Nigel’s solution is slightly more efficient :smiley:

That’s awesome Nigel! I would never have figured that out on my own.

Many Thanks!