【问题标题】:How to close a Terminal tab using AppleScript?如何使用 AppleScript 关闭终端选项卡?
【发布时间】:2014-12-12 23:59:20
【问题描述】:

我正在使用 AppleScript 在终端选项卡中打开 PostgreSQL,如下所示:

#!/bin/bash

function new_tab() {
  TAB_NAME=$1
  COMMAND=$2
  osascript \
    -e "tell application \"Terminal\"" \
    -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
    -e "do script \"printf '\\\e]1;$TAB_NAME\\\a'; $COMMAND\" in front window" \
    -e "end tell" > /dev/null
}

new_tab "PostgreSQL" "postgres -D /usr/local/var/postgres"

从终端运行此脚本将打开一个新选项卡,其中包含 PostgreSQL 服务器。所以在执行结束时,我将有 2 个选项卡:第一个用于运行脚本,第二个包含服务器。

如何关闭第一个?

这是我的尝试:

osascript -e "tell application \"Terminal\" to close tab 1 of window 1"

但我收到此错误消息:

执行错误:终端出现错误:窗口 1 的选项卡 1 没有 理解“关闭”信息。 (-1708)

【问题讨论】:

  • 唉,终端的脚本界面是由小猫眼泪和发霉的袜子组成的。 (它的 make 命令也不起作用。)按照下面的建议使用 GUI 脚本,或者给自己一个更好的终端仿真应用程序,例如 iTerm。

标签: applescript osascript


【解决方案1】:

你可以试试这样的:

tell application "Terminal"
    activate
    tell window 1
        set selected tab to tab 1
        my closeTabOne()
    end tell
end tell


on closeTabOne()
    activate application "Terminal"
    delay 0.5
    tell application "System Events"
        tell process "Terminal"
            keystroke "w" using {command down}
        end tell
    end tell
end closeTabOne

【讨论】:

    【解决方案2】:

    一种方法是这样的:

    osascript \
        -e "tell application \"Terminal\"" \
        -e "do script \"exit\" in tab 1 of front window" \
        -e "end tell" > /dev/null
    

    但终端必须配置为在 shell 退出时关闭窗口。

    有人有不需要这样做的解决方案吗?

    【讨论】:

      【解决方案3】:

      这将只关闭活动标签:

      tell application "Terminal" to close (get window 1)
      

      【讨论】:

        【解决方案4】:

        要确定选项卡的窗口,您可以解析尝试访问选项卡仍然不存在的窗口属性时收到的错误消息。错误消息通常包含窗口的 id,您可以使用它来引用窗口。

        由于您的问题已有 5 年的历史,我将使用可以在脚本编辑器中运行的示例代码而不是难以阅读的 bash 脚本来完成我的回答。

        tell application "Terminal"
        
            -- Perform command
            set theTab to do script "echo 'Hello World'"
        
            try
        
                -- Try to get the tab's window (this should fail)
                set theWindow to window of theTab
        
            on error eMsg number eNum
        
                if eNum = -1728 then
        
                    (*
                        The error message should look like this:
                        Terminal got an error: Can’t get window of tab 1 of window id 6270.
                    *)
        
                    -- Specify the expected text that comes before the window id 
                    set windowIdPrefix to "window id "
        
                    -- Find the position of the window id
                    set windowIdPosition to (offset of windowIdPrefix in eMsg) + (length of windowIdPrefix)
        
                    -- Get the window id (omitting the period and everything else after)
                    set windowId to (first word of (text windowIdPosition thru -1 of eMsg)) as integer
        
                    -- Store the window object in a variable
                    set theWindow to window id windowId
        
                else
        
                    -- Some other error occurred; raise it
                    error eMsg number eNum
        
                end if
        
            end try
        
            close theWindow
        
        end tell
        

        【讨论】:

          【解决方案5】:

          我认为关键是获取前窗的id。

          tell application "Terminal"
              do script "pwd"
              set myID to id of front window
              close window id myID
          end tell
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2014-11-22
            • 2013-12-24
            • 2018-09-22
            • 1970-01-01
            • 2020-09-12
            • 1970-01-01
            • 2022-10-15
            相关资源
            最近更新 更多