将变量从shell脚本传递到applescript

afdcj2ne  于 11个月前  发布在  Shell
关注(0)|答案(3)|浏览(133)

我调用了一个shell脚本,它使用osascriptosascript调用了一个shell脚本,并传入了一个变量,这个变量是我在原始shell脚本中设置的,我不知道如何将这个变量从applescript传入shell脚本。
如何将变量从shell脚本传递到applescript再传递到shell脚本.?
如果我说的不对就告诉我。

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

字符串

xjreopfe

xjreopfe1#

Shell变量不会在单引号内展开。当你想将shell变量传递给osascript时,你需要使用双""引号。问题是,你必须在osascript中转义双引号,比如:
脚本

say "Hello" using "Alex"

字符串
你需要转义引号

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


这不是很可读,因此最好使用bash的heredoc特性,如

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


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

tgabmvqs

tgabmvqs2#

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

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

字符串
我不知道任何方法来获得标准。on run {input, arguments}只适用于自动化。

g9icjywg

g9icjywg3#

下面是为我工作的。
pathToRepo是变量,其中osascript传递到一个终端是开放的,它cd到正确的目录.(然后运行npm start这只是为了参考,如果你想添加更多的命令)

pathToRepo="/Users/<YOUR_MAC_NAME>/Documents/<REPO_NAME>"

osascript - "$pathToRepo" <<EOF
    on run argv -- argv is a list of strings
        tell application "Terminal"
            do script ("cd " & quoted form of item 1 of argv & " && npm start")
        end tell
    end run
EOF

字符串
来源/参考:https://stackoverflow.com/a/67413043/6217734

相关问题