【问题标题】:Applescript or Shell Script to close every not-in-use terminal window in OSX用于关闭 OSX 中每个未使用的终端窗口的 Applescript 或 Shell 脚本
【发布时间】:2015-04-20 14:12:58
【问题描述】:

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

无论如何,有时这会导致大量终端窗口打开。我想要的是附加到这个 shell 脚本或者 Applescript 中,我可以在我的单声道应用程序中调用它来关闭每个不运行单声道进程的终端窗口。 Applescript 可以确定终端窗口是否正在运行某个程序? shell可以确定吗?如果有,怎么做?

【问题讨论】:

    标签: macos shell applescript


    【解决方案1】:

    这个怎么样。

    将 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
    

    【讨论】:

      【解决方案2】:

      我看了这个帖子: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 测试将其排除在关门了。(可能大部分时间都不忙。)

      【讨论】:

      • 不错的一个。但是您可以将系统事件调用替换为close aWn
      • 我试过了,关闭窗口,关闭选中的窗口,在测试的时候发现了一个bug,在测试tab的业务时出现了错误。跨度>
      • 应该是if not (get busy of aTab) then
      • 非常感谢马克,你说的完全正确,现在我在每个选项卡中执行了一个脚本,它也删除了它的 ui 脚本。 :)
      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 2014-07-31
      • 1970-01-01
      • 2013-05-03
      • 2013-11-18
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多