Pass in variable from shell script to applescript
Asked Answered
N

3

11

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
Nonmoral answered 21/6, 2013 at 19:19 Comment(0)
T
15

Shell variables don't expand 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...

Tedi answered 21/6, 2013 at 19:57 Comment(3)
This is bad advice. Aside from being unnecessarily clumsy, it does not sanitize the inserted text so is neither robust nor secure, e.g. text='Bob says "hello"' will cause AS to throw a syntax error due to unescaped quotes. Never use code munging if a better solution exists, which it does: as Lauri Ranta said, define an explicit run handler and pass your strings via ARGV. See #16966617 for more detail.Blaeberry
@Blaeberry You're right, using on run argv is "more" correct. Mine is not an perfect solution, but i'm used it myself many-many times without any problems, it is simple and usable for many scripts...Tedi
Yours is a buggy solution. If $text contains double quote or backslash characters it will cause the AS code either to error or - worse - behave in unintended ways. If you must use code munging, you must sanitize your inputs. e.g. Google "SQL injection attack" to understand why "it works for me" is not an appropriate response when someone points out this flaw.Blaeberry
V
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.

Vashti answered 21/6, 2013 at 21:27 Comment(0)
C
0

Below is what worked for me.

pathToRepo is the variable, which osascript passed to a Terminal that was open and it cd into the correct directory. (And then run npm start which is just for reference if you wanted to add more commands)

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

Source/Reference: https://mcmap.net/q/412475/-osascript-how-to-pass-in-a-variable

Curricle answered 27/10, 2023 at 2:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.