如何使用 AppleScript 关闭 Safari 首选项 window?

How to close Safari preference window using AppleScript?

我正在使用以下脚本启用开发者菜单。

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

如何关闭首选项window?使用 keystroke "w" using {command down},我得到了 cannot The document cannot be closed while the script is running. 错误。


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

只需点击第一个window的第一个按钮,勾选复选框value

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
            tell checkbox "Show Develop menu in menu bar" of group 1 of group 1 of window 1
                if value is 0 then click it
            end tell
            click button 1 of window 1
        end tell
    end tell
end tell

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

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