我怎样才能摆脱这个 osascript 输出?
以下问题与发布在 这个问题:
我喜欢创建自己的函数来打开新终端的概念,因此克雷格·沃克在上述问题中链接到的脚本适合我的需求。该脚本由 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
tab 1 of window 2835
是do script
命令返回的对象的 AppleScript 表示:它是为执行该命令而创建的tab
实例。osascript
将脚本执行的结果返回到标准输出。由于AppleScript脚本中没有显式的return
,因此整个脚本的返回值是最后执行的语句(通常是do script
命令)的结果。两个最简单的修复方法是重定向 osascript 的 stdout(最好不在出现错误时重定向 stderr):或者将显式的
return
(没有值)插入到AppleScript。tab 1 of window 2835
is the AppleScript representation of the object returned by thedo script
command: it is thetab
instance created to execute the command.osascript
returns the results of the script execution to standard output. Since there is no explicitreturn
in the AppleScript script, the returned value of the whole script is the result of the last-executed statement, normally thedo script
command. The two easiest fixes are to either redirect stdout of the osascript (and preferably not redirect stderr in case of errors):or insert an explicit
return
(with no value) into the AppleScript.