Search code examples
macosterminalapplescriptosascript

Select a particular tab in terminal depending upon the content using applescript and best practices


This is an application specific problem. I am trying to find and select a tab in Terminal.app depending on contents within. Here is what I'm doing:

tell application "Terminal"
    set foundTabs to (every tab of every window) whose contents contains "selectme"
    repeat with possibleTab in foundTabs
        try
            set selected of possibleTab to true
        end try
    end repeat
end tell

This isn't acting as expected and is pretty foolproof. I wonder if someone can suggest a way to do this with much less code (for instance, the looping shouldn't really be necessary, but applescript is an elusive language).

Thanks


Solution

  • Thing is, the following Applescript will do what you want, but unless your "selectme" string is very unique, you will find it in many tabs. But anyway, here you go:

    tell application "Terminal"
    set allWindows to number of windows
    
    repeat with i from 1 to allWindows
        set allTabs to number of tabs of window i
        repeat with j from 1 to allTabs
            if contents of tab j of window i contains "selectme" then
                set frontmost of window i to true
                set selected of tab j of window i to true
            end if
        end repeat
    end repeat
    end tell