Macos 将新函数传递给osascript命令

Macos 将新函数传递给osascript命令,macos,osascript,Macos,Osascript,我有更改终端标题的功能。我希望将其传递给osascript命令 我尝试: function title { name=hostname printf "\033]0;$name %s\007" "$1" } osascript -e "tell application \"Terminal\"" \ -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \ -e "

我有更改终端标题的功能。我希望将其传递给osascript命令

我尝试:

function title {
  name=hostname
  printf "\033]0;$name %s\007" "$1"
}

osascript -e "tell application \"Terminal\"" \
    -e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
    -e "do script \"title newTitle \" in front window" \
    -e "end tell"
    > /dev/null;
它不起作用

有什么想法吗


谢谢

您的
.bash\u配置文件
中是否定义了您的函数? 如果是这样的话,这应该是可行的(见下文)。如果没有,您需要添加它,否则您创建的新选项卡不知道该函数

不带
$1
替换的多行脚本可以放在单引号中而无需转义

osascript -e 'tell application "Terminal"
tell application "System Events" to keystroke "t" using {command down}
do script "title NewTitle" in front window
end tell'
由于启动新选项卡时生成的文本,
/dev/null
似乎没有多大区别