Send argument values to new terminal - bash

I want to open new terminal and pass some values on OSX. I tried this code:
open_new_terminals_automatically()
{
osascript -e 'tell application "Terminal" to do script "cd $1; $2"'
}
# call the function and pass arguments
open_new_terminals_automatically "/root/var/fome_path" "some_commnds -argument"
This is very simple example to explain what I want to do.
How I can implement it to run as bash script on OS X.

What you have almost works, except you need double quotes instead of single quotes for the variable expansion to work properly.
Just create a script run.sh with contents
osascript -e "tell application \"Terminal\" to do script \"cd $1; $2\""
Then you can call it with sh run.sh "/root/var/fome_path" "some_commnds -argument".
If you want this all done in one script, then just do
open_new_terminals_automatically()
{
osascript -e "tell application \"Terminal\" to do script \"cd $1; $2\""
}
# call the function and pass arguments
open_new_terminals_automatically "/root/var/fome_path" "some_commnds -argument"

Related

Insert a variable in-between ' ' in a shell osascript command?

I am using a MacOS osascript command in a shell script and I try to run the following command:
APPNAME=$#
if pgrep -x "$APPNAME" > /dev/null # checking if app is open
then
echo "Closing..."
osascript -e 'quit app $APPNAME'
else
echo "*** The app is not open"
fi
Ideally, the command would be osascript -e 'quit app "Calendar"' as an example of a working solution. However, I can't seem to insert a variable in-between ' ' quotation marks.
What would be a workaround?
Trying to build a script dynamically using string interpolation is always fragile. You should pass the app name as an argument to the AppleScript, so that you don't have to worry about escaping any characters through two levels of interpreters.
APPNAME=$1 # The name should be passed as a single argument.
if pgrep -x "$APPNAME" > /dev/null # checking if app is open
then
echo "Closing..."
osascript -e 'on run argv' -e 'quit app (item 1 of argv)' -e 'end run' "$APPNAME"
else
echo "*** The app is not open"
fi
No matter what the value of APPNAME is, you are executing the exact same script; the only difference is the argument that script receives.
The point (one of the points, anyway) of single quotes is to prevent variable interpolation. It sounds like what you really want is a way to get double quotes into a string. There are lots of ways to do that. A common one is:
osascript -e "quit app \"$appname\""
This implementation worked for me:
osascript -e 'quit app "'"$APPNAME"'"'
Trying this using:
osascript -e 'quit app "\"$APPNAME\""'
did not work resulting in error showing: "\"$APPNAME\""... as the value it is seeing.
See https://discussions.apple.com/thread/251633896 for more
Try:
osascript -e 'quit app '"$APPNAME"
Or, if osascript requires additional double quotes, try:
osascript -e 'quit app "'"$APPNAME"'"'

Passing arguments with "open -a Terminal"

It seems like it's a very stupid question.
I'm trying to open a bash-file with (another) Terminal inside a bash-file, providing two arguments.
Something like this:
open -a Terminal path/to/file.sh ARG1 ARG2
I tried something like that, which gives me no errors but simply doesn't provide the arguments:
open -a Terminal path/to/file.sh --args ARG1 ARG2
Can someone help me?
I think you mean this:
osascript -e 'tell application "Terminal" to do script "date +s"'
or this with arguments:
osascript -e 'tell application "Terminal" to do script "echo 'arg2' 'arg2'"'
Or this way of working may suit better:
osascript<<EOF
tell application "Terminal"
do script "yourScript $1 $2"
end tell
EOF

Apple script parameter with space in path

I am unable to do the following - as there is a space in the $MY_PATH
osascript -e "tell application \"Terminal\" to do script \"cd $MY_PATH\""
Can this be done. I know that in a normal sh script I need to do something like "$(pwd)" but I don't know how to do this, when passing it to an Apple script?
This is actually quite tricky, because you essentially have two levels of quoting/parsing to deal with: you have to get the path string from shell (your script) into AppleScript intact, then from AppleScript back into shell (the argument to cd). You could wrap it in multiple layers of quotes and let each translation unwrap one layer, but that gets messy quickly (especially since shell and AppleScript have significantly different quoting rules).
But there are a couple of tricks you can use: to get the path into AppleScript, pass it as an argument to the script instead of part of the script (see this previous answer to "osascript using bash variable with a space"), then use the AppleScript quoted form of to prep it to use with cd:
osascript \
-e "on run(argv)" \
-e "set my_path to item 1 of argv" \
-e "tell application \"Terminal\" to do script \"cd \" & the quoted form of my_path" \
-e "end" \
-- "$MY_PATH"
UPDATE: I haven't used docker, and I don't have a setup to test with, but I think I see how to fix up the combined script. First, let me point out some problems and unnecessary confusion in your current script:
DOCKER_EVAL=$(printf "%s" 'eval $(docker-machine env default)')
...this is just an elaborate equivalent of DOCKER_EVAL='eval $(docker-machine env default)' -- the whole printf "%s" thing isn't actually doing anything. It's also not the what you should be using; that's DOCKER_EVAL='eval "$(docker-machine env default)"'; the double-quotes might not be necessary, but are recommended to avoid possible misparsing. Now look at this line:
SERVER_COMMAND=$(printf "%s;sh %q" "$DOCKER_EVAL" "$SERVER_PATH")
here the %q format string is asking for the SERVER_PATH to be "quoted", but it's being quoted in a form suitable for bash, not for AppleScript. Including that string in an AppleScript is probably what's causing the error (though as I said, I don't have a setup to test with).
There are several ways to solve this, but the easiest is probably to pass the DOCKER_EVAL command in the same way as the path:
DOCKER_EVAL='eval "$(docker-machine env default)"'
osascript \
-e "on run(argv)" \
-e "set docker_eval to item 1 of argv" \
-e "set my_path to item 2 of argv" \
-e "tell application \"Terminal\" to do script docker_eval & \"; sh \" & the quoted form of my_path" \
-e "end" \
-- "$DOCKER_EVAL" "$SERVER_PATH"
...alternately, you could include the eval "$(... bit directly in the AppleScript, but you'd wind up with multiple layers of escapes. This way's cleaner.
BTW, I don't know if the commands docker-machine env produces export the various variables, or just set them; if they're not exported, the sh shell won't inherit them; you may have to source it instead. But that's uglier in some ways, and hopefully won't be necessary.
Perhaps this:
printf 'tell application "Terminal" to do script "cd %q"' '/a/path with spaces' |
osascript
Result:
tell application "Terminal" to do script "cd /a/path\ with\ spaces"
Or this:
printf 'tell application "Terminal" to do script "cd \x27%s\x27"' '/a/path with spaces' |
osascript
Result:
tell application "Terminal" to do script "cd '/a/path with spaces'"

Issue with multiple quotation marks

I can't figure out how I am supposed to write this. Can anyone help?
"do shell script \"osascript -e 'quit app "\(clientUsed)"'""
The script is supposed to say this (if notes was clientUsed)
osascript -e 'quit app "notes"'
try with this (we escape " with \" )
"do shell script \"osascript -e 'quit app \"\(clientUsed)\"'\""
it corresponds to the command:
do shell script "osascript -e 'quit app "YOUR_CLIENT_USED"'"

Pass in variable from shell script to applescript

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

Resources