如何使用 Apple 脚本关闭终端 window
How to close a terminal window using Apple Script
背景
我正在使用 AppleScript 和 Bash 的组合来创建一个程序,该程序在 Terminal.app
然后在它终止时关闭 window。
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 "" ]; then
wd=""
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 不会在脚本本身完成时关闭 window。
我将您的代码复制并粘贴到一个空的文件中,保存并使其可执行。
然后我在终端中运行它并收到以下错误:
175:183: syntax error: A command name can’t go after this “my”. (-2740)
然后我将 close()
handler 的名称更改为 closeWindow()
并且 script 按预期工作.
请注意,close
是内置的 AppleScript 命令,不能用作 处理程序
的名称
背景
我正在使用 AppleScript 和 Bash 的组合来创建一个程序,该程序在 Terminal.app
然后在它终止时关闭 window。
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 "" ]; then
wd=""
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 不会在脚本本身完成时关闭 window。
我将您的代码复制并粘贴到一个空的文件中,保存并使其可执行。
然后我在终端中运行它并收到以下错误:
175:183: syntax error: A command name can’t go after this “my”. (-2740)
然后我将 close()
handler 的名称更改为 closeWindow()
并且 script 按预期工作.
请注意,close
是内置的 AppleScript 命令,不能用作 处理程序