1

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
2
  • You can just use AppleScript, but the result from reading a file or using a shell script will be text, so you will need to convert it to a list. The easiest way with your example would be to run a script with the result, e.g. set myMenu to run script myMenu, but the best way would be to restructure your file so you can just get the items without using the list syntax. Commented Oct 19, 2019 at 2:00
  • Agreed: switch from CSV to XML or JSON if you can. Both can describe complex nested data, and are easy to parse using existing AS tools. Commented Oct 19, 2019 at 10:44

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.