Bash 输出为 Applescript 列表问题
这简直让我发疯。我正在尝试在 bash 中读取文件,删除重复项,排序,然后通过 applescript 显示“列表选择”窗口。
我的 $DATALOG 文件的格式如下:
field1 field2
field1 field3
field1 field4
等...
Applescript=awk '{print $2}' $DATALOG | awk ' !x[$0]++' |排序 -u | tr“_”“”| sed 's/^/\"/' | sed 's/$/\"/' | tr“\n”“,”| sed 's/.$//'
现在,该行工作得很好。在 $Applescript 中,我得到如下输出:
“字段 2”、“字段 3”、“字段 4”
这正是我想要的。
现在,我获取该输出,并在引号和 applescript 部分之前添加反斜杠。
Applescript=`echo "tell application \"System Events\" to return (choose from list {$Applescript})"| sed 's/\"/\\\"/g'`
这正是我想要的:
告诉应用程序\“系统事件\”返回(从列表{\“字段2\”,\“字段3\”,\“字段4\”}中选择)
现在,我尝试 osascript 命令:
osascript -e $Applescript
我收到错误:
4:4:语法错误:需要表达式,但发现脚本结尾。 (-2741)
所以,我添加引号:
osascript -e“$Applescript”
我收到错误:
17:18:语法错误:预期的表达式、属性或密钥形式等,但发现未知的标记。 (-2741)
我不知道这里到底发生了什么,所以我决定复制 $Airport 的回声并尝试将其作为变量。
Airport=
告诉应用程序\“系统事件\”返回(从列表{\“字段2\”,\“字段3\”,\“字段4\”}中选择)
< /p>
并且无需任何修改。
所以......
我需要弄清楚如何做到这一点,而不必永久设置我的变量。
This is just driving me nuts. I am trying to read a file in bash, remove duplicates, sort, and then display a "list choice" window via applescript.
My $DATALOG file is formatted like this:
field1 field2
field1 field3
field1 field4
etc...
Applescript=awk '{print $2}' $DATALOG | awk ' !x[$0]++' | sort -u | tr "_" " "| sed 's/^/\"/' | sed 's/$/\"/' | tr "\n" "," | sed 's/.$//'
Now, that line works GREAT. in $Applescript, I get an output like this:
"field 2","field 3", "field 4"
Which is exactly waht I want.
Now, I take that output, and add the backslash before the quotes, and the applescript parts.
Applescript=`echo "tell application \"System Events\" to return (choose from list {$Applescript})"| sed 's/\"/\\\"/g'`
And this gets me exactly what I want:
tell application \"System Events\" to return (choose from list {\"field 2\",\"field 3\",\"field 4\"})
Now, I try the osascript command:
osascript -e $Applescript
And I get an error:
4:4: syntax error: Expected expression but found end of script. (-2741)
So, I add quotes:
osascript -e "$Applescript"
And I get an error:
17:18: syntax error: Expected expression, property or key form, etc. but found unknown token. (-2741)
I can't tell what the hell's going on here, so I decide to COPY an echo of $Airport and try that as a variable.
Airport=
tell application \"System Events\" to return (choose from list {\"field 2\",\"field 3\",\"field 4\"})
AND THAT WORKS WITHOUT ANY MODIFICATION.
So....
I need to figure out how to do this without having to set my variables permanently.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要试图让它变得比需要的更复杂。利用 shell 的两个字符串引号字符形成一个 shell 单词作为 osascript -e 参数的值:
此外,最好避免使用反引号来进行命令替换;首选
$(command)
形式,因为即使在处理复杂的嵌套时,也更容易构造正确的命令。Don't try to make it more complicated than needed. Take advantage of the shell's two string quote characters to form one shell word as the value for the osascript -e argument:
Also, it's a good idea to avoid the use of backticks to do command substitution; the
$(command)
form is preferred because it is much easier to construct correct commands even when dealing with complex nestings.