Using AppleScript editor to input multiple commands in terminal - terminal

So i'm trying to write a simple script that opens terminal, ssh onto a server and does stuff while it's there.
tell application "Terminal"
Activate
do script "cd documents"
delay 2
do script "ssh private key user#server"
delay 6
do script "while true; do curl..."
end tell
How do i get it all in one terminal tab?
Currently it opens separate windows for each command

Try:
tell application "Terminal"
reopen
activate
do script "echo \"commmand one\"" in window 1
do script "echo \"commmand two\"" in window 1
end tell

Another way is to use semicolon to concatenate two commands, like this:
tell application "Terminal"
activate
do script "echo \"commmand one\"" & " ; " & "echo \"commmand two\""
end tell
I used & symbol to demo concatenation in case the "echo \"commmand one\"" is a variable.

tell application "Terminal"
reopen
activate
delay 1
do script "cd ~/Projects" in front window
do script "ls -al" in front window
do script "date" in front window
tell application "System Events" to keystroke "t" using {command down}
delay 1
do script "cd ~/Projects/react-app" in front window
do script "ls -al" in front window
end tell

Related

how to save keystroke output in AppleScript variable?

I have written an AppleScript that starts my shell by typing in the commands in the terminal. But I want to test the shell and so I want to save the command result in a variable. This is my code:
#!/bin/bash
osascript <<EOF
tell application "System Events"
keystroke "cd /Users/uwe/documents/coding/c/pshell"
delay 0.5
keystroke return
delay 0.5
keystroke "make compile_and_run"
delay 1
keystroke return
delay 0.5
keystroke "bash /Users/uwe/documents/coding/c/pshell/tests/user_test.sh"
delay 0.5
keystroke return
key code 126
delay 0.5
set arrow_result to keystroke return // this is where I want to save the output
display dialog arrow_result // it says there is no variable called 'arrow_result'
end tell
EOF
If its possible I could also store the result in a bash variable but I don't think that that works
You can of course send keystroke commands and then get the text content of the terminal window (and then parse it) with the following command:
set arrow_result to value of text field 1 of scroll area 1 of splitter group 1 of window 1 of process "Terminal"
But why be so perverted. There is no need to use Terminal, System Events and GUI scripting to complete your task. Instead, use the most powerful tool in the AppleScript language, the do shell script command:
#!/bin/bash
osascript <<EOF
set arrow_result to(do shell script "cd /Users/uwe/documents/coding/c/pshell
make compile_and_run
bash /Users/uwe/documents/coding/c/pshell/tests/user_test.sh")
display dialog arrow_result
EOF
NOTE: I tested my suggestion in the Terminal with following code:
osascript <<EOF
set arrow_result to do shell script "echo \"Hello, World!\""
display dialog arrow_result
EOF

Running multiple Applescripts in order in Automator

I am trying to automate this process.
step 1: change system date to a specific date.
step 2: open an application.
step 3: change system date back to normal.
Now on Automator, I have three apple scripts placed like this.
on run {input, parameters}
tell application "Terminal"
do script with command "sudo date 082704002018"
activate
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
end run
on run {input, parameters}
tell application "Terminal"
do script with command "open -a applicationName"
activate
end tell
end run
on run {input, parameters}
tell application "Terminal"
do script with command "sudo ntpdate -u time.apple.com"
activate
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
end run
The problem is that Automator only runs the first code. I'm not sure how to make it run all the codes in order.
Sorry if this is a stupid question. I am completely new to automator and applescript.
Thank you
I'm not quite sure why you chose to use three separate AppleScripts. You can combine them all into one AppleScript as I have done in this following example. I'm not quite sure why you used the “activate” commands. I don't think they are necessary so I removed those lines of the code. Anyway, this following code should work for you…
tell application "Terminal"
do script with command "sudo date 082704002018"
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
tell application "Terminal"
do script with command "open -a applicationName"
delay 1
do script with command "sudo ntpdate -u time.apple.com"
end tell
delay 1
tell application "System Events"
keystroke "mypassword" & return
delay 3
end tell
Alternately, launching Terminal app to run shell scripts is not necessary all the time as you can run shell scripts in AppleScript by using the “do shell script” command. This following applescript code is your code using only eight lines of code.
do shell script "sudo date 082704002018"
tell application "System Events" to keystroke "mypassword" & return
delay 3
do shell script "open -a applicationName"
delay 1
do shell script "sudo ntpdate -u time.apple.com"
delay 1
tell application "System Events" to keystroke "mypassword" & return
If my versions of your code throw errors, it may be necessary to adjust the delay commands or re-insert the activate commands
If you are hell-bent on using your version of the code and three separate Applescripts, just remove the on run {input, parameters} and end run lines of code from each AppleScript and that should eliminate your problem

Apple script open Terminal with ready command

I'm trying to open terminal using apple script with a ready command but without executing it and allowing user to do this just by clicking enter (so I don't want to use tell Terminal to do script)
One of the approaches I used is using keystrokes:
tell application "Terminal" do script "echo Hi!"
keystroke "abc"
end tell
but it doesn't work for me. Any ideas?
I think you want to start Terminal and have a command all lined up ready in the Terminal ready for the user so he/she only has to press "Enter". If so, you can do this:
tell application "Terminal"
activate
delay 1
tell application "System Events"
keystroke "echo hi"
end tell
end tell
Then the user just has to press Enter and the command echo hi will execute.
it is hard to understand what you mean.
You can't for instance have the terminal wait for a user to click its window.
(But you can poll for a keystroke after the terminal window is opened.)
You'd have to use a dialog before your code, in order to make the user enter the terminal consciously.
display dialog "Press ok to enter the terminal" buttons {"Cancel","Enter"} cancel button 1 default button 2
Other than that, the way you'd need to use system events to send keystroke to the Terminal
tell application "System Events"
tell application process "Terminal"
keystroke "abcd"
end tell
end tell
You can poll for a keypress in the do script command to your terminal with this:
read -n 1 -s MYCHAR </dev/tty
This will force the user to press enter from a do script
a=`read`

