2010-11-10 98 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