Skip navigation

Saving Text Output from Terminal as Variable

1640 Views 6 Replies Latest reply: Jan 22, 2014 1:14 PM by Edward K. Chew RSS
yellowb0x Calculating status...
Currently Being Moderated
Oct 27, 2012 8:03 PM

Hey guys I know that running commands in terminal is overcomplicating things, but I need to know how to save the output from a Terminal command as a text variable. This is the first script I tried...



do shell script "cd downloads/example"

do shell script "..."

do shell script "..."

set savedresult as result

display dialog savedresult



For some reason the shell script couldn't locate the file "downloads/example" when it was clearly there

So I resorted to using Terminal to run the exact same commands which worked fine.



tell application "Terminal"


set problem1 to do script "cd downloads/example"

do script "..." in problem1

do script "..." in problem1

set savedresult to result

display dialog savedresult



The problem with this is that I get this error...


error "Can’t make «class ttab» 1 of window id 347 of application \"Terminal\" into type text." number -1700 from «class ttab» 1 of window id 347 to text


So I'm stuck here and need some info on how to fix either of these problems. Thanks for any help you provide.



MacBook Pro (15-inch Late 2008), Mac OS X (10.7.4), AppleScript
  • Niel Level 10 Level 10 (234,580 points)
    Currently Being Moderated
    Oct 27, 2012 8:10 PM (in response to yellowb0x)

    You need to use:


    do shell script "cd ~/Downloads/example;"


    and then the second command in the first do shell script line. If not done, the cd won't work, because it targets the top level of the drive and not the home folder, and if it did, its effect wouldn't carry over into the second shell command.



  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Oct 27, 2012 10:06 PM (in response to yellowb0x)

    For some reason the shell script couldn't locate the file "downloads/example" when it was clearly there


    You have to consider/realize that each 'do shell script' command is really its own entity. It knows nothing of any other commands run in any other 'do shell script' command.


    The solution, therefore, is to wrap all your commands in a single 'do shell script' statement. You can separate commands with a semi-colon, e.g.:


    do shell script "cd ~/downloads/example; othercommand -a -b -c; someothercommand -x -y -z"


    As for your other approach of using (which isn't recommended if you can get do shell script to work), the problem to that one lies in the fact that you're saying:


    do script "..." in problem1

    set savedresult to result

 's dictionary states that the result of 'do script' is the tab the command was executed in, and you cannot convert a tab to text. So instead of:


    display dialog savedresult


    You should:


    display dialog contents of savedresult


    Where 'contents' of a tab indicates the text within the window. There are several caveats to this, though. One is that the contents will contain the whole window text, not just the output of the last command. The second is that there's no concept of command execution - that is the script doesn't care if the command takes some time to execute - it wil grab the contents of the window, even if the command hasn't yet finished.

    For these reasons (and more) use do shell script and eschew altogether.

  • MrHoffman Level 6 Level 6 (11,700 points)
    Currently Being Moderated
    Oct 28, 2012 8:16 AM (in response to yellowb0x)

    You've gotten some good feedback from folks here.


    What's overcomplicating things here is arguablty the use of AppleScript.  AppleScript is intended for scripting OS X GUI application operations.  Beyond that particular domain and requirement, it's often not the most expeditious choice. 


    For scripting Unix-level activities, bash and Python and various other scripting languages are often more appropriate; for non-GUI-scripting tasks. 


    And FWIW, is just a window into a native Unix command shell; it's nothing special as far as scripting applications go.  It's the Unix level you're working with here, and an ssh or telnet session into your OS X system could run the same bash scripts; no required.  Put another way, you can invoke shell scripts from contexts other than, including directly from the GUI; the shell scripts can be (are) applications.


    Invoking GUI dialog boxes from bash (or another shell) is possible with the osascript command that's available on OS X, if you need to prompt for stuff.  (But this assumes that the bash script is running with the GUI around.)  For instance:


    osascript -e 'tell app "System Events" to display dialog "Your Text Here"'


    Having gone a very long way 'round to my actual question, what are you up to here?  What problem(s) are you solving with this AppleScript?  Scripting might well be the shortest route, but there might be an alternative route, and possibly a better route.

  • ninjon Level 1 Level 1 (0 points)
    Currently Being Moderated
    Nov 14, 2012 1:08 PM (in response to Camelot)

    Unfortuanately this is not working for me. Here is a sample I tried to make to get this working.


    tell application "Terminal"

      do script "pwd" in window 1

              set testVar to result

      display dialog contents of testVar

    end tell


    but I still get this errror

    "Terminal got an error: Can’t make tab 1 of window id 1447 into type string." number -1700 from tab 1 of window id 1447 to string


    I have

    I have re-read your reply several times and I still have not been able to pull out why this is not working for me. I am on Mac 10.8

  • Edward K. Chew Level 1 Level 1 (40 points)
    Currently Being Moderated
    Dec 16, 2013 2:32 PM (in response to ninjon)

    I feel your pain ninjon. After numerous futile attempts to access the contents of the Terminal window tab returned by do script, I noticed something. If I actually spelled out the whole object with tab and window indices, it would happily return the contents. So here's a little routine which looks through the windows and tabs until it finds the one in question and returns its contents. (It uses the tty strings to find the matching tab. For whatever reason, the tty property could be read from the original tab object where the contents could not.) This code was tested on my iMac running Mavericks. Hope it works for you too!


    tell application "Terminal" to set termTab to do script "pwd"

    delay 2

    set tabText to contentsOfTerminalTab(termTab)


    on contentsOfTerminalTab(termTab)

              tell application "Terminal"

                        set termTabTTY to tty of termTab

                        set iWindow to 1


                                  set tabCount to number of tabs in window iWindow

                                  repeat with iTab from 1 to tabCount

                                            set windowTabTTY to tty of tab iTab of window iWindow

                                            if windowTabTTY is termTabTTY then return contents of tab iTab of window iWindow

                                  end repeat

                                  set iWindow to iWindow + 1

                        end repeat

              end tell

    end contentsOfTerminalTab

  • Edward K. Chew Level 1 Level 1 (40 points)
    Currently Being Moderated
    Jan 22, 2014 1:14 PM (in response to Edward K. Chew)

    I submitted a bug report to Apple over this and learned that what is returned by "do script" is a reference to the window tab and not the tab itself. You need to specify "contents of" to get to the object and then "contents of" again to get the text in the object. So here's what you need to do:


    tell application "Terminal" to set termTabRef to do script "pwd"

    delay 2

    set tabText to contents of contents of termTabRef


More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.