在继续编写脚本之前,如何等待特定文本出现在屏幕上?

时间:2015-10-08 10:15:19

标签: applescript

我是Applecript的相对新人,所以任何帮助都会非常感激!

我正在尝试让我的脚本仅在特定文本在网页上显示时运行。我一直在努力为我的查询寻求帮助,到目前为止已经干了。

谢谢!

2 个答案:

答案 0 :(得分:0)

AppleScripts无法监控事件,除非目标应用程序在其AppleScript字典中提供特定的事件处理程序。

解决方法是使用重复循环来定期监视所请求事件的状态,但这非常昂贵。

答案 1 :(得分:0)

您可以在stay-open applescript应用程序中使用idle handler。空闲处理程序允许代码以指定的间隔(以秒为单位)运行。 您可以从此代码开始。保存为应用程序,并将其检查为“运行后保持打开状态”。

property browserName : "Safari"
property searchString : "google"
property theDelay : 60  -- delay time in seconds

on run
    -- do any setup here
end run

on idle
    -- do your stuff here
    tell application "System Events" to set appRunning to ((name of processes) contains browserName)
    if appRunning then
        tell application "Safari" to set t to the text of document 1
    end if
    if t contains searchString then
        -- do stuff
        beep

    end if
    return theDelay
end idle

on quit
    -- do any cleanup necessary upon quit
    continue quit
end quit

另一种方法是使用OS X中的launchd实用程序以指定的时间或间隔运行脚本。这是一个很好的教程:http://nathangrigg.net/2012/07/schedule-jobs-using-launchd/

相关问题