El Capitan: Hide / Auto-hide menu bar with KM?

In El Capitan there is the new preference:
General > "Automatically hide and show the menu bar"
How can this be accessed in Keyboard Maestro? (Presumably through Applescript?)

Thanks.

Adding to my previous question. For activating the dock's autohide one invokes the Applescript:

tell application "System Events" to set the autohide of the dock preferences to true

Is there a similar piece of code for the menu bar ?

Hey RL,

You can answer that yourself by looking at the System Events dictionary in the Script Editor and searching for ‘auto’.

But no. I don’t find anything related.

That means that unless there’s a defaults item that can be written from the shell that the only way to automate it is with System Events and GUI-Scripting.

-Chris

------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/10/08 14:50
# dMod: 2015/10/08 15:03
# Appl: System Preferences & System Events
# Task: Toggle Auto-Hide/Show Menu Bar
# Tags: @Applescript, @Script, @System_Events, @System_Preferences, @Auto, @Hide, @Show, @Menubar
------------------------------------------------------------

tell application "System Preferences"
  if not running then run # Works around 'activate' bug.
  activate
  reveal pane id "com.apple.preference.general"
end tell
tell application "System Events"
  tell application process "System Preferences"
    tell (first window whose subrole is "AXStandardWindow")
      tell checkbox "Automatically hide and show the menu bar"
        if its value = 0 then
          click
        else if its value = 1 then
          click
        end if
      end tell
    end tell
  end tell
end tell
tell application "System Preferences" to quit

------------------------------------------------------------
2 Likes

Thanks Christopher for your reply.
I have dabbled in quite a few languages, but am not at ease with Applescript.
Before your message I had adapted the following code, from general GUI-scrpiting code I found.

 # Turn Auto-hide ON for the Menu Bar
----------------    
tell application "System Preferences"
     activate
     set current pane to pane "General"
end tell
delay 1
tell application "System Events"
    tell process "System Preferences"
        tell window "General"
            set theCheckBox to checkbox "Automatically hide and show the menu bar"
            tell theCheckBox
                if its value = 0  then click
            end tell
        end tell
    end tell
end tell
tell application "System Preferences" to quit

You will probably agree that this whole GUI-Scripting feels like a kludge. There ought to be a better way.

Three minor remarks/questions:

  • I wasn’t aware of the ‘activate’ bug. Can you explain?
  • For my own version, I found it necessary to insert the “delay
  • Is there a way to prevent the “System Preferences” window from showing on screen?

Hey RL,

For several versions of OSX now the activate verb in AppleScript won't necessarily bring the app to the front as it should. That bit of code helps.

I didn't have any problems yesterday, but I am today – so here's an updated version with some checking.

------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2015/10/08 14:50
# dMod: 2015/10/09 14:08
# Appl: System Preferences & System Events
# Task: Toggle Auto-Hide/Show Menu Bar
# Tags: @Applescript, @Script, @System_Events, @System_Preferences, @Auto, @Hide, @Show, @Menubar
------------------------------------------------------------  

set sysPrefApp to get path to application id "com.apple.systempreferences"
set generalPrefPane to alias ((path to system folder as text) & "Library:PreferencePanes:Appearance.prefPane:")
do shell script "open " & POSIX path of generalPrefPane

------------------------------------------------------------

set _cntr to 0
repeat while (get path to frontmost application) is not sysPrefApp and _cntr < 20
  delay 0.1
  set _cntr to _cntr + 1
end repeat
if _cntr = 20 then error "Script Failure!  Problem with activating General Pref-pane of System Preferences."

------------------------------------------------------------

tell application "System Events"
  tell application process "System Preferences"
    set frontmost to true
    tell (first window whose subrole is "AXStandardWindow")
      
      ---------------------------------------------------
      set _cntr to 0
      repeat while (enabled of checkbox "Automatically hide and show the menu bar" ≠ true) and _cntr < 20
        delay 0.1
        set _cntr to _cntr + 1
      end repeat
      if _cntr = 20 then error "Script Failure!  Problem with checkbox 'Automatically hide and show the menu bar'."
      ---------------------------------------------------
      
      tell checkbox "Automatically hide and show the menu bar"
        if its value = 0 then
          click
        else if its value = 1 then
          click
        end if
      end tell
      
    end tell
  end tell
end tell

------------------------------------------------------------

tell application "System Preferences" to quit

------------------------------------------------------------

Sort of.

You can use run or launch to keep it in the background.

-Chris

1 Like

Here is my script. The system preferences icon just bounces and that it.

tell application "System Preferences"
   reveal pane "com.apple.preference.general"
end tell
delay 0.1
tell application "System Events" to tell process "System Preferences"
   click checkbox "Automatically hide and show the menu bar" of window "General"
end tell
delay 0.1
tell application "System Preferences"
   quit
end tell
2 Likes

Hey Michael,

That's why I put a bunch of detection code in my script.

Yours actually works pretty reliably on my system though.

Which version of OSX are you using?

-Chris

1 Like

10.11, when I say thats it I mean it bounces and works :stuck_out_tongue:

2 Likes

According to http://apple.stackexchange.com/questions/209877/programmatically-auto-hide-menubar-in-el-capitan the defaults commands are:

To hide:

defaults write NSGlobalDomain _HIHideMenuBar -bool true

To show:

defaults write NSGlobalDomain _HIHideMenuBar -bool false

But then the poster says that you have to logout/login to make it take effect, or killall Finder

Well, killall Finder didn’t work for me, nor did killall Dock or killall SystemUIServer, and there’s no way that I want to logout/login to get this to work, so right now I think the GUI Scripting is the best solution we have, unfortunately.

1 Like

Can I run these separately in the AppleScript editor? I ask because they look like Terminal commands?

They are shell script commands. Generally, you can run any shell script in AppleScript using this method:

set cmdStr to "your shell script"
set ssResults to do shell script cmdStr

Of course, if your shell script contains any double-quotes, you will need to escape them:

set cmdStr to "your shell script that has some \"text in quotes\" and more"
set ssResults to do shell script cmdStr

For more info, see Calling Command-Line Tools .

This AppleScript below worked but I had to go back to Bartender because I needed control of what apps remained visible.

tell application "System Preferences"
reveal pane id "com.apple.preference.general"
delay 1
tell application "System Events"
	click checkbox "Automatically hide and show the menu bar" of window "General" of process "System Preferences"
end tell
quit

end tell