3

I'm working on MacOS menu bar app which can have variable length:

@IBOutlet weak var statusMenu: NSMenu!

let statusItem: NSStatusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)

statusItem.title = "Long text..."
statusItem.menu = statusMenu

If the status item title length will be to long to display and the menu bar item will hide I'd like to change the title to the shortened version. So I need:

  1. check if statusItem is visible
  2. if no, check how much free space I have to adjust the shortening
  3. change the title

Point 3 is easy. I have problems with point 1 and 2. None of the stackoverflow answers work for me. Is there any way how this can be done?

Tom
  • 369
  • 2
  • 14
  • Hi tom !, interesting issue, when I took a look at the apple docs, looks title like is deprecated, according to the documentation it states that: `Instances are created by the NSStatusBar method statusItem(withLength:), which automatically adds the new status item to the status bar. The appearance and behavior of the status item are then set using the button property.` can you try using the button property instead and see if that fixes the issue ? here's the [documentation link](https://developer.apple.com/documentation/appkit/nsstatusitem) – Mostafa Berg Oct 26 '18 at 07:17
  • Hi @MostafaBerg . Thanks for mention that it's maked as deprecated. Unfortunately the problem is not in the way how I set the title. button I checked the button property, and can't find there any way how to check is the statusItem visible (.button.isVisible don't work) – Tom Oct 26 '18 at 09:17
  • Well ‘isVisible’ will probably won’t work in this case, do you have a screenshot of both states so I can take a look ? – Mostafa Berg Oct 26 '18 at 11:57
  • Text visible: https://ibb.co/dH8nkq, text too long, invisible: https://ibb.co/jKqdrV – Tom Oct 28 '18 at 15:41
  • Do you have control over the order ? If you quit Skype and start it again does it’s icon go to the left side of your status item ? – Mostafa Berg Oct 28 '18 at 17:45
  • It goes to the left site. Without private api it's not possible (?) to programmatically change the elements order (ak priority). – Tom Oct 29 '18 at 08:25
  • That’s correct, but when it goes to the left, is there a huge empty space where the string should be ? Or does it shrink again to a small box ? – Mostafa Berg Oct 29 '18 at 08:26
  • If the skype icon is on the left and there is not enough space for my app which is on the right both skype and my app are hidden. It looks like the currently focused application menu calculates how much space it require and if the space is bigger than available it starts to hide menubar icons starting from the left one until there will be enough free space. I wonder is there a way how to calculate the size of the currently focused application menu? – Tom Oct 29 '18 at 11:10

0 Answers0