繁体   English   中英

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

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

我有一个在OSX的UI用户会话中运行的应用程序(在终端窗口中运行的Mono应用程序,一个命令行程序)。 有时,这个mono应用程序会出于某种原因退出,并且启动时每2分钟运行一次shell脚本,以检查ps ax | grep "mono" ps ax | grep "mono"仅将grep命令作为唯一的mono进程返回。 如果是这样,请生成一个新的终端窗口,然后在该终端窗口中再次运行mono程序。

无论如何,有时这会打开大量终端窗口。 我想要的是要么追加到此Shell脚本,要么追加到Applescript,可以在我的mono应用程序中调用它以关闭每个不运行mono进程的终端窗口。 Applescript可以确定终端窗口是否正在运行某个程序吗? Shell可以确定吗? 如果是这样,怎么办?

这个怎么样。

将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

我看了这篇文章: osascript-如何使用AppleScript关闭“终端”选项卡? - 堆栈溢出

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测试将其排除在外。关闭(大多数时候可能不忙)

暂无
暂无

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

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