加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

macos – 如何摆脱这个osascript输出?

发布时间:2020-12-15 21:20:58 所属栏目:安全 来源:网络整理
导读:以下问题与 this question发布的答案有关: 我喜欢创建我自己的函数来打开一个新终端的概念,所以Craig Walker在上面引用的问题中链接的脚本符合我的需求.该剧本由Mark Liyanage撰写,发现于here. 那个脚本是这样的: #!/bin/sh## Open a new Mac OS X termina
以下问题与 this question发布的答案有关:

我喜欢创建我自己的函数来打开一个新终端的概念,所以Craig Walker在上面引用的问题中链接的脚本符合我的需求.该剧本由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; $@"
#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之前将stderr的重定向移动到/ dev / null,但这没有任何区别.)

解决方法

窗口2835的选项卡1是do script命令返回的对象的AppleScript表示形式:它是为执行命令而创建的选项卡实例. osascript将脚本执行的结果返回给标准输出.由于AppleScript脚本中没有显式返回,因此整个脚本的返回值是最后执行的语句的结果,通常是do script命令.最简单的两个修复方法是重定向osascript的stdout(最好不要在发生错误时重定向stderr):

osascript >/dev/null <<EOF

或者在AppleScript中插入显式返回(没有值).

tell application "Terminal"
    activate
    do script with command "$COMMAND $EXIT"
end tell
return

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读