【问题标题】:Bash output as Applescript list problemsBash 输出为 Applescript 列表问题
【发布时间】:2011-01-08 17:41:32
【问题描述】:

这简直把我逼疯了。我正在尝试在 bash 中读取文件,删除重复项,排序,然后通过 applescript 显示“列表选择”窗口。

我的 $DATALOG 文件格式如下:

字段1 字段2

字段1 字段3

字段1 字段4

等等……

Applescript=awk '{print $2}' $DATALOG | awk ' !x[$0]++' | sort -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 的回声并尝试将其作为变量。

机场=tell application \"System Events\" to return (choose from list {\"field 2\",\"field 3\",\"field 4\"})

并且无需任何修改即可使用。

所以....

我需要弄清楚如何做到这一点,而不必永久设置我的变量。

【问题讨论】:

  • 我忘了写一个临时文件可以正常工作。我真的不想写临时文件...还有其他方式吗?

标签: bash macos list sed applescript


【解决方案1】:

不要试图让它变得比需要的更复杂。利用 shell 的两个字符串引号字符组成一个 shell 单词作为 osascript -e 参数的值:

Applescript=$(awk '{print $2}' $DATALOG | awk ' !x[$0]++' | sort -u | tr "_" " "| sed 's/^/\"/' | sed 's/$/\"/' | tr "\n" "," | sed 's/.$//')
osascript -e 'tell application "System Events" to return (choose from list {'"$Applescript"'})'

另外,避免使用反引号来进行命令替换是个好主意; $(command) 形式是首选,因为即使在处理复杂的嵌套时也更容易构造正确的命令。

【讨论】:

  • 谢谢 - 效果很好。我还是 bash 的新手,所以这些小东西很有帮助。
猜你喜欢
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 2011-01-23
  • 1970-01-01
  • 2018-05-22
  • 1970-01-01
  • 2021-06-11
  • 1970-01-01
相关资源
最近更新 更多