Use Applescript to Enter command into Terminal but do not Execute - macos

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

Related

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

How to open multiple panes on iTerm?

I have created aliases of applescripts to open multiple panes on iTerm. However, ever since the latest update the scripts stopped working. I keep getting this error:
syntax error: Expected end of line but found identifier. (-2741)
Here's the script:
newPaneDown() {
osascript -e "
tell application \"iTerm\"
make new terminal
tell the current terminal
activate current session
tell the last session
tell i term application \"System Events\" to key code 2 using {shift down, command down}
end tell
end tell
end tell"
}
newPaneLeft() {
osascript -e "
tell application \"iTerm\"
make new terminal
tell the current terminal
activate current session
tell the last session
tell i term application \"System Events\" to key code 2 using command down
end tell
end tell
end tell"
}
newPanes4x4() {
/usr/bin/env osascript <<-EOF
tell application "iTerm"
activate
launch session "Panes"
tell i term application "System Events" to keystroke "d" using command down
tell i term application "System Events" to keystroke "D" using command down
tell i term application "System Events" to keystroke "[" using command down
tell i term application "System Events" to keystroke "[" using command down
tell i term application "System Events" to keystroke "D" using command down
end tell
EOF
}
alias p2='newPaneLeft'
alias p3='newPaneDown && newPaneLeft'
alias p4='newPanes4x4'
Applescript is not backwards compatible since iTerm2 Version 3.
The new Applescript syntax is described here.
You should replace:
make new terminal
with
create window with default profile

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

Using AppleScript editor to input multiple commands in 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

Resources