2014-03-13 23 views
0

我有一个AppleScript写入,它告诉终端执行脚本。在某个终端输出上触发的AppleScript

on run 

    tell application "Terminal" 
     set currentTab to do script ("...") 
    end tell 


end run 

但是我想触发它​​只在我从终端窗口得到确定的输出时才运行。

这样做的最佳方法是什么?

我想:

tell application "Terminal" 

     if contents of front window contains "..." then 
      say "match" 

     else 
      say "no match" 
     end if 

end tell 

但它似乎并没有工作。你会建议使用什么方法?

+0

你是如何运行脚本的? –

+0

目前通过Apple脚本编辑器启动它。整个过程在远程服务器上运行,理论上运行脚本可以每5分钟检查一次终端的状态并在需要时运行脚本。 – user3414803

回答

0

你的脚本只能运行一次,然后退出。您需要创建一个带空闲处理程序的stay open script,该处理程序定期检查终端内容。

例如:

on idle 
    tell me to run 
    return 5 * minutes --Runs the script every 5 minutes 
end idle 

当您保存文件时,选择Application作为文件类型,并检查Stay open after run handler选项。然后双击图标运行你的脚本。脚本将保持打开状态,每5分钟运行一次,直到您退出为止。

1

不创建应用程序,您需要将终端输出写入.txt或日志文件,然后读取它。

因此,定义txt文件:

set temp_file to quoted form of (POSIX path of (path to temporary items from user domain) & "logger.txt") 

然后创建文本文件,并写入端子输出:

try 
    do shell script "rm -f " & temp_file 
end try 
do shell script "touch " & temp_file 
do shell script "do something " & " | tee " & temp_file 

得到的AppleScript来读取文件,把每行到一个列表项:

set the_output to my get_lines() 
on get_lines() 

    set temp_file to (POSIX path of (path to temporary items from user domain) & "logger.txt") 
    set logs to paragraphs of (read temp_file) 
    set line_count to count of logs 

    set line_items to {} 
    repeat with i from 1 to line_count by 1 
     if (i + 0) is not greater than line_count then 
      set end of line_items to items i thru (i + 0) of logs 
     else 
      set end of line_items to items i thru line_count of logs 
     end if 
    end repeat 

    return line_items 

end get_lines 

找到你的“确定输出”并拉动触发器...

repeat with n from 1 to (count of the_output) 

    set str to text of (item n of the_output) as string 

    if str starts with "TEXT TO LOOK FOR" then 

     do something 

    end if 

end repeat