【问题标题】:Combining variables in Bash to form a command sent to AppleScript using the osascript command使用 osascript 命令组合 Bash 中的变量以形成发送到 AppleScript 的命令
【发布时间】:2013-06-02 16:46:45
【问题描述】:

我正在尝试使这个脚本工作。这是一个Bash 脚本,用于获取一些变量,将它们放在一起并使用结果发送AppleScript 命令。手动将从变量to_osa 回显到osascript -e 后面的字符串粘贴到终端可以按我的意愿和期望工作。但是当我尝试组合命令osascript -e 和字符串to_osa 时,它不起作用。我怎样才能做到这一点?

the_url="\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\""
the_script='tell application "Safari" to set the URL of the front document to '
delimiter="'"
to_osa=${delimiter}${the_script}${the_url}${delimiter}
echo ${to_osa}
osascript -e ${to_osa}

除了手动工作之外,当我将所需的命令写入脚本然后执行它时,该脚本也可以工作:

echo "osascript -e" $to_osa > ~/Desktop/outputfile.sh
sh  ~/Desktop/outputfile.sh

【问题讨论】:

    标签: macos bash applescript osascript


    【解决方案1】:

    作为一般规则,不要将双引号放在变量中,将它们围绕变量。在这种情况下,它更复杂,因为您有一些用于 bash 级引用的双引号,还有一些用于 AppleScript 级引用;在这种情况下,AppleScript 级引号位于变量中,bash 级引号位于变量周围:

    the_url="\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\""
    the_script='tell application "Safari" to set the URL of the front document to '
    osascript -e "${the_script}${the_url}"
    

    顺便说一句,使用echo 来检查这样的事情是高度误导的。 echo 告诉您变量中的内容,而不是在命令行上引用变量时将执行的内容。最大的区别是echo 他们已经通过 bash 解析(引号和转义删除等)之后打印它的参数,但是当你说“手动粘贴字符串......有效”时,你'是说这是你想要的解析之前。如果回显字符串中有引号,则意味着 bash 没有将它们识别为引号并删除它们。比较:

    string='"quoted string"'
    echo $string          # prints the string with double-quotes around it because bash doesnt't recognize them in a variable
    echo "quoted string"  # prints *without* quotes because bash recognizes and removes them
    

    【讨论】:

      【解决方案2】:

      字符串混搭可执行代码容易出错且很邪恶,这里完全不需要它。通过定义显式的“运行”处理程序将参数传递给 AppleScript 很简单:

      on run argv -- argv is a list of strings
          -- do stuff here
      end run
      

      然后你像这样调用它:

      osascript -e /path/to/script arg1 arg2 ...
      

      顺便说一句,如果你的脚本需要固定数量的参数,你也可以这样写:

      on run {arg1, arg2, ...} -- each arg is a string
          -- do stuff here
      end run
      

      ...

      更进一步,您甚至可以使 AppleScript 像其他任何 shell 脚本一样直接可执行。首先,添加一个hashbang如下:

      #!/usr/bin/osascript
      
      on run argv
          -- do stuff here
      end run
      

      然后将其保存为未编译的纯文本格式并运行chmod +x /path/to/myscript 以使文件可执行。然后,您可以从 shell 执行它,如下所示:

      /path/to/myscript arg1 arg2 ...
      

      或者,如果您不想每次都指定完整路径,请将文件放在 /usr/local/bin 或您的 shell PATH 上的其他目录中:

      myscript arg1 arg2 ...
      

      ...

      所以你应该如何编写你的原始脚本:

      #!/bin/sh
      the_url="http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash"
      osascript -e 'on run {theURL}' -e 'tell application "Safari" to set URL of document 1 to theURL' -e 'end run' $the_url
      

      快速、简单且非常强大。

      --

      附言如果您希望在新窗口而不是现有窗口中打开 URL,请参阅 OS X 的 open 工具的手册页。

      【讨论】:

      • 为什么 the_url 变成了 URL 呢?
      • 习惯;随意命名。
      • 如果希望 Apple srcipt 执行依赖于变量的 shell 脚本怎么办?例如。 osascript -e '做shell脚本\"echo $the_url\"'。
      • @lef,遇到了同样的问题,需要此信息来运行脚本。我发现这对我有用:一些变量定义 variable=$1 加上 the_script='tell application "terminal" to do script "echo 'osascript -e "${the_script}${variable}\"" ,它应该可以扩展到几乎任何东西。注意echo 前面的孤独的" 和转义的\" 以关闭它。
      猜你喜欢
      • 2014-06-19
      • 2012-03-02
      • 2013-08-06
      • 2014-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2020-03-22
      相关资源
      最近更新 更多