How to get results from do shell script

I am trying to get results from a do shell script. If I run a do shell script, is it easy to just get the results displayed in the terminal? How would you go about it?

is it possible to just set variableName to a result of a doshell script?

Yes, just set a variable to the result of the shell script as follows:

set the_script to “echo Hello World”
set the_result to do shell script the_script
display dialog the_result


If you want the result displayed in the, use the “do script” command in a tell block e.g.,

tell application "Terminal"
	do script "date"
end tell

Using the “do shell script” command invokes the “sh”, which will not open a window e.g.,

do shell script "date"

The result will show up in the ‘Results’ window of your Script Editor.
If you want to diaplay the result in a dialog window via AppleScript, you can try this…

set x to do shell script "date"
display dialog x

Thanks! that really helps. I got one more question related to this

say I launch an application through the “do shell script” through applescript. How would I then send commands to that application through applescript? for example telling it to quit.

I’m not sure I understand what you are trying to do. What application are trying to launch via “do shell script”? Why are you using AppleScript to run a “do shell script” to launch an application? Please post an example of the code you are using, there are way too many unknowns to try to help you.

what I’m trying to do is be able to open an application like pico and be able to manipulate it through a gui I am creating with applescript studio and also be able to see the status of it through my gui. So I want to be able to send a command though my gui to it once it is already running to change preferences and such and get the results displayed in my gui.

You might not be able to do that via AppleScript…

Here’s some info from Apple…

I tried using

do shell script "cd $HOME;pico >> testtext; echo some text"

which will create the file named “testtext” in the home directory, but the contents of the file are the error message “Environment variable TERM not defined!”

I don’t remember if it was on this board or on the Apple/Applescript board, but I remember a discussion about this. When ‘do shell script’ is called, a shell is opened (I think /bin/sh) neither .login, .tcshrc nor .cshrc are read, therfore environment variables will not be defined for the do shell script.

Not sure what a workaround would be …

Thanks for your help. That clarifies it. It is just not possible to interact with an application that way. I would have to keep the terminal open and pass commands to it but still might now be able to get the response. The other option would be to use expect and learn tcl which defeats the purpose of applescript.

Thanks all!