首页 > 解决方案 > 仅在选中时如何取消选中菜单栏选项

问题描述

我目前正在尝试制作一个脚本,该脚本将隐藏工具栏以获得最佳全屏效果。问题是我无法仅在必要时隐藏工具栏,因此如果工具栏已经从以前的脚本使用中隐藏,它将再次显示工具栏。这是代码的那一部分。

tell application "Safari" to activate
tell application "System Events"
    tell process "Safari"
        if menu item "Hide Status Bar" of menu "View" of menu bar 1 exists then
            click menu item "Hide Status Bar" of menu "View" of menu bar 1
        end if
        if menu item "Hide Favorites Bar" of menu "View" of menu bar 1 exists then
            click menu item "Hide Favorites Bar" of menu "View" of menu bar 1
        end if
        if menu item "Hide Tab Bar" of menu "View" of menu bar 1 exists then
            click menu item "Hide Tab Bar" of menu "View" of menu bar 1
        end if
        if menu item "Always Show Toolbar in Full Screen" of menu "View" of menu bar 1 exists then
            click menu item "Always Show Toolbar in Full Screen" of menu "View" of menu bar 1
        end if
    end tell
end tell

任何人都可以帮忙吗?我在系统偏好设置中看到了一些关于实际复选框的内容,但这并不太奏效......

标签: macossafariapplescript

解决方案


假设Safari,根据问题上的safari 标签,并且仅在处于全屏视图时单击Always Show Toolbar in Full Screen 菜单项,则以下示例AppleScript代码将执行此操作。

示例 AppleScript 代码

tell application "System Events"
    tell application process "Safari"
        tell its menu "View" of menu bar 1
            if exists menu item "Always Show Toolbar in Full Screen" then
                if the value of attribute "AXMenuItemMarkChar" of ¬
                    menu item "Always Show Toolbar in Full Screen" is "✓" then
                    click menu item "Always Show Toolbar in Full Screen"
                end if
            end if
        end tell
    end tell
end tell

由于目标菜单项也存在于普通窗口中,尽管未启用,但这是处理它的另一种方法,因为它首先检查它是否已启用并被选中,如果是,请单击它以取消选中它:

示例 AppleScript 代码

tell application "System Events" to ¬
    tell application process "Safari" to ¬
        tell its menu "View" of menu bar 1 to ¬
            tell its menu item "Always Show Toolbar in Full Screen" to ¬
                if the value of attribute "AXEnabled" is true and ¬
                    the value of attribute "AXMenuItemMarkChar" is "✓" then click it

推荐阅读