Bash中的变量组合使用osascript命令形式向AppleScript的命令 [英] Combining variables in Bash to form a command sent to AppleScript using the osascript command

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

问题描述

我试图让这个脚本的工作。这是一个猛砸的脚本,是为了采取一些变量,把它们放在一起,并使用结果发送 AppleScript的的命令。手动粘贴从变量 to_osa 背后 osascript -e 到终端呼应的字符串工程,我想,它期望。但是,当我尝试命令 osascript -e 和字符串 to_osa 相结合,这是行不通的。我怎样才能使这项工作?

<$p$p><$c$c>the_url=\"\\\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\\\"\"
the_script ='告诉应用程序野生动物园设定前文档的URL
定界符='
to_osa = $ {分隔符} $ {the_script} $ {THE_URL} $ {}定界符
回声$ {} to_osa
osascript -e $ {} to_osa

在除工作手动脚本也适用当我写所需的命令脚本,然后执行:

 回声osascript -e$ to_osa&GT; 〜/桌面/ outputfile.sh
SH〜/桌面/ outputfile.sh


解决方案

作为一般规则,不要把双引号中的变量,把它们的围绕的变量。在这种情况下,它更复杂,因为你有一些双引号对于bash级报价,有些人的AppleScript级报价;在这种情况下,AppleScript的级别行情在变去,在bash级引号的变量去:

<$p$p><$c$c>the_url=\"\\\"http://stackoverflow.com/questions/1521462/looping-through-the-content-of-a-file-in-bash\\\"\"
the_script ='告诉应用程序野生动物园设定前文档的URL
osascript -e$ {the_script} $ {} THE_URL

BTW,使用回声来检查这样的事情是非常误导。 回声告诉你什么是在变量,而不是当你引用一个命令行变量会被执行了什么。最大的区别是,回声显示它的参数的之后的他们已经通过庆典解析(报价和逃生拆除等),但是当你说手动粘贴串......作品你说这是你想要的的解析。如果行情是那里的呼应字符串,这意味着庆典不承认他们作为报价和删除它们。比较:

 字符串='引用的字符串'
回声$字符串#打印与它周围的双引号的字符串,因为bash的doesnt't承认他们在一个变量
回声报援引字符串#打印*不*引号,因为庆典识别并删除它们

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

解决方案

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}"

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天全站免登陆