Get Contents of Terminal.app Window
Description
Twitter user (and fellow Brooklynite) JustinD was looking for a way to use AppleScript to search the contents of a window in Apple’s Terminal.app and, if it found something it was looking for, continue on with some additional actions.
I haven’t tested this, but thought I’d throw this out as a “1.0″ approach to solving the problem via AppleScript. Change the properties at the beginning to add the code for Terminal to run and, optionally, also the thing you want the script to look for before moving on.
Anyone know a better way to do this via AppleScript? Let us know in the comments below!
The Code
Veritrope.com
Get Contents of Terminal.app Window
Version 1.0
January 23, 2012
Project Status, Latest Updates, and Comments Collected at:
http://veritrope.com/code/get-contents-of-terminal-window
// CHANGELOG:
1.0 Initial Release
*)
(*
======================================
// USER DEFINED PROPERTIES
======================================
*)
--TYPE IN THE TERMINAL COMMAND HERE
property script_Command : ""
--SEARCH STRING (OPTIONAL)
property contents_Search : ""
(*
======================================
// MAIN PROGRAM
======================================
*)
-- THIS SCRIPT WILL RETURN THE TERMINAL
-- WINDOW'S CONTENTS TO A VARIABLE (t_Contents)
tell application "Terminal"
set resultTab to do script script_Command
activate
tell front window to set t_Contents to (contents of selected tab)
-- OPTIONAL ACTION IF THE CONTENTS CONTAIN
-- YOUR SEARCH STRING:
if t_Contents contains contents_Search then
-- DO SOMETHING...
end if
end tell
Quick Links
Click here to open it in your Script Editor
Save this into Snippets
New To AppleScript?
- Here's how to use the AppleScripts you find here and -- to make them even easier to use -- here's how run them with simple keyboard shortcuts!
- Be sure to check out other great AppleScripts in the Veritrope.com Code Library -- and share your own AppleScript projects using the quick submission form!




One Response to “Get Contents of Terminal.app Window”
Thanks Justin!
So for my purposes, I had to eliminate this line/variable:
set resultTab to do script script_Command
… as it would spawn a new Terminal window before executing the command – and it was necessary for the command to be executed in the previous running window.
I instead had it issue the command on its own, via another ‘do script “xyz” in window 1′.
Other than that, this worked like a charm! Thanks so much! =)