質問

次の質問は、投稿された答えに関するものです この質問:

新しい端末を開く独自の関数を作成するという概念が好きなので、その上に参照された質問でリンクしたスクリプトが私のニーズに合っています。 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に移動しようとしましたが、それは違いはありませんでした。)

役に立ちましたか?

解決

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

osascript >/dev/null <<EOF

または明示的なものを挿入します return (価値なし)applescriptに。

tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT"
end tell
return
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top