Как закрыть окно терминала с помощью Apple Script - PullRequest
1 голос
/ 20 января 2020

Фон

Я использую комбинацию AppleScript и Bash, чтобы создать программу, которая запускает набор команд в 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 "$@"

Issue

В настоящее время AppleScript не закрывает окно после завершения самого скрипта.

1 Ответ

1 голос
/ 20 января 2020

Я скопировал и вставил ваш код в пустой файл , сохранил его и сделал его исполняемым.

Затем я запустил его в Terminal и получил следующую ошибку:

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

Затем я изменил имя обработчика close() на closeWindow(), а сценарий работал как нужно.

Обратите внимание, что close является встроенной AppleScript командой и не может использоваться в качестве имени обработчика

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...