Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Use Applescript to run a terminal command.. stuck

I have a Terminal command and associated password to execute the command (a command and a password) I must run dozens of times a day. Bored of doing it hand 1 by 1.

So, I started writing an Applescript (that I will turn into an Application so I can just click it to execute all in 1 click) that opens terminal, enters the command BUT the command is only executed if I enter the necessary password. but, that is where I am stuck. I can open terminal, have it enter the command, but I am not sure how to then follow-up with the password to execute the command when prompted. Here is what I have and it works perfect

tell application "Terminal"
activate
do script "blahblahblahblahblah" in front window
end tell

I have tried these variations, but no luck

do script "blahblahblahblahblah" password "passwordhere" in front window - as a means of entering script and the password in 1 shot
do script "passwordhere" in front window - as a means of then entering the password after first script runs

so, not sure how to get it to follow up the command at the password prompt that comes up and enter the password. Yes, I will keep working on it.. but if you can see the step or help, please advise

I USE to be a Level 3 with hundreds of post ... no seriously, Mac OS X (10.5.6), Loaded G5 + 30" Dell + MBP 17"

Posted on Feb 12, 2009 8:05 AM

Reply
Question marked as Best reply

Posted on Feb 12, 2009 8:19 AM

First question: Do you need to use Terminal.app?

Terminal's support for AppleScript doesn't stretch much beyond taking the text you send it, optionally opening a window, and executing the text in the front window just as if you'd typed it.

Therefore there's no concept of sending a password to Terminal.app because it doesn't know the difference between your text is running a new command or typing into an existing shell application.

That said, there are ways of doing what you want.

If you really, really need to use Terminal.app (e.g. because the command you're sending to Terminal requires some kind of command-line interface that you need to interact with) then you need to send multiple 'do script' commands, one for each block of text you want to send, like:

tell application "Terminal"
activate
do script "blahblah blah" in front window
delay 1 -- pause for a second
do script "passwordhere" in front window
end tell


Remember 'do script' should really be thought of more as 'type this'.

If you don't need to see what's going on you may not need to use Terminal at all. AppleScript can run shell commands directly. It's not useful in all cases (you can't drive vi directly through AppleScript, for example), but it can be used to launch shell processes:

do shell script "blahblahblah"


The issue hinges on the password, and whether the password is entered as a prompt to the process you're executing (which comes back to interactive element that does need Terminal.app), or whether your shell script is using something like sudo to elevate your privileges which is something that AppleScript can handle directly.

If the latter then instead of doing:

tell application "Terminal.app"
do script "sudo /path/to/command"
do script "passwordhere"
end tell


You can just do:

do shell script "/path/to/command" with administrative privileges password "passwordhere"
6 replies
Question marked as Best reply

Feb 12, 2009 8:19 AM in response to gPod

First question: Do you need to use Terminal.app?

Terminal's support for AppleScript doesn't stretch much beyond taking the text you send it, optionally opening a window, and executing the text in the front window just as if you'd typed it.

Therefore there's no concept of sending a password to Terminal.app because it doesn't know the difference between your text is running a new command or typing into an existing shell application.

That said, there are ways of doing what you want.

If you really, really need to use Terminal.app (e.g. because the command you're sending to Terminal requires some kind of command-line interface that you need to interact with) then you need to send multiple 'do script' commands, one for each block of text you want to send, like:

tell application "Terminal"
activate
do script "blahblah blah" in front window
delay 1 -- pause for a second
do script "passwordhere" in front window
end tell


Remember 'do script' should really be thought of more as 'type this'.

If you don't need to see what's going on you may not need to use Terminal at all. AppleScript can run shell commands directly. It's not useful in all cases (you can't drive vi directly through AppleScript, for example), but it can be used to launch shell processes:

do shell script "blahblahblah"


The issue hinges on the password, and whether the password is entered as a prompt to the process you're executing (which comes back to interactive element that does need Terminal.app), or whether your shell script is using something like sudo to elevate your privileges which is something that AppleScript can handle directly.

If the latter then instead of doing:

tell application "Terminal.app"
do script "sudo /path/to/command"
do script "passwordhere"
end tell


You can just do:

do shell script "/path/to/command" with administrative privileges password "passwordhere"

Feb 12, 2009 8:56 AM in response to Camelot

well.. I figured, since my IT people told me to execute these commands via the Terminal app that yes, I must use it to make the system work (working with an SVN for web development off site). I understand that the Terminal gives me the visual view of the command in action, so it is kind of nice to see the terminal run and visually see the actions taking place.

I understand this comment 100% and I figured as much "you need to send multiple 'do script' commands, one for each block of text you want to send," which you can see I tried BUT it appears I never considered having to have....

delay 1 -- pause for a second

now that I added that piece of the script.... it runs 🙂 thanks!

Now, I might actually use AS to run the shell command and not open terminal - I considered that earlier, but kind of liked seeing terminal execute the commands so I can watch the file list it uploads visually. But I just did not want to hand type those 2 commands in every single time dozens of times a day!

"and whether the password is entered as a prompt to the process you're executing".. yes, once I enter the first command, it executes and then prompts me for the password and waits for it. So, it is the former issue not the latter.

from my understanding, "do shell script "/path/to/command" with administrative privileges password "passwordhere" just changes my local privileges, but the password I actually input is needed for the server I am accessing to allow the action to take place I want it to execute.

Feb 12, 2009 10:01 AM in response to gPod

from my understanding, "do shell script "/path/to/command" with administrative privileges password "passwordhere" just changes my local privileges, but the password I actually input is needed for the server I am accessing to allow the action to take place I want it to execute.


That's correct - it's the equivalent of using sudo to elevate your privileges to execute the command. If that command subsequently requests another password (e.g. as part of a login sequence) then this option won't work for you (unless the command has a command-line switch where you can pass in the password like "/path/to/command --password=blah"

Feb 12, 2009 11:16 AM in response to gPod

While AppleScript isn't the fastest thing out there, it's worth bearing in mind that AppleScript will run as fast as it can. In this case, without the delay the password is being sent before the first command has a chance to get to the point of needing the password (i.e. it was still loading).

Adding a static delay is a hack, but it works for the most part. A better solution would be to watch the content of the window and wait for the password prompt before continuing, but that's a lot more complex.

Use Applescript to run a terminal command.. stuck

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.