Pass in variable from shell script to applescript

喜欢而已 提交于 2019-11-30 23:17:49

问题


I've got a shell script that I call that uses osascript, and that osascript calls 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.

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

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 $2}'); 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

回答1:


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

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 heredoc feature, like

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 -e args...




回答2:


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.



来源:https://stackoverflow.com/questions/17242773/pass-in-variable-from-shell-script-to-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!