我怎样才能摆脱这个 osascript 输出?

发布于 2024-10-01 13:50:49 字数 1762 浏览 2 评论 0原文

以下问题与发布在 这个问题

我喜欢创建自己的函数来打开新终端的概念,因此克雷格·沃克在上述问题中链接到的脚本适合我的需求。该脚本由 Mark Liyanage 编写,位于此处。< /a>

该脚本是这样的:

#!/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,但这没有什么区别。)

The following question relates to an answer that was posted on this question:

I like the notion of creating my own function that opens a new terminal, so the script that Craig Walker linked to in that above-referenced question suited my needs. The script, written by Mark Liyanage, is found here.

That script is this:

#!/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

I made one change to the script on the linked site; I commented out the line that outputs "$COMMAND $EXIT" to eliminate some verbosity. However, when I run the script I still get this output

tab 1 of window id 2835

just before it opens the new window and executes the command that I pass in. Any ideas why this would be happening? (I tried moving the redirect of stderr to /dev/null before the call to oascript, but that made no difference.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-10-08 13:50:49

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

osascript >/dev/null <<EOF

或者将显式的return(没有值)插入到AppleScript。

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

tab 1 of window 2835 is the AppleScript representation of the object returned by the do script command: it is the tab instance created to execute the command. osascript returns the results of the script execution to standard output. Since there is no explicit return in the AppleScript script, the returned value of the whole script is the result of the last-executed statement, normally the do script command. The two easiest fixes are to either redirect stdout of the osascript (and preferably not redirect stderr in case of errors):

osascript >/dev/null <<EOF

or insert an explicit return (with no value) into the AppleScript.

tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT"
end tell
return
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文