首页 > 解决方案 > 如何使用 AppleScript 关闭 Safari 偏好设置窗口?

问题描述

我正在使用以下脚本来启用开发人员菜单。

tell application "Safari"
    tell application "System Events"
        tell process "Safari"
            click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1
            click button "Advanced" of toolbar 1 of window 1
            click checkbox "Show Develop menu in menu bar" of group 1 of group 1 of window 1
            -- delay 2
            keystroke "w" using {command down}  -- > not working
        end tell
    end tell
end tell

如何关闭首选项窗口?有了keystroke "w" using {command down},我就不能The document cannot be closed while the script is running.出错了。


还有如何仅在未启用时启用复选框?目前,如果我运行脚本两次,它会切换。

标签: macosapplescript

解决方案


此 AppleScript 代码适用于我使用最新版本的 macOS Mojave。

tell application "System Events"
    tell application process "Safari"
        set frontmost to true
    end tell
    keystroke "." using {command down}
end tell

推荐阅读