使用applescript打开和关闭终端窗口

时间:2018-04-12 11:40:12

标签: macos shell terminal applescript

我正在远程主机中运行一个实验,并且我有一个执行某些任务的shell脚本,调用osascript启动几个执行某些任务的终端窗口,一些在ssh会话中,然后关闭。 / p>

现在为了做到这一点,我必须将终端窗口保持在上下文中,所以当它关闭时我可以按键终止然后接受下拉菜单询问我是否要退出(因为ssh打开)。如果我有例如safari打开并且正在浏览,则关闭浏览器。

我的问题是,是否有更好的方法来做到这一点,因为我必须始终保持这个终端窗口,其中有6个标签,打开,这不会让我自由地同时执行其他任务

贝娄是我的片段

osascript -e 'tell app "Terminal"
    do script ""
    delay 0.2
    do script "
/*do stuf inside ssh*/
    " in tab 1 of front window
    delay 0.2
    my makeTab()
    do script "
/*do stuf here*/
    " in tab 2 of front window
    delay 0.2
    my makeTab()
    do script "
/*do stuf here*/
    " in tab 3 of front window
    delay 0.2
    my makeTab()
    do script "
/*do stuf inside ssh*/
    " in tab 4 of front window
    my makeTab()
    do script "
/*do stuf here with tshark */
tshark -i en0 -f \"udp dst port 5683 or udp src port 5683\" -w sink.pcap
    " in tab 5 of front window
    my makeTab()
    do script "
/*do stuf here*/
    " in tab 6 of front window
    delay '$CLOSE_WINDOW_DELAY'
    my ending()    
end tell


on makeTab()
  tell application "System Events" to keystroke "t" using {command down}
  delay 0.5
end makeTab

on ending()
  tell application "System Events" to keystroke "w" using {command down, shift down}
  delay 0.9
  tell application "System Events" to keystroke return /*for the question due to ssh session*/
end ending

'

提前谢谢

1 个答案:

答案 0 :(得分:1)

如果您不需要接收和处理脚本的答案,这会更快 - 利用背景线程:

if (isset($_GET['act2']))
                    {
                        if (!isset($_GET['ip']))
                        {
                            echo '<div class="alert alert-danger">No log ID.  </div><meta http-equiv="refresh" content="2;url=?p=settings">';
                        }else{
                            $act = $_GET['act2'];
                            $tid = $_GET['ip'];
                            if (ctype_digit($tid))
                            {
                                if (ctype_alnum($act))
                                {
                                    $arr = array('blacklist');
                                    if (in_array($act, $arr))
                                    {
                                        $cnt = $odb->prepare("SELECT COUNT(*) FROM login_attempts WHERE id = :i");
                                        $cnt->execute(array(":i" => $tid));
                                        if ($cnt->fetchColumn(0) > 0)
                                        {


                                            $cpermss = $odb->prepare("SELECT privileges FROM users WHERE username = :u");
                                            $cpermss->execute(array(":u" => $_SESSION['username']) );
                                            $cperms = $cpermss->fetchColumn(0);
                                            if ($userperms == "moderator" && $cperms == "admin")
                                            {
                                                echo '<div class="alert alert-danger">Invalid Permissions.</div>';
                                            }else{
                                                if ($userperms == "user" && strtolower($cr) != strtolower($username))
                                                {
                                                    echo '<div class="alert alert-danger">Invalid Permissions.</div>';
                                                }else{
                                                    switch ($act)
                                                    {
                                                        case "blacklist":
                                                            $de = $odb->prepare("INSERT INTO 'blacklist'(ip) VALUES [':i']");
                                                            $de->execute(array(":i" => $tid));
                                                            echo '<div class="alert alert-success">ip added to blacklist.  </div><meta http-equiv="refresh" content="2;url=settings.php">';
                                                            break;
                                                    }
                                                }
                                            }
                                        }else{
                                            echo '<div class="alert alert-danger">Log not found in database.  </div><meta http-equiv="refresh" content="2;url=?p=settings">';
                                        }
                                    }else{
                                        echo '<div class="alert alert-danger">Invalid action.  </div><meta http-equiv="refresh" content="2;url=?p=settings">';
                                    }
                                }
                            }
                        }
                    }





if (isset($_GET['act']))
                    {
                        if (!isset($_GET['id']))
                        {
                            echo '<div class="alert alert-danger">No log ID.  </div><meta http-equiv="refresh" content="2;url=?p=settings">';
                        }else{
                            $act = $_GET['act'];
                            $tid = $_GET['id'];
                            if (ctype_digit($tid))
                            {
                                if (ctype_alnum($act))
                                {
                                    $arr = array('delete');
                                    if (in_array($act, $arr))
                                    {
                                        $cnt = $odb->prepare("SELECT COUNT(*) FROM login_attempts WHERE id = :i");
                                        $cnt->execute(array(":i" => $tid));
                                        if ($cnt->fetchColumn(0) > 0)
                                        {


                                            $cpermss = $odb->prepare("SELECT privileges FROM users WHERE username = :u");
                                            $cpermss->execute(array(":u" => $_SESSION['username']) );
                                            $cperms = $cpermss->fetchColumn(0);
                                            if ($userperms == "moderator" && $cperms == "admin")
                                            {
                                                echo '<div class="alert alert-danger">Invalid Permissions.</div>';
                                            }else{
                                                if ($userperms == "user" && strtolower($cr) != strtolower($username))
                                                {
                                                    echo '<div class="alert alert-danger">Invalid Permissions.</div>';
                                                }else{
                                                    switch ($act)
                                                    {
                                                        case "delete":
                                                            $de = $odb->prepare("DELETE FROM login_attempts WHERE id = :i");
                                                            $de->execute(array(":i" => $tid));
                                                            echo '<div class="alert alert-success">log deleted.  </div><meta http-equiv="refresh" content="2;url=settings.php">';
                                                            break;
                                                    }
                                                }
                                            }
                                        }else{
                                            echo '<div class="alert alert-danger">Log not found in database.  </div><meta http-equiv="refresh" content="2;url=?p=settings">';
                                        }
                                    }else{
                                        echo '<div class="alert alert-danger">Invalid action.  </div><meta http-equiv="refresh" content="2;url=?p=settings">';
                                    }
                                }
                            }
                        }
                    }

'&gt; / dev / null'抑制stdout; '2&gt;&amp; 1'抑制stderr(具体地说,将stderr发送到与stdout相同的位置),并且尾随'&amp;'将命令放在后台。

使用此后缀会立即将控制权返回给AppleScript,从而使shell进程在后台运行。因此,您可以同时启动不同的命令。

如果您想收到答案:

set myScript to "some shell command here"
do shell script myScript &"  > /dev/null 2>&1 &" 

在这种情况下,该过程将等待答案。