簡體   English   中英

如何使用 Apple Script 關閉終端窗口

[英]How to close a terminal window using Apple Script

背景

我正在使用AppleScriptBash的組合來創建一個程序,該程序在Terminal.app中啟動一組命令,然后在它終止時關閉該窗口。

terminal.sh

#!/bin/bash

# Usage:
#     terminal                   Opens the current directory in a new terminal window
#     terminal [PATH]            Open PATH in a new terminal window
#     terminal [CMD]             Open a new terminal window and execute CMD
#     terminal [PATH] [CMD]      Opens a new terminal window @ the PATH and executes CMD
#
# Example:
#     terminal ~/Code/HelloWorld ./setup.sh

[ "$(uname -s)" != "Darwin" ] && {
    echo 'Mac OS Only'
    return
}

function terminal() {
    local cmd=""
    local wd="$PWD"
    local args="$*"

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    if [ -d "$1" ]; then
        wd="$1"
        args="${*:2}"
    fi

    if [ -n "$args" ]; then
        cmd="$args"
    fi

    # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

    osascript <<EOF
tell application "Terminal"
    activate
    tell window 1
        do script "cd $wd ; $cmd"
        repeat while busy
            delay 1
        end repeat
        my close()
    end tell
end tell

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

terminal "$@"

問題

目前,AppleScript 不會在腳本本身完成時關閉窗口。

我將您的代碼復制並粘貼到一個空文件中,保存並使其可執行。

然后我在終端中運行它並收到以下錯誤:

 175:183: syntax error: A command name can’t go after this “my”. (-2740)

然后我將close()處理程序的名稱更改為closeWindow()並且腳本按需要工作。

請注意close是一個內置的AppleScript命令,不能用作處理程序名稱

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM