简体   繁体   中英

Applescript or Shell Script to close every not-in-use terminal window in OSX

I have a an application that runs in a UI user session in OSX (a mono app that runs in a terminal window, a command line program). Sometimes this mono app exits for whatever reason, and a shell script that runs every 2 minutes as launchd checks to see if ps ax | grep "mono" ps ax | grep "mono" only returns the grep command as the only mono process. If so, spawn a new terminal window and in that terminal window run the mono program again.

Anyway, sometimes this leaves a large number of terminal windows open. What I would like is either to append to this shell script or perhaps in Applescript which I can call in my mono app to close every terminal window not running a mono process. Can Applescript determine if a terminal window is running a certain program? Can shell determine that? If so, how?

How about this.

Change processCheckName to your process

set processCheckName to "du"
tell application "Terminal"
    --activate
    set theWindows to every window of it
    repeat with thisWindows in theWindows
        set biglist to {}
        set theTabs to every tab of thisWindows
        repeat with i from 1 to number of items in theTabs
            set this_item to item i of theTabs
            set theProc to processes of this_item
            if theProc contains processCheckName then
                copy id of thisWindows to end of biglist
            end if
        end repeat
        if (id of thisWindows) is not in biglist then

            close thisWindows
        end if
    end repeat
end tell

I looked at this post: osascript - How to close a Terminal tab using AppleScript? - Stack Overflow

tell application "Terminal"
    activate
    set wns to every window of it
    repeat with aWn in wns

        set theTabs to every tab of aWn
        repeat with i from (count theTabs) to 1 by -1
            set aTab to item i of theTabs
            if not (get busy of aTab) then
                tell aWn to set selected tab to tab i of aWn
                do script "exit" in tab i of aWn
            end if
        end repeat
    end repeat
end tell

If the process isn't busy, then it obviously isn't running something at the moment, which was my take on this.

The problem is, that if your mono process is running in the foreground, the terminal being busy, then I guess an script you run, will be put on hold. What you can do, is to extract the output (text) of the terminal window, and look for your mono process there. (I really figured, closing all windows that weren't busy was the best. Say if you have a work window, then you can assign a 'special title' for that window, and thereby, using an if test to excempt it from being closed. (It is probably not busy most of the time.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM