0

Is there a way to respond to a shell script that is asking for info inside of an AppleScript?

I have a script that runs a command that asks two questions when run in Terminal.

I already know the answer and I have to do this a thousand times. Is there a way I can enter those responses so when the shell script prompts me for them it continues the process?

Example:

on run {email}
    set result to do shell script "myprocess \"" & email & "\""
    keystroke "file.png" + "enter" 
    keystroke "y" + "enter"
    return result
end run

The error I get is:

error "Enter the file name to save:"

...which is the prompt in Terminal that is asking me to type in a value.

NOTE: It is very similar to this question but I do not want to open the Terminal window. I want to stay in AppleScript.

Community
  • 1
  • 1
1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231

2 Answers2

0

Yes and no. While do shell script does not allow you to interact with the process, it doesn't look like you really need to. Since you have a known response, you can put the responses in a file and redirect stdin to myprocess.

Something like this:

do shell script "myprocess \"" & email & "\" < responses.txt"

where responses.txt is a file containing:

file.png
y
Michael J. Barber
  • 24,518
  • 9
  • 68
  • 88
  • So each response is on a separate line? Is there a response for return command? Some are optional and just ask you to press enter to continue. – 1.21 gigawatts Jan 16 '15 at 09:19
  • The newline at the end of each line is part of the input that you provide. So an empty line in the file corresponds to pressing just Enter. – tripleee Jun 12 '19 at 04:31
0

You can create an "expect" bash script if you have to respond to prompts and stuff from a shell command. You can do this right inside of applescript.

Here's an example. Suppose I wanted to log into a computer via SSH, run a couple commands, and then exit. I know at the command line I must first ssh into the IP address of the machine, then I have to wait for the machine to ask me for a password, then I must enter the password, then I wait again and so on. I can do that with an expect script inside of applescript like the below script. Note that the line:

expect \\\"password:\\\"

is where I wait for the password request by expecting "password:" command line prompt. After I receive that prompt I send the password with:

send \\\"$PASS\\r\\\"

You can see I then issue some other ssh commands and eventually exit the script. So hopefully this will give you an idea of how you can write your code. You can google for more ideas regarding "expect" scripts.

To explain the back slashes... in a bash script we would have \" in the expect command to escape a quote. Since we're in applescript we have to escape each of those things because they have a special meaning to applescript so it becomes \\" which is \ to send a backslash and \" to send a quote.

Good luck.

set shScript to "#!/bin/bash

HOST=192.168.1.xxx
USER=username
PASS=passwrd
PROMPT=#

expect -c \"
spawn ssh $USER@$HOST
expect \\\"password:\\\"
send \\\"$PASS\\r\\\"
expect \\\"$PROMPT\\\"
send \\\"cd /\\r\\\"
expect \\\"$PROMPT\\\"
send \\\"ls -al\\r\\\"
expect \\\"$PROMPT\\\"
send \\\"exit\\r\\\"
\"
"

do shell script "sh -c " & quoted form of shScript
regulus6633
  • 18,848
  • 5
  • 41
  • 49