2010-11-10 99 views
3

下面的问题涉及被张贴在this question答案:如何摆脱这个osascript输出?

我喜欢创造我自己的功能,打开一个新的终端的概念,从而使克雷格·沃克挂在上面提到的问题,剧本适合我需要。该脚本,由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; [email protected]" 
#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之前标准错误重定向移动到/ dev/null,但其并没有区别。)

回答

7

tab 1 of window 2835是由do script命令返回的对象的AppleScript的表示:它是创建的tab实例执行命令。 osascript将脚本执行的结果返回给标准输出。由于AppleScript脚本中没有明确的return,因此整个脚本的返回值是最后执行语句的结果,通常为do script命令。最简单的两种修复程序是要么重定向osascript的标准输出(并且优选不重定向 stderr的在错误的情况下):

osascript >/dev/null <<EOF 

或插入一个明确return(没有值)插入的AppleScript。

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

工程就像一个魅力。原始脚本有 osascript 2>/dev/null << EOF 正在将stderr重定向到/ dev/null,这就是我为什么移动它的原因。我没想过尝试将常规输出重定向到/ dev/null ......谢谢! – barclay 2010-11-15 18:43:28