March 2012

Find Terminal Sessions with Applescript

If you have so many terminals or terminal tabs open that you lose track of your sessions, this script can help you track them down.

It asks for a search term, which it matches against the name (title) of each tab, and optionally also the entire terminal contents. If there's more than one match you select them from the list (by a combination of the terminal title, and the last line of text found in the terminal). The selected terminal is raised to the front and switched to the appropriate tab.

set question to display dialog ("Find Terminal Session:") default answer "" buttons {"Cancel", "Search Titles", "Also Search Contents"} default button 2

set searchpat to text returned of question

set searchtype to button returned of question


if searchtype is equal to "Cancel" then return


tell application "Terminal"

set winlist to every window

set winmatchlist to {}

set tabmatchlist to {}

set tabnamematchlist to {}

repeat with win in winlist

set ok to true

try

set tablist to every tab of win

on error errmsg

--display dialog name of win as string

set ok to false

end try

if ok then

repeat with t from 1 to count of tablist

if searchpat is in (custom title of tab t of win as string) then

set end of winmatchlist to win

set end of tabmatchlist to t

set parlist to paragraphs of (contents of tab t of win as text)

set parnum to (count of parlist)

repeat while (length of item parnum of parlist = 0) and (parnum > 0)

set parnum to parnum - 1

end repeat

set partext to item parnum of parlist

if length of parlist > 50 then

set partext to characters 1 thru 50 of partext

end if

set end of tabnamematchlist to (custom title of tab t of win as string) & "  |  " & partext & " ." & (id of win as string) & "." & t

--display dialog name of t as string

else if searchtype is equal to "Also Search Contents" then

if searchpat is in (contents of tab t of win as text) then

set end of winmatchlist to win

set end of tabmatchlist to t

set parlist to paragraphs of (contents of tab t of win as text)

set parnum to (count of parlist)

repeat while (length of item parnum of parlist = 0) and (parnum > 0)

set parnum to parnum - 1

end repeat

set partext to item parnum of parlist

if length of parlist > 50 then

set partext to characters 1 thru 50 of partext

end if

set end of tabnamematchlist to (custom title of tab t of win as string) & "  |  " & partext & " ." & (id of win as string) & "." & t

end if

end if

end repeat

end if

end repeat

if (count of tabmatchlist) = 1 then

--display dialog "one!"

set w to item 1 of winmatchlist

set t to item 1 of tabmatchlist

set selected tab of w to tab t of w

set index of w to 1

set frontmost of w to true

activate

else if (count of tabmatchlist) = 0 then

display dialog "No matches"

else

set whichtab to choose from list of tabnamematchlist with prompt "The following tabs match, please select one:"

if whichtab is not equal to false then

set AppleScript's text item delimiters to "."

set tmp to reverse of text items of (whichtab as string)

set w to (item 2 of tmp) as integer

set t to (item 1 of tmp) as integer

set selected tab of window id w to tab t of window id w

set index of window id w to 1

set frontmost of window id w to true

end if

end if

end tell

To set this up, look in Applications, then Utilities, and run "AppleScript Editor". Cut and paste the above program into a new window there and save it as "Find Terminal Session" (or whatever name you like), and place it in your home directory, under Library/Scripts. Then in the AppleScript Editor Preferences, check the box "Show Script menu in menu bar (I also uncheck "Show Computer scripts", which would show lots of system scripts you might not want). You'll get a script menu in your menubar with your new script in it. Just select it to run it.

This script has an older brother which does roughly the same thing for Safari windows and tabs: Find Safari Tabs.

And if you're wondering how I converted the AppleScript Editor formatted program into HTML, I also do that with software. I can convert any copied text that has formatting into HTML.


Reader Comments (Experimental. Moderated, expect delays. Posts may be edited or ignored. I reserve the right to remove any or all comments, at any time.)

No comments

Add a comment


More Mac OS X Stuff


Tom Fine's Home Send Me Email