设想

我经常使用终端来进行SSH,但是希望终端应用程序在最后一个选项卡使用出口关闭时退出。我已经将终端窗口本身设置为成功的出口,但是终端保持运行。我跟随瑞奇·坎贝尔的教程 这里 它使用苹果订阅退出终端。 AppleScript在终端外运行时可以很好地工作,但是我以某种方式无法将陷阱命令放置在终端中。

作为参考,我将在此处发布相关文本:

现在,我们需要一种启动脚本的方法。这可以通过Bash命令“陷阱”来完成,让它聆听出口信号。由于我希望每次用户打开外壳时都会发生这种情况,因此我将其添加到.profile文件中。我和Nano一起做到这一点:

$ nano〜/.profile

在此文件中,添加以下位置。您可能需要编辑前面创建的AppleScript文件的路径:

陷阱'/usr/bin/osascript $ home/脚本/quitterminal.scpt'出口

所有这些都是聆听BASH中的退出信号,并且当它获取它时,运行我们之前创建的脚本。

问题

我如何解决此问题。由于某种原因,即使在重新启动后,陷阱也不会按照其方式发射或工作。是否有另一种方法可以使终端应用程序关闭时关闭?

解决方案

多里(Dori)提供的链接导致了解决方案。文章建议陷阱命令应在 .profile, ,实际上必须放入 .bashrc. 。将其移至正确的脚本解决问题。

有帮助吗?

解决方案

该方法 在这里给出 看起来有点简单 - 可能对您有用吗?

如果您不想记住键入“退出”而不是“退出”,而您正在使用bash,则只需将以下内容添加到您的 .bashrc 或其他Shell启动脚本:

trap '/usr/bin/osascript -e "tell application \"terminal\" to quit"' 0

它做了什么?当Shell收到信号0(零)即将被告知退出时,它将执行此命令作为最后一件事。这允许您的外壳等优雅地退出,并询问terminal.app通过applescript退出,确保其执行相同的操作。换句话说,键入“退出”,然后您的外壳退出,然后终端退出,全部均干净和自然意图的方式。

完整线可以在这里找到.

其他提示

您也可以使用此复杂的命令,该命令不会引发有关终止其过程的警告:

osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit

或者,您可以定义一个别名(例如 quit 或者 exit)在您的个人资料中:

alias quit='osascript -e "do shell script \"osascript -e \\\"tell application \\\\\\\"Terminal\\\\\\\" to quit\\\" &> /dev/null &\""; exit'

你不需要 trap 任何信号。

简而言之,将osascript命令放入您的 ~/.bash_logout 文件(创建它,如果不存在)

osascript -e "tell application \"Terminal\" to quit"

.bash_logout 登录后,由Bash执行。您可以在此文件中输入任何shell命令,每个命令在登录时将执行。

例如,您可以在最后一个文件夹中打开一个查找器窗口,然后退出之前 open . 或喜欢...

open .
say "Good bye, my master - leaving terminal now, continue with Finder"
osascript -e "tell application \"Terminal\" to quit"

等等...

我发现此URL适用于多标签终端情况。http://cyberdork33.wordpress.com/2010/05/25/make-osx-terminal-quit-when-last-window-close/
刚刚在OS X 10.8.2上进行了测试
TL.DR:
对于选项卡部分(退出至关闭当前选项卡)
终端>首选项>设置>外壳
当外壳退出时:如果外壳干净地退出(或关闭窗户),请关闭
关闭之前提示:永远不要(或仅当登录外壳以外的其他过程和:插入“ osascript”)

对于最后一个选项卡部分(退出以关闭最后一个选项卡和整个终端窗口,然后退出终端。
1)附加到 ~/.bash_profile:

trap '/usr/bin/osascript $HOME/Scripts/QuitTerminal.scpt' EXIT

2)创建脚本/quitterminal.scpt

tell application "Terminal"
    # If there is only one tab remaining, and it contains the word "logout"
    # then this is the final window
    if (count of (tabs of (every window whose visible is true))) = 1 then
        try
            set theContents to words of ((contents of tab 1 of window 1) as Unicode text)
            set exitLastTab to (theContents contains "logout")
        on error
            set exitLastTab to false
        end try

        if exitLastTab is true then
            quit
        end if

    else if (count of (tabs of (every window whose visible is true))) < 1 then
        # If no window remains open, then obviously we can quit the app.
        # This would occur when the final window is closed without 'exit'
        quit
    end if
end tell

希望能帮助到你。

许可以下: CC-BY-SA归因
不隶属于 apple.stackexchange
scroll top