在 Bash 中组合变量以形成使用 osascript 命令发送到 AppleScript 的命令 [英] Combining variables in Bash to form a command sent to AppleScript using the osascript command

查看:26
本文介绍了在 Bash 中组合变量以形成使用 osascript 命令发送到 AppleScript 的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to make this script work. It's a Bash script that is meant to take some variables, put them together and use the result to send an AppleScript command. Manually pasting the string echoed from the variable to_osa behind osascript -e to the terminal works as I want and expect it to. But when I try to combine the command osascript -e and the string to_osa, it does not work. How can I make this work?

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}

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

In addition to working manually the script also works when I write the desired command to a script and then execute it:

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

推荐答案

作为一般规则,不要在变量中放置双引号,而是将它们包围变量.在这种情况下,它更复杂,因为您有一些用于 bash 级引用的双引号,以及一些用于 AppleScript 级引用的双引号;在这种情况下,AppleScript 级引号进入变量,bash 级引号环绕变量:

As a general rule, don't put double-quotes in the variable, put them around the variable. In this case it's more complicated, since you have some double-quotes for bash-level quoting, and some for AppleScript-level quoting; in this case, the AppleScript-level quotes go in the variable, the bash-level quotes go around the variable:

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 没有将它们识别为引号并将它们删除.比较:

BTW, using echo to check things like this is highly misleading. echo is telling you what's in the variable, not what'll be executed when you reference the variable on a command line. The biggest difference is that echo prints its arguments after they've been through bash parsing (quote and escape removal, etc), but when you say "Manually pasting the string ... works" you're saying it's what you want before parsing. If the quotes are there in the echoed string, that means bash didn't recognize them as quotes and remove them. Compare:

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

这篇关于在 Bash 中组合变量以形成使用 osascript 命令发送到 AppleScript 的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