How to enable auto generation of Window MenuItems (e.g. Tile Window to Left/Right of Screen and Open file with checkmark) in macOS menubar - macos

I am currently creating a macOS menubar for an app without using any interface builder (no XIB/NIB files), just pure code. However I was expecting some items to be auto-generated during the start-up of the app. Items like "Start Dictation", "Emoji & Symbols" under Edit menu were existing as well as the "Enter Full Screen" menu item under the View Menu. But when it comes to Window Menu nothing was automatically generated, only the menu items I've set in the code. Do I have to enable some flags or options when instantiating a Window NSMenu so it automatically generates those items? I am new to macOS development so I feel like I am kind of lost. Thanks in advance.

The Window and Help menus are a little special in that they have their own NSApplication properties, so you will need to set them to your menus so that the system will know what they are.
For example, if you just create a window menu and add it to the main, all you will get are the items that you have provided. If you also set it as the application’s windowsMenu, in addition you will get all the stuff for moving, tab support, etc.
Setting NSApp’s helpMenu is similar, where a Spotlight menu item is added to the menu.

Related

Programmatically obtaining a Windows application's tray menu items

I have an application with a tray menu and I'm trying to automate some tests that involve the tray menu. Basically I need obtain the tray menu's items and do stuff with them.
However, I've only been able to find ways to programmatically obtain menu items for within the application. But my automation tests are going to be an external application, so that doesn't help me.
How can I obtain an external application's tray menu items programmatically?
There are ways to enumerate/access the tray icons themselves (usually involving hooking into the notification tray itself, or using UI Automation), but there is no way to access a popup menu that appears when a tray icon is clicked on. The reason is because the icon's owning application receives a message when the click occurs and then acts accordingly, which usually involves displaying its own popup menu. There is no menu associated with the icon itself.
For what you are attempting, you would have to enumerate the icons and figure out which icon belongs to the app you are interested in (not a trivial task on its own), then simulate a click on the icon so the app displays its popup menu. See the following question for some details:
Finding and simulating a click on a system tray icon?
Interacting with the popup menu once it is displayed will be more difficult. You won't have access to the menu itself. You will likely have to resort to just issuing mouse events via mouse_event() or SendInput() to move the mouse cursor over the menu and click its items (assuming they appear in predictable locations relative to the icon).
If you can obtain the icon's HWND+ID or GUID (by hooking the notification tray itself), you can use Shell_NotifyIconGetRect() to get the icon's coordinates, at least.
How can I obtain an external application's tray menu items programmatically?
You cannot. There is no public API that provides access to notification icons.
Depending on what sort of assumptions you find acceptable, you can programmatically interact with the taskbar button's menu once it's visible. The image below shows the Inspect SDK tool reporting properties on the OneNote clipping tool button's menu. (And the menu items say they support the UIA Invoke Patten, so they should be programmatically invokable by UIA client code.)
If you want to invoke your tray button's menu items, you might consider the following steps using UIA. You may feel the assumptions that I make here are unacceptable for your situation.
Find the element with a class name "NotifyIconOverflowWindow", that's a direct child of the root menu. I'm assuming the button is in the overflow area.
Enumerate the children of the overflow element, looking for a button with the name of your button. This assumes the UI language is known and accounted for.
Get the bounding rect of the button and simulate a mouse right-click on the button. The click simulation is necessary because I'll bet the UI doesn't support IUIAutomationElement3::ShowContextMenu(), (but you could always try it).
Once the context menu's up, find the element with a ControlType a Menu, a Name of "Context", that's a direct child of the root element.
Once you have the menu, enumerate the child elements in the menu to find the items, and do what you want with them. Eg get the menu item's Invoke pattern and invoke it.

Display NSPopover above dock icon

How can I display an NSPopover above my app's dock icon?
There are two ways to customise the dock menu, based on if your application is running or not. See the Apple documentation for customising it when its open here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/docktasks_cocoa/docktasks_cocoa.html#//apple_ref/doc/uid/TP30000986-CH3-SW1
To summarise, you basically provide a menu in the main application's XIB or storyboard which contains the extra menu options that you want to add to the dock menu.
To customise your Dock icon's menu when the application is not running, see the guide here:
http://developer.apple.com/library/mac/#documentation/Carbon/Conceptual/customizing_docktile/CreatingaDockTilePlug-in/CreatingaDockTilePlug-in.html
Basically, you need to create a plugin which you drop into your application's resource folder. This works very like the original method, but executes inside Finder's dock process.

Adding static items to "Windows" menu in Cocoa application

I have a application with 2 windows (think like Twitter Timeline and mentions). For now I have been having the menus "Timeline ⌘1" and "Mentions ⌘2" in the "File" menu but this is kind of wrong. So I moved them to the "Windows" menu just by drag and drop.
When I open the application everything is nice, I can see both menu items unter the "Windows" menu. But then when I close for example the Mentions window the menu Item "Mentions ⌘2" disappears and I am not able to open this window anymore.
I have had a look at other applications and most of them have their Menu items to show the windows in the "Windows" menu and they don't disappear either after closing the windows. I am aware of the fact that Cocoa does soma magic with the "Window" menu item and it is quite ok in doing so (because I open some other windows which need to appear and disappear from there), but how can I make those two items kind of static so they don't disappear?
I finally found an answer, in order for them to not be handled by the Windows menu you need to call
[timelineViewWindow setExcludedFromWindowsMenu:YES];
[mentionsViewWindow setExcludedFromWindowsMenu:YES];
and after that add the Menu items to the Windows menu and they will not disappear anymore.

System is adding unwanted items to my customized contextual menu in my app

I have customized the contextual menu for my Cocoa application such that only certain items are visible. In spite of my customization, I found that the system adds a menu item -- "Add to iTunes as a spoken track".
I tried removing this item from the menu but somehow, I am unable to get the control. Is there a way this item can be removed, or do I have to write an AppleScript to disable the iTunes option under Keyboard Shortcuts? I hate doing that since I will then have to restore it for the user.
You can control the contents of these menus (system-wide) by using Preferences.
See System Preferences > Keyboard > Keyboard Shortcuts > Services > Text
In my experience, Apple's default items trigger off of the exact title of the menu. For example, I have a toy app with an NSMenu that I am creating entirely in code (no nib). I find that the View menu gets an extra item (for full screen) if I initialize it as follows:
NSMenu* viewMenu = [[NSMenu alloc]initWithTitle:#"View"];
However, if I put an extra space into the name, then Apple doesn't mess with it:
NSMenu* viewMenu = [[NSMenu alloc]initWithTitle:#"View "];
Is there a way this item can be removed, or do I have to write an AppleScript to disable the iTunes option under Keyboard Shortcuts?
Don't fight with The System; those Service items are supposed to appear on every context menu, as specified in the System Preferences. It even appears on the context menu of Safari which I'm using to view this post right now. Yes I agree that having "add to iTunes" enabled by default is a poor choice on the part of Apple, but that's life.
Maybe the user has his/her own service item s/he installed say as an Automator action. In that case the user probably doesn't want to have it removed.

How can I get to the menu bar in my app in interface builder

XCode works in mysterious ways (at least to me).
I simply want to create a Preference pane in my app. When I run my app, the stock menu bar comes up (Apple, MyApp, File, Edit...) and the "Preferences" menu item is grayed out. It makes sense since I haven't started playing with it.
How on earth do I add/remove/activate/inactivate menu items? I'm not talking about adding anything new, simply using what should be there.
Thanks in advance.
NSMenu has "Auto Enable Items" enabled by default. That means if the menu item does not have it's action message hooked up, it will appear grayed out. So in your case, you would simply set the Preferences menu item's "Sent Action" to whatever action shows your preferences window. This can be hooked to some sort of showPreferencesWindow: method of your AppDelegate, or directly to the showWindow: method of a window controller.
To dynamically enable/disable menu items the best way is to implement the NSUserInterfaceItemValidation protocol which is excellently documented here
Edit: Your app's menu bar items live in the MainMenu.xib file. The menu bar appears as a "Main Menu" object on the left hand side (if you're using Xcode 4) Simply click on the items to modify them, and you can Ctrl+drag connections to and from them like any UI object.

Resources