簡體   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