2

I have an M1 MacBook Pro and I often connect it to an external display. I would like to automatically unhide the dock and menu bar when my MacBook Pro is connected to the external display, and hide them when my MacBook Pro is not connected to the external display. I would like to accomplish this using AppleScript or anything else.

Here's what I tried.

-- Check if an external display is connected
tell application "System Events"
    set displays to name of every desktop
end tell

if (count of displays) > 1 then
    -- External display is connected, hide the Dock and menu bar
    tell application "System Events"
        set autohide of dock preferences to true
        set UI elements enabled to true
    end tell
else
    -- External display is not connected, unhide the Dock and menu bar
    tell application "System Events"
        set autohide of dock preferences to false
        set UI elements enabled to false
    end tell
end if

The line set UI elements enabled to false keeps throwing this: error "System Events got an error: Can’t set UI elements enabled of application to false." number -10006 from UI elements enabled of application

What is the syntax for writing such a script? Are there any built-in functions in macOS that can help me accomplish this task? Are there any third-party libraries or resources that could be helpful? How can I set up the script to run automatically when the external display is connected or disconnected? Is this even possible?

2 Answers 2

1

In your script, replace the following,

set UI elements enabled to false

with this

set autohide menu bar of dock preferences to false

This should now hide the menubar. Just change the false to true for the other line.

I found this answer here

5
  • That worked beautifully. Now do you know how I can get my mac to listen to those events?
    – himom
    Mar 22, 2023 at 7:13
  • As far as I could tell, you would need to have a program running to detect when the monitor is connected. I found this on here. apple.stackexchange.com/questions/333556/…
    – ErniePC12
    Mar 22, 2023 at 14:07
  • Hmmm seems a bit taxing..
    – himom
    Mar 23, 2023 at 2:18
  • A small app, running in the background checking the status of the displays in a given interval, probably not as taxing as you might image. But it seems that there is nothing at this point, at least that I am aware of, that will fire an action when the displays increase by 1.
    – ErniePC12
    Mar 23, 2023 at 15:35
  • Based on my experience, it appears that hammerspoon (talk.automators.fm/t/…) did not function properly. Nonetheless, I have a functional applescript file that I run when I connect my monitor, which is at least satisfactory for the time being.
    – himom
    Mar 25, 2023 at 15:11
0

If you'd like to skip the pain of figuring out the AppleScript incantation and how to automate it, there's a free app that can do per-display dock configuration called HiDock.

2
  • Sorry, I read "I would like to accomplish this using AppleScript or anything else." and thought "anything else" means that any other method would be good as long as it solves the problem. Am I misunderstanding this? Mar 21, 2023 at 20:07
  • Thank you, @AlinPanaitiu, for your suggestion. However, it appears that the application is limited to managing the dock and has some issues. Specifically, the minimum size for the dock within the application does not correspond to the minimum size within the system settings. I appreciate your input, but please note that I am seeking a solution that can effectively provide the desired functionality.
    – himom
    Mar 21, 2023 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .