Arrays AppleScript将字符串变量转换为列表

Arrays AppleScript将字符串变量转换为列表,arrays,string,list,applescript,Arrays,String,List,Applescript,我试图在AppleScript中执行一个shell脚本,其中我grep了一个CSV文件,它会将一个列表返回到AppleScript 例如,我的CSV有行 01,{"tacos","burritos"} 02,{"burgers","hot dogs", "corn dogs"} 我的AppleScript设置为 set a to 01 set b to 02 set myMenu to do shell script "grep " & a & " [path to C

我试图在AppleScript中执行一个shell脚本,其中我grep了一个CSV文件,它会将一个列表返回到AppleScript

例如,我的CSV有行

01,{"tacos","burritos"}
02,{"burgers","hot dogs", "corn dogs"}
我的AppleScript设置为

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
执行此操作时,我的“选择自”列表显示为列表中的一个项目,该列表将菜单项显示为字符串,但我希望它们是单独的菜单项

意思是我的选择列表显示为:

{"tacos","burritos"}
而不是:

tacos
burritos

我认为下面应该让您了解如何从shell脚本的输出中获取多个项来填充Applescript列表。基本上,假设您的shell脚本执行以下操作:

echo item1,item2,item3
您可以在列表中提供这3项供用户选择,如下所示:

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
01,tacos,burritos
02,burgers,hot dogs,corn dogs
然后,您可以使用:

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

osascript您可以只使用AppleScript,但读取文件或使用shell脚本的结果将是文本,因此您需要将其转换为列表。使用您的示例最简单的方法是运行带有结果的脚本,例如,
将myMenu设置为运行脚本myMenu
,但最好的方法是重新构造文件,这样您就可以在不使用列表语法的情况下获取项目。同意:如果可以,请从CSV切换到XML或JSON。两者都可以描述复杂的嵌套数据,并且很容易使用现有的AS工具进行解析。