bash 将变量从 shell 脚本传递到 applescript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17242773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:45:09  来源:igfitidea点击:

Pass in variable from shell script to applescript

bashshellapplescriptosascript

提问by SirRupertIII

I've got a shell script that I call that uses osascript, and that osascriptcalls a shell script and passes in a variable that I've set in the original shell script. I don't know how to pass that variable in from the applescript to shell script.

我有一个我称之为使用的 shell 脚本,osascriptosascript调用一个 shell 脚本并传入一个我在原始 shell 脚本中设置的变量。我不知道如何将该变量从applescript 传递到shell 脚本。

How can I pass in a variable from shell script to applescript to shell script...?

如何将变量从shell脚本传递到applescript到shell脚本......?

Let me know if I don't make sense.

如果我没有道理,请告诉我。

 i=0
 for line in $(system_profiler SPUSBDataType | sed -n -e '/iPad/,/Serial/p' -e '/iPhone/,/Serial/p' | grep "Serial Number:" | awk -F ": " '{print }'); do
 UDID=${line}
 echo $UDID
 #i=$(($i+1))
 sleep 1


 osascript -e 'tell application "Terminal" to activate' \
 -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down' \
 -e 'tell application "Terminal" to do script "cd '$current_dir'" in selected tab of the front window' \
 -e 'tell application "Terminal" to do script "./script.sh ip_address '${#UDID}' &" in selected tab of the front window'

 done

回答by jm666

Shell variables doesn't expanding inside single quotes. When you to want pass a shell variable to osascriptyou need to use double ""quotes. The problem is, than you must escape double quotes needed inside the osascript, like:

Shell 变量不会在单引号内扩展。当你想要传递一个 shell 变量时,osascript你需要使用双""引号。问题是,您必须转义 osascript 中所需的双引号,例如:

the script

剧本

say "Hello" using "Alex"

you need escape quotes

你需要转义引号

text="Hello"
osascript -e "say \"$text\" using \"Alex\""

This not very readable, therefore it much better to use the bash's heredocfeature, like

这不是很可读,因此最好使用 bash 的heredoc功能,例如

text="Hello world"
osascript <<EOF
say "$text" using "Alex"
EOF

And you can write multiline script inside for a free, it is much better than using multiple -eargs...

而且你可以免费在里面写多行脚本,这比使用多个-e参数要好得多......

回答by Lri

You can also use a run handler or export:

您还可以使用运行处理程序或导出:

osascript -e 'on run argv
    item 1 of argv
end run' aa

osascript -e 'on run argv
    item 1 of argv
end run' -- -aa

osascript - -aa <<'END' 2> /dev/null
on run {a}
    a
end run
END

export v=1
osascript -e 'system attribute "v"'

I don't know any way to get STDIN. on run {input, arguments}only works in Automator.

我不知道有什么办法可以得到 STDIN。on run {input, arguments}仅适用于 Automator。