【问题标题】:Open and close Terminal windows with applescript使用 applescript 打开和关闭终端窗口
【发布时间】:2018-09-22 13:12:07
【问题描述】:

我正在远程主机上运行一个实验,为此我有一个 shell 脚本来执行一些任务,调用 osascript 来启动几个终端窗口来执行一些任务,一些在 ssh 会话中,然后关闭。

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

我的问题是是否有更好的方法来做到这一点,因为我必须始终打开这个带有 6 个选项卡的终端窗口,这并不能让我同时执行其他任务

下面是我的sn-p

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

'

提前谢谢你

【问题讨论】:

    标签: macos shell terminal applescript


    【解决方案1】:

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

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

    '> /dev/null' 抑制标准输出; '2>&1' 抑制 stderr(具体来说,将 stderr 发送到与 stdout 相同的位置),并且尾随的 '&' 将命令置于后台。

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

    如果您想收到答案:

    set theAnswer to do shell script myScript
    

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

    【讨论】:

      猜你喜欢
      • 2010-12-20
      • 2013-07-24
      • 2013-05-22
      • 2011-12-10
      • 1970-01-01
      • 2014-07-20
      • 2020-08-15
      • 2022-12-11
      • 1970-01-01
      相关资源
      最近更新 更多