Applescript–execute multi line code - bash

I have some apple script code:
tell application "System Events"
key code 97
end tell
How do i write the code as a osascript -e command in Terminal?
Everytime I try using \n or the such, I get errors. Sorry if I'm not being specific enough.

You have a couple of options:
Pass each line of the AppleScript code as a separate -e option:
osascript -e 'tell application "System Events"' -e 'key code 97' -e 'end tell'
Pipe the AppleScript code to osascript's STDIN:
osascript <<END
tell application "System Events"
key code 97
end tell
END
Oh, and you can also save AppleScript code as an executable shell script. Just add #!/usr/bin/osascript at the top of the code and save it as a plain text file:
#!/usr/bin/osascript
tell application "System Events"
key code 97
end tell

Other example:
open -a Terminal && \
sleep 2 && \
osascript -e 'activate application "Terminal"' -e 'tell application "System Events" to keystroke "q" using command down'
the first two lines are just to show the final goal, which is focus the Terminal window and quit it, sending Command+q

Actually -e option accepts new lines:
osascript -e '
tell application "System Events"
key code 97
end tell'

Related

Can't send keystrokes from Terminal to app using osascript in MacOS

I have TextEdit open.
I want to enter "hello" in TextEdit, but not by typing it in myself, but using a command from Terminal.
I tried this:
osascript -e 'tell application "TextEdit" to keystroke "hello"'
but it give the following error:
31:48: execution error: TextEdit got an error: Can’t get keystroke "hello". (-1728)
What am I doing wrong?
red_menace is right, this works:
osascript -e 'activate application "TextEdit"'; osascript -e 'tell application "System Events" to keystroke "hello"'

How to automate program execution using bash shell

I wrote a program that I want to execute in 30 terminal tabs.
So I have this shell program:
for i in {1..29}
do
osascript -e 'tell application "Terminal" to activate' -e 'tell application "System Events" to tell process "Terminal" to keystroke "t" using command down'
done
And for every terminal windows just opened I want to do something like:
cd "Folder {i}"
python3 script.py
How can I achieve this?
As you are opening a new tab this becomes the front window, so just tell the front window to execute a script, changing the folder as necessary.
for i in {1..29}; do
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 folder$i ; script.py\" in window 1"
; done
Hope this helps

How to execute commands in new tabs?

I want to start two different databases but want to keep each process running in a separate tab in a terminal. How do I do this within a shell script? I currently have the following code:
#! /bin/bash
mysqld &
redis-server
Each database needs its own tab. This is on OSX.
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 "mysqld" in selected tab of the front window'
(based on https://stackoverflow.com/a/7177891/1566267)
The similar command is for redis-server.

Have a script wait until the last script is complete

I have a script that has two apple scripts that open a new tab and do a command. I want the second apple script to wait until the first apple script's command line action is completed. I would rather not have to only to a sleep for x long, before it continues.
Is there a way that I can do this?
Here is what I have:
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 "./my_script arg1" in selected tab of the front window'
------ Wait until this process is finished -------
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 "./my_script different_arg1" in selected tab of the front window'
You can check the busy property of a window or tab:
tell application "Terminal"
set w to do script "sleep 1"
repeat
delay 0.1
if not busy of w then exit repeat
end repeat
end tell
osascript -e 'on run {a}
tell application "Terminal"
activate
tell application "System Events" to keystroke "t" using command down
do script "cd " & quoted form of a & "; sleep 1" in window 1
repeat
delay 0.1
if not busy of window 1 then exit repeat
end repeat
tell application "System Events" to keystroke "t" using command down
do script "cd " & quoted form of a & "; sleep 1" in window 1
end tell
end run' /tmp

applescript to osascript conversion?

I am using max msp to run shell commands, I have been prototyping code in applescript and need them to run in osascript for example -
tell application "Google Chrome" to close tab 1 of window 1
converts to
osascript -e 'tell application \"Google Chrome\" to close tab 1 of window 1'
I have converted around 10 commands, but am stuck on the very last one which is
tell application "Google Chrome" to activate
tell application "System Events"
tell process "Google Chrome"
do shell script "/usr/local/bin/cliclick/ c:360,550"
end tell
end tell
which I think goes to
osascript -e 'tell application \"Google Chrome\" to activate' -e 'tell application \"System Events\" to tell process \"Google Chrome\" to do shell script \"/usr/local/bin/cliclick c:360, 550\"'
cliclick lets you use the mouse through shell. http://www.bluem.net/en/mac/cliclick/. The c is the command identifier for clicking, so at x360 y550
is my syntax correct? it works when I dont include the c identifier.
Thanks
I tried this and it gave me this error message:
99:151: execution error: System Events got an error: Invalid argument “360,” to command “c”: Expected two coordinates, separated by a comma. Example: “c:123,456” (1)
Solution: you have an extra space before the number 550 (c:360, 550) and the second value gets lost. Remove the space character and it should work (c:360,550)…
Here is a version where I also changed the quoting:
osascript -e "tell application \"Safari\" to activate" -e "tell application \"System Events\" to tell process \"Safari\" to do shell script \"/usr/local/bin/cliclick c:360,550\""

Resources