IceGod
IceGod

Reputation: 61

How to uncheck menu bar option only if checked

I am currently trying to make a script that will hide the toolbar to have the best possible fullscreen. The problem is that I can't manage to only hide the toolbar when it is necessary, so that makes it so it will show the toolbar again if it was already hidden from a previous script usage. Here is that part of the code.

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

Can anyone help? I saw some stuff talking about actual checkboxes in system preferences but that didn't quite work...

Upvotes: 1

Views: 231

Answers (1)

user3439894
user3439894

Reputation: 7565

Assuming Safari, per the safari tag on the question, and are only going to click the Always Show Toolbar in Full Screen menu item when it is in Full Screen view, then the following example AppleScript code will do that.

Example AppleScript code:

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

Since the target menu item exists in a normal window too, albeit not enabled, here is another way to handle it as it first checks to see if it's enabled and also is checked, and if so clicks it to uncheck it:

Example AppleScript code:

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

Upvotes: 2

Related Questions