Hammerspoon: Menubar item that is specific to one display

Created on 31 Mar 2020  路  8Comments  路  Source: Hammerspoon/hammerspoon

I'd like to use Hammerspoon to display the currently active space on each monitor. For this, I need to display a separate menubar item on the menu bar of each display. I can also imagine other scenarios where information should only be displayed on one monitor in a multi-monitor setup.

All 8 comments

Adding a menu to just one screen isn't possible with Hammerspoon (I'm not even positive its possible in macOS at all, but definitely not with the Hammerspoon module), but what you could do is use a callback function with hs.menubar:setMenu and in that callback function return a different table based on the value of hs.screen.mainScreen.

Something along the lines of this should give you the idea:

~lua
mb = hs.menubar.new():setTitle("test"):setMenu(function(mods)
local sc = hs.screen.mainScreen():name()
if sc == "Color LCD" then -- or whatever other logic you want to differentiate the screens
return { ... } -- menu for laptop display
else
return { ... } -- menu for other displays
end
end)
~

@asmagill - There might be some private APIs that can do this?

https://github.com/w0lfschild/cleanMenuBar/blob/master/CleanMenuBar/NSStatusItem.h#L44

@latenitefilms I'm not going to have a chance to play with that any time soon, but you're welcome to give it a shot, if you like. I will caution that NSStatusBar and NSStatusItem have had breaking changes with their private methods somewhere around 10.12 (wrt to positioning) so while many private methods seem to be more or less persistent, these may not be.

Thanks for your answer! Do I understand correctly that the code snippet would show the value for the currently focused display on the status bars of all displays?

There is an app called WhichSpace that creates a separate menuitem for each display. However, it hasn't been maintained for 3 years and did not work when I installed it. Maybe it's still useful to see how it was done there?

Are you sure WhichSpace creates a seperate menuitem for each display? It looks like there's only a single menuitem, which shows the space number (i.e. if you're in space 1, then all screens will display "1" in the menuitem).

(edit, not sure how it got munged, but I've cleaned it up, I hope)

The above code snippit allows you to change whats in the menu when you click on it, based on the current screen (monitor, not space) not it's image or title.

You still won't be able to change the title/image on each different screen (monitor), but you could take a look at hs.screen.watcher.newWIthActiveScreen which will trigger a function call whenever the active screen changes. You could change the title/image in there.

Re spaces, Apple has NEVER officially released an API for manipulating/accessing information about them. And they've broken what people figured out on there own with macOS updates more than once...

But, you can take a look at hs.spaces.watcher.new which will fire off a function whenever the space changes... that's it, no reliable identifier or number, just that it has changed.

You can also check out an unsupported module I wrote a few years back that tries to use some of the undocumented spaces API's that have been discovered, but... I don't have time to support it much these days and some people report that some of the functions aren't working with the latest macOS versions. I've not seen any issues, but I'm using it very minimally. At any rate, the module can be found at hs._asm.undocumented.spaces and a sample that is similar to what I'm using myself can be found at https://github.com/szymonkaliski/Dotfiles/blob/ae42c100a56c26bc65f6e3ca2ad36e30b558ba10/Dotfiles/hammerspoon/utils/spaces/dots.lua

Okay, thanks again.

I'm using yabai which gives me all the information I need to display (i.e. all screens, how many open windows they have, what display they are attached to, and which space is currently focused).

The key problem for me is to find a way to show a different menu item in the status bar of each display. This way, I can show the spaces that are assigned to that display.

@latenitefilms, do you think the link you shared earlier can help with this?

Feel free to continue the discussion about possible workarounds, but I'm going to close this issue because I'm pretty sure it's not possible to have a menubar item that only appears on one display. Sorry!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

luckman212 picture luckman212  路  4Comments

agzam picture agzam  路  3Comments

jasonrudolph picture jasonrudolph  路  4Comments

franzwr picture franzwr  路  3Comments

piskov picture piskov  路  4Comments