【问题标题】:how to pass bash variables to AppleScript如何将 bash 变量传递给 AppleScript
【发布时间】:2015-05-23 00:18:04
【问题描述】:
#/bin/bash
corr="apple"
echo $corr

osascript -e 'tell application "Messages" to send  "Did you mean "'"$corr"'" "  to buddy  "A"'

错误:

51:57: syntax error: A identifier can’t go after this “"”. (-2740)

如果我只是通过

osascript -e 'tell application "Messages" to send  "Did you mean $corr "  to buddy  "A"'

消息类似于“您的意思是 $corr”

我已经尝试了Pass in variable from shell script to applescript 中提到的所有内容

【问题讨论】:

  • 格式太可怕了。现在应该更具可读性
  • 如果您尝试发送文字字符串Did you mean "apple",那么您不会转义apple 周围的引号。第一个关闭消息,因此apple 是一个意外标识符,而不是消息字符串的一部分。

标签: bash variables applescript imessage


【解决方案1】:

here 的最后有一个很好的解释和很好的例子。

基于此,我能够完成这项工作(使用 TextEdit,而不是 Messages):

 #!/bin/sh
rightNow=$(date +"%m_%d%B%Y")
osascript -e 'on run {rightNow}' -e 'tell application "TextEdit" to make new document with properties{name: "Date003.txt", text:rightNow}'  -e 'end run' $rightNow

我还发现我必须非常小心单引号和双引号。出于某种原因,我的键盘一直用弯引号代替普通引号。烦人。

【讨论】:

  • 可以在 TextEdit 的首选项中禁用该选项(替换引号)
  • @Zero 谢谢!我很少将 TextEdit 用于 shell 脚本,但我认为这很简单。现在它已经修复了。
  • 虽然在这个特定示例中不是必需的,但我也强烈建议在末尾双引号 $rightNow。在编写 bash 脚本时,这是一个非常的好习惯,因为它可以确保在插入的字符串包含任何空格时行为正确。 (Bash 就是这样一个 POS,它甚至不能正确地进行变量替换 - 相反,它只是按原样插入原始字符串并将其解析为代码。例如,如果字符串是 foo bar,那么 $rightNow 将被替换为 两个单独的参数,foobar,除非你明确地将变量名括在引号中:"$rightNow""foo bar"。)
猜你喜欢
  • 1970-01-01
  • 2012-02-13
  • 1970-01-01
  • 2020-12-23
  • 2011-11-23
  • 2018-02-17
  • 2011-02-17
  • 1970-01-01
相关资源
最近更新 更多