Mortennn / Dozer

Hide menu bar icons on macOS
Mozilla Public License 2.0
8.14k stars 256 forks source link

How to move status bar icons programmatically? #69

Open Mortennn opened 5 years ago

Mortennn commented 5 years ago

Does anyone know how to move status bar icons programmatically?

aonez commented 4 years ago

After seeing how Dozer hides icons using this:

static let hide: CGFloat = 10_000

I suppose the way to go might be some kind of AppleScript.

shumink commented 4 years ago

Also would like to hide icons programmatically

AndrewMast commented 4 years ago

This might be useful: https://github.com/shergin/NSStatusBar-MISSINGOrder

aonez commented 4 years ago

This might be useful: https://github.com/shergin/NSStatusBar-MISSINGOrder

@AndrewMast those do not respond (macOS 10.14), so that code is obsolete :(

[self respondsToSelector:statusItemWithLengthWithPrioritySelector] &&
[self respondsToSelector:insertStatusItemWithPrioritySelector]
gingerbeardman commented 4 years ago

I noticed some keys in the plist preference file for apps:

Thought A: NSStatusItem Preferred Position Item-0

What about reading and writing these keys in app preferences? I believe this is what macOS does when icons are rearranged using cmd+drag.

So I am thinking that even though all positions are "preferred", if Dozer manages all of these positions for all menu bar icons, then they should be granted their desired position without exception. Right?

We could read the keys to find out the preferred position of an icon, which after Dozer managed them all would be the actual position.

I have not checked how macOS refreshes the positions during cmd+drag, but it most certainly does not require app to restart themselves.

Searching GitHub for that, I found this shell script https://github.com/DaFuqtor/barhide that writes a key to an app preferences plist to hide menu bar icons.

Thought B: NSStatusItem Visible Item-0

Probably not good enough to be the way Dozer hides/shows icons.

Screen shot 2020-07-09 at 12 32 24

What do you think @Mortennn @aonez ?

Mortennn commented 4 years ago

Great research!

So I am thinking that even though all positions are "preferred", if Dozer manages all of these positions for all menu bar icons, then they should be granted their desired position without exception. Right?

This is actually a pretty clever thought. Could work. We would probably also need to figure out which process updates the menu bar icons after we have updated the plists.

As you say, thought B requires restart, so it is sadly not an option.

As a side note, Bartender requires accessibility access, so we might need to look in that direction.

gingerbeardman commented 4 years ago

Thought I'd start a list of what we may need to do:

...there are probably more!

gingerbeardman commented 3 years ago

Bartender showed me this in Big Sur:

Screen shot 2021-05-07 at 21 36 01

How do they do it?

dmattera commented 3 years ago

From what it looks like, it would appear that Bartender is indeed using some form of @gingerbeardman's "Thought A" where it manages the values of each app's NSStatusItem Preferred Position Item-n.

Prior to using Bartender to make any adjustments to an app I use which has 3 seperate NSStatusItem's, the value for each item was:

NSStatusItem Preferred Position Item-0 = 435.76 NSStatusItem Preferred Position Item-1 = 447 NSStatusItem Preferred Position Item-2 = 474.5

After changing the app's visibility setting in Bartender from the default "Show" to the new value of "Hide", those values were each immediately changed to :

NSStatusItem Preferred Position Item-0 = 20435.76 NSStatusItem Preferred Position Item-1 = 20447 NSStatusItem Preferred Position Item-2 = 20474.5 (increased by 20000)

So to "hide" the icons, it looks like they are really just moving them so far to the left on the menubar that they can't possibly be in view.

gingerbeardman commented 3 years ago

@dmattera How do those positions change if you rearrange those menu bar items? Both using Bartender and not would be useful to know.