Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get rid of this osascript output?

The following question relates to an answer that was posted on this question:

I like the notion of creating my own function that opens a new terminal, so the script that Craig Walker linked to in that above-referenced question suited my needs. The script, written by Mark Liyanage, is found here.

That script is this:

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

I made one change to the script on the linked site; I commented out the line that outputs "$COMMAND $EXIT" to eliminate some verbosity. However, when I run the script I still get this output

tab 1 of window id 2835

just before it opens the new window and executes the command that I pass in. Any ideas why this would be happening? (I tried moving the redirect of stderr to /dev/null before the call to oascript, but that made no difference.)

like image 564
barclay Avatar asked Feb 26 '23 21:02

barclay


1 Answers

tab 1 of window 2835 is the AppleScript representation of the object returned by the do script command: it is the tab instance created to execute the command. osascript returns the results of the script execution to standard output. Since there is no explicit return in the AppleScript script, the returned value of the whole script is the result of the last-executed statement, normally the do script command. The two easiest fixes are to either redirect stdout of the osascript (and preferably not redirect stderr in case of errors):

osascript >/dev/null <<EOF

or insert an explicit return (with no value) into the AppleScript.

tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT"
end tell
return
like image 152
Ned Deily Avatar answered Mar 08 '23 00:03

Ned Deily