Wojtek
Wojtek

Reputation: 1044

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?

Upvotes: 0

Views: 3005

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207405

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.

Upvotes: 1

McUsr
McUsr

Reputation: 1409

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`

Upvotes: 0

Related Questions