Er Ar
Er Ar

Reputation: 11

AppleScript convert variable that is a string into a list

I am attempting to do a shell script within my AppleScript where I grep a CSV file and it returns a list to my AppleScript.

For example, my CSV has lines

01,{"tacos","burritos"}
02,{"burgers","hot dogs", "corn dogs"}

my AppleScript is set as

set a to 01
set b to 02    
set myMenu to do shell script "grep " & a & " [path to CSV] | cut -d ',' -f 2")
    set menuChoice to (choose from list myMenu)
    display alert menuChoice

When I do this, my choose from list displays as one item in the list that shows the menu items as a string, but I want them to be individual menu items.

Meaning my choose from list shows as:

{"tacos","burritos"}

Instead of:

tacos
burritos

Upvotes: 1

Views: 2018

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 208003

I think the following should give you an idea of how to grab multiple items from the output of a shell script to populate an Applescript list. Basically, say your shell script does this:

echo item1,item2,item3

You can offer those 3 items in a list for the user to choose from like this:

osascript <<EOF
   set AppleScript's text item delimiters to ","
   set theList to every text item of (do shell script "echo item1,item2,item3")
   set menuChoice to (choose from list theList)
EOF

which will give you this:

enter image description here


Now, more similar to your question, if you make your CSV look like this:

01,tacos,burritos
02,burgers,hot dogs,corn dogs

You can then use:

osascript <<EOF
set AppleScript's text item delimiters to ","
set theList to every text item of (do shell script "grep '^01,' my.csv | cut -d, -f 2-")
set menuChoice to (choose from list theList)
EOF

to get:

enter image description here

Upvotes: 1

Related Questions