How to run shell scripts in series in terminal windows in Mac OS X

In my Desktop/src/ directory I have shell scripts that I want to run in a series and some must run after the first has completed running and all the commands must run on new terminal window. I am working on Mac OS X.
So far I have tried the following code
osascript<<EOF
tell application "System Events"
tell process "Terminal" to keystroke "n" using command down
end
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM1.sh"
end tell
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM2.sh"
end tell
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM3.sh"
end tell
EOF
The problem is that startRM1.sh takes a little time to start, and startRM2.sh, startRM3.sh start straight away and crash since they must wait for startRM1.sh to complete
All three of them must start in new window.
Edit: startRM1.sh is a server which keeps running; therefore the control never goes to the second step.
problem solved using following
osascript<<EOF
tell application "System Events"
tell process "Terminal" to keystroke "n" using command down
end
tell application "Terminal"
activate
do script with command "cd Desktop/src/ && sh startRM1.sh" in window 1
end tell
tell application "Terminal"
activate
do script with command "sleep 5 && cd Desktop/src/ && sh startRM2.sh"
end tell
tell application "Terminal"
activate
do script with command "sleep 5 && cd Desktop/src/ && sh startRM3.sh"
end tell
EOF
Create a shellscript that is your start point.
You can create a .sh file that would could call:
source mynewShell.sh
and in that, it will have all the other files, called in the orders you need, and any config data etc

Use Applescript to Enter command into Terminal but do not Execute

Using Applescript, is it possible to open a new Terminal and enter the command into the terminal but do not run it?
tell application "Terminal"
do script "echo Hello"
end tell
This code will type the line echo Hello into the Terminal and run it. Can we avoid the execution?
Good case for System Events app and emulating keystrokes:
tell application "Terminal" to activate -- only needed if Terminal may not be running
tell application "System Events"
tell application process "Terminal"
set frontmost to true
keystroke "echo Hello"
end tell
end tell

Resources