使用Applescript在Safari中单击陷阱鼠标

时间:2013-09-24 01:09:03

标签: safari onclick applescript

我试图在Safari中使用Applescript捕获鼠标点击事件。我的Applescript做了它需要做的事情,但是如果用户点击Safari中的文档我想终止。我想在Applescript中设置一个On_Click事件,如果用户点击Safari文档,它将触发。

2 个答案:

答案 0 :(得分:0)

当聚焦窗口变为普通浏览器窗口时,您可以退出脚本,例如测试最前面的窗口是否有全屏按钮:

tell application "System Events" to tell process "Safari"
    repeat until exists value of attribute "AXFullScreenButton" of window 1
        --do something
        delay 1
    end repeat
end tell

或者,当Safari成为最前面的应用程序时,脚本应该停止:

repeat until frontmost of application "Safari"
    --do something
    delay 1
end repeat

答案 1 :(得分:0)

我不知道如何使用applescript“捕获”鼠标事件。你必须使用objective-c和cocoa API。

但是你可能会采取另一种方式。这是一个简单的例子。将此保存为保持打开的AppleScript应用程序。当您运行它时,Safari窗口将滚动。要暂停滚动,只需单击applescript的停靠图标即可。如果再次单击停靠图标,则滚动将再次开始,反之亦然。您会注意到,每次单击停靠栏图标时,都会执行“on reopen”处理程序,该处理程序将shouldScroll变量切换为true和false。

无论如何,我希望这能让你有一个让你自己的脚本工作的想法。请记住,您可以通过右键单击其停靠栏图标并选择退出来退出保持打开的AppleScript。祝你好运。

property shouldScroll : missing value
property theCounter : missing value
property counterMaxValue : missing value

on run
    tell application "Safari" to activate
    set shouldScroll to true
    set theCounter to 0
    set counterMaxValue to 2000
end run

on reopen
    tell application "Safari" to activate
    set shouldScroll to not shouldScroll
end reopen

on idle
    if shouldScroll then
        if theCounter is greater than counterMaxValue then set theCounter to 0
        tell application "Safari" to tell document 1
            do JavaScript "window.scroll (0," & (theCounter as text) & ")"
        end tell
        set theCounter to theCounter + 20
    end if
    return 0.1
end idle