我写了一个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 :(得分: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
答案 1 :(得分: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分钟运行一次,直到您退出。