كيف يمكنني التخلص من إخراج Osascript هذا؟
سؤال
يتعلق السؤال التالي بالإجابة التي تم نشرها هذا السؤال:
يعجبني فكرة إنشاء وظيفتي الخاصة التي تفتح محطة جديدة ، وبالتالي فإن البرنامج النصي الذي يرتبط به كريج ووكر في هذا السؤال المرجع أعلاه يناسب احتياجاتي. تم العثور على السيناريو ، الذي كتبه مارك لياناج هنا.
هذا السيناريو هو:
#!/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
قبل أن يفتح النافذة الجديدة مباشرة وينفذ الأمر الذي أعمره. أي أفكار لماذا يحدث هذا؟ (حاولت تحريك إعادة توجيه STDERR إلى /dev /null قبل الدعوة إلى OASCRIP ، لكن ذلك لم يحدث فرقًا.)
المحلول
tab 1 of window 2835
هو تمثيل applescript للكائن الذي تم إرجاعه بواسطة do script
الأمر: إنه tab
تم إنشاء مثيل لتنفيذ الأمر. osascript
إرجاع نتائج تنفيذ البرنامج النصي إلى الإخراج القياسي. لأنه لا يوجد صريح return
في البرنامج النصي Applescript ، تكون القيمة التي تم إرجاعها للنص الكامل هي نتيجة البيان آخر تنفيذي ، عادة do script
يأمر. إن أسهل الحلول هو إما إعادة توجيه stdout من Osascript (ويفضل ليس إعادة توجيه stderr في حالة الأخطاء):
osascript >/dev/null <<EOF
أو أدخل صريح return
(مع عدم وجود قيمة) في Applescript.
tell application "Terminal"
activate
do script with command "$COMMAND $EXIT"
end tell
return