4

次の質問は、この質問に投稿された回答に関連しています。

新しい端末を開く独自の関数を作成するという概念が好きなので、上記の質問でCraigWalkerがリンクしたスクリプトが私のニーズに合っていました。Mark Liyanageによって書かれたスクリプトは、ここにあります。

そのスクリプトは次のとおりです。

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

リンク先のサイトのスクリプトに1つ変更を加えました。冗長性を排除するために、「$ COMMAND$EXIT」を出力する行をコメントアウトしました。ただし、スクリプトを実行すると、この出力が得られます

tab 1 of window id 2835

新しいウィンドウが開き、渡したコマンドを実行する直前。なぜこれが発生するのか考えてみてください。(oascriptを呼び出す前にstderrのリダイレクトを/ dev / nullに移動しようとしましたが、違いはありませんでした。)

4

1 に答える 1

8

tab 1 of window 2835do scriptコマンドによって返されるオブジェクトのAppleScript表現ですtab。コマンドを実行するために作成されたインスタンスです。osascriptスクリプト実行の結果を標準出力に返します。AppleScriptスクリプトには明示的なものがないためreturn、スクリプト全体の戻り値は、最後に実行されたステートメント(通常はdo scriptコマンド)の結果です。2つの最も簡単な修正は、os​​ascriptのstdoutをリダイレクトすることです(エラーの場合はstderrをリダイレクトしないことが望ましい)。

osascript >/dev/null <<EOF

または、明示的なreturn(値なしの)AppleScriptに挿入します。

tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT"
end tell
return
于 2010-11-10T18:29:48.633 に答える