【问题标题】:How can I get rid of this osascript output?我怎样才能摆脱这个 osascript 输出?
【发布时间】:2010-11-10 16:16:50
【问题描述】:

以下问题与this question 上发布的答案有关:

我喜欢创建我自己的函数来打开一个新终端的概念,因此 Craig Walker 在上面提到的问题中链接到的脚本适合我的需要。该脚本由 Mark Liyanage 编写,可在 here.

找到

那个脚本是这样的:

#!/bin/sh
#
# Open a new Mac OS X terminal window with the command given
# as argument.
#
# - If there are no arguments, the new terminal window will
#   be opened in the current directory, i.e. as if the command
#   would be "cd `pwd`".
# - If the first argument is a directory, the new terminal will
#   "cd" into that directory before executing the remaining
#   arguments as command.
# - If there are arguments and the first one is not a directory,
#   the new window will be opened in the current directory and
#   then the arguments will be executed as command.
# - The optional, leading "-x" flag will cause the new terminal
#   to be closed immediately after the executed command finishes.
#
# Written by Marc Liyanage <http://www.entropy.ch>
#
# Version 1.0
#

if [ "x-x" = x"$1" ]; then
    EXIT="; exit"; shift;
fi

if [[ -d "$1" ]]; then
    WD=`cd "$1"; pwd`; shift;
else
    WD="'`pwd`'";
fi

COMMAND="cd $WD; $@"
#echo "$COMMAND $EXIT"

osascript 2>/dev/null <<EOF
    tell application "Terminal"
        activate
        do script with command "$COMMAND $EXIT"
    end tell
EOF

我对链接站点上的脚本进行了一项更改;我注释掉了输出“$COMMAND $EXIT”的行以消除一些冗长。但是,当我运行脚本时,我仍然会得到这个输出

tab 1 of window id 2835

就在它打开新窗口并执行我传入的命令之前。任何想法为什么会发生这种情况? (在调用 oascript 之前,我尝试将 stderr 的重定向移动到 /dev/null,但这没有任何区别。)

【问题讨论】:

    标签: macos bash terminal osascript


    【解决方案1】:

    tab 1 of window 2835do script 命令返回的对象的AppleScript 表示:它是为执行命令而创建的tab 实例。 osascript 将脚本执行的结果返回到标准输出。由于 AppleScript 脚本中没有显式的return,因此整个脚本的返回值是最后执行的语句的结果,通常是do script 命令。两个最简单的修复方法是重定向 osascript 的标准输出(最好 not 重定向标准错误,以防出现错误):

    osascript >/dev/null <<EOF
    

    或在 AppleScript 中插入显式 return(没有值)。

    tell application "Terminal"
        activate
        do script with command "$COMMAND $EXIT"
    end tell
    return
    

    【讨论】:

    • 像魅力一样工作。原始脚本有 osascript 2>/dev/null
    猜你喜欢
    • 2021-01-27
    • 1970-01-01
    • 1970-01-01
    • 2012-10-11
    • 2022-10-18
    • 2020-01-05
    • 1970-01-01
    • 2020-10-24
    • 2011-03-12
    相关资源
    最近更新 更多