如何使用 AppleScript 单击 Safari 工具栏扩展按钮(全屏时)

How to click a Safari toolbar Extension button using AppleScript (when in full screen)

我正在尝试编写一个小的 AppleScript,它将单击 OneNote 工具栏扩展,因此我可以将它绑定到键盘快捷键。只要 Safari 实例处于 windowed 模式,下面的脚本就可以完美运行,但在全屏模式下它会失败并且 returns 错误:

error "System Events got an error: Can’t get window 1 of process \"Safari\". Invalid index." number -1719 from window 1 of process "Safari"

我已将此脚本导出为应用程序并授予对其的访问权限。实际上,全屏 Safari window 似乎没有索引或不再是 window,现在是一个不同的对象。

tell application "System Events"
 tell process "Safari"
    click button whose description contains "OneNote" of toolbar 1 of window 1
 end tell 
end tell

MacBook Pro(视网膜显示屏,13 英寸,2013 年末) OS X El Capitan,版本:10.11.6 (15G1004)

试试这个脚本。无论 Safari 是否处于全屏模式,它都有效:

set myButton to "OneNote"

tell application "System Events"
    tell process "Safari"
        set isfullscreen to value of attribute "AXFullScreen" of window 1
        if isfullscreen is true then
            click ((buttons of toolbar 1 of group 1 of window 1) whose description contains myButton)
        else
            click ((buttons of toolbar 1 of window 1) whose description contains myButton)
        end if
    end tell
end tell

将 "OneNote" 替换为您要单击的任何按钮。