Delimited String to Array

I can find a hundred examples of reading a delimited file and bring it into an array, but how do I get delimited data out of a string?

Basically, my app is yanking data from a web server to populate a table. I can spit this data out however the heck I need too, but no matter what I do, I can’t seem to do what I want.

This seems to work just fine:


set content of table view 1 of scroll view "theTable" of window "main" to {{"abc","123","xyz"}{"abc","123","xyz"}{"abc","123","xyz"}{"abc","123","xyz"}}

This doesn’t:


set dataSource to do shell script "curl [url=http://mydomain.com/data/fetch.php]http://mydomain.com/data/fetch.php"[/url]
set content of table view 1 of scroll view "theTable" of window "main" to dataSource

In the last example, I have tried all various types of things, like giving the output of my php file to be “{{“abc”,“123”,“xyz”}{“abc”,“123”,“xyz”}{“abc”,“123”,“xyz”}{“abc”,“123”,“xyz”}}” or even “1,2,3/n4,5,6/n” and using some code to slice and dice that, but no luck.

Any help or advice!? Please… Newbie I am.

If you configured you php app to return this:

AppleScript receives this:

That is, a string, not a list
You can coerce it to list very easilly as follow:

set dataSource to do shell script "curl [url=http://mydomain.com/data/fetch.php]http://mydomain.com/data/fetch.php"[/url]
set dataSource to (run script dataSource) --> will hold now a list, if syntax is OK

Thank you, Thank you!

One last remaining issue… it says that dataSource is not a defined variable.

I assume it is in reference to this:


set content of table view 1 of scroll view "theTable" of window "main" to dataSource

I assume this is because dataSource is not a variable but an array. How do I get applescript to process dataSource as an array and not a variable to populate my table?

THanks.

Hmmm… Perhaps the “run script” thing is not working… What do you see in the run long if you “log dataSource” after the “run script” line?

IGNORE my previous post.

I figured out the error: ID10T Error

:smiley:

BTW - Thank you very much. I searched for HOURS yesterday to resolve this issue w/ no luck. Tried a bunch of different possibilities. Have a good day!

I have a problem which is along similar lines, the difference is that I’m trying to pull data out of a text file on my desktop (delimited by return) into a datatable, I can read the data fine but it seems to be seeing it as a string rather than a list and hence doesn’t go into my data table correctly. I’m sure there’s a simple solution but I’ve been unable to track it down and this post seemed to deal with a very similar problem.
Thanks in advance for any help.

You could try:

paragraphs of (read alias "path:to:file.txt")

And you will get a list.

Thanks for your help but I’m still having problems with that solution, I have a list like this:

“Wake.mov”,“MooV”,“27.3 MB”
“Base.swf”,"SWF ",“1.6 MB”
“cast.mov”,“MooV”,“1.3 MB”
etc…

Using paragraphs of (read alias “path:to:file.txt”), splits it nicely into the different rows of the table but all 3 fields end up in the first column, still in quotes. I’m guessing this is as it’s still seeing the information as a string rather than a list, but I’m not sure what I’ve done wrong.

So, you have a tab-delimited text file? (being “tab” the field separator and “return” the record separator). Eg:

a	1	x
b	2	y
c	3	z

Then, you may need something as:

set x to paragraphs of (read alias "path:to:file.txt")

set lst to {}

set AppleScript's text item delimiters to tab
repeat with i from 1 to count x
	set lst's end to x's item i's text items
end repeat
set AppleScript's text item delimiters to {""}

lst --> {{"a", "1", "x"}, {"b", "2", "y"}, {"c", "3", "z"}}

Awesome, thank you so much. Been driving me slightly nuts that one, but now works like a charm.