简体   繁体   English

Applescript或Shell脚本关闭OSX中的每个未使用的终端窗口

[英]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). 我有一个在OSX的UI用户会话中运行的应用程序(在终端窗口中运行的Mono应用程序,一个命令行程序)。 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" 有时,这个mono应用程序会出于某种原因退出,并且启动时每2分钟运行一次shell脚本,以检查ps ax | grep "mono" ps ax | grep "mono" only returns the grep command as the only mono process. ps ax | grep "mono"仅将grep命令作为唯一的mono进程返回。 If so, spawn a new terminal window and in that terminal window run the mono program again. 如果是这样,请生成一个新的终端窗口,然后在该终端窗口中再次运行mono程序。

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. 我想要的是要么追加到此Shell脚本,要么追加到Applescript,可以在我的mono应用程序中调用它以关闭每个不运行mono进程的终端窗口。 Can Applescript determine if a terminal window is running a certain program? Applescript可以确定终端窗口是否正在运行某个程序吗? Can shell determine that? Shell可以确定吗? If so, how? 如果是这样,怎么办?

How about this. 这个怎么样。

Change processCheckName to your process 将processCheckName更改为您的流程

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? 我看了这篇文章: osascript-如何使用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.) (我真的认为,关闭所有不忙的窗口是最好的。假设您有一个工作窗口,则可以为该窗口分配一个“特殊标题”,从而使用if测试将其排除在外。关闭(大多数时候可能不忙)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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