2014-03-13 22 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