Disabling Menu Item - objective-c

One of my e-books explains how to add a new menu item. Well, that's not very difficult to figure out. It's not difficult to figure out how to link an IBAction to a new menu item, either. But this book and Google search results don't explain how to disable a menu item. More particularly, I want to disable Preferences and Quit, depending on where window currently appears. If the application currently shows the Preferences window, I want to disable these menu items.
Suppose that I have an IBOutlet named preferencesMenu and then that I have
[preferencesMenu setEnabled:NO];
, that won't disable the menu item in question. So how do you disable a particular menu item?
Thank you for your advice.
Tom

NSMenuItems disable themselves automatically only if both their target and selector are nil and NULL respectively.
[preferencesMenu setTarget:nil];
[preferencesMenu setAction:NULL];
Which a quick test reveals leads to this:

Just set the target to nil. I'll do the job.
[preferencesMenu setTarget:nil];

Related

Removing tabs in tabcontrol and using designated buttons to switch between tabs

I'm currently working on an application for Windows, however, I have one small problem: I can't seem to figure this one out...
Is there a way to remove the tab headers from tab control and designate other buttons to switch between tabs? I'm going for a more modern look and the default tabs in tab control are not at all what I'm interested in.
thanks for your answers on this question!
I've just thought of a different method to keep the clean look of my program without having to get too complicated with code.
For anyone wondering about this, you could set different buttons to hide and show different things, for example:
Under homeBtn you could have code that shows the information shown on the page by default, yet at the same time, you could also hide any information from the previous tab.
Thanks, Laugh
You can easily add buttons setting the property SelectedIndex on the tab control to switch the pages. To hide the tab headers, there are some ideas over here.

What type of rollover menus does Apple use in their Contacts App?

Does anyone know how Apple creates their rollover menus in the Contacts "business card" view*? I am trying to replicate that interface in an App I'm writing, but I'm not sure how to get there via XCode. I'm currently running Mavericks...
Thank you in advance!
* i.e. the "work" heading for an address is a context-sensitive popup menu that allows you to copy the address, open in maps, etc...
I wasn't able to replicate the Contacts interface exactly (I suspect they are doing some custom drawing routines), but I came up with something that was good enough for me. I created pulldown buttons with the "gradient" style, and visual set to "bordered".
Here is the key: during startup, I call the following:
[myButton setShowsBorderOnlyWhileMouseInside:true];
When you hover over the button, it highlights. Click, and the menu drops down. Perfect!

Objective-C: get menu information from external application

I'm trying to write a Mac OS menu extra application that displays a contextual menu containing the currently active application's menu bar items, when the user presses some hotkey. The displaying of the contextual menu I can do fine, but I can't seem to get the currently active application's menu bar items. At the moment I'm using [[[NSWorkspace sharedWorkspace] runningApplications] filteredArrayUsingPredicate:] to get the active applications' name, but NSRunningApplication seems to contain precious little other information. Is there any way I can get information about application menus from an external application?
UPDATE:
Using the ScriptingBridge framework seems to work fairly well, if you're happy using AppleScript:
SystemEventsApplication* sevApp = [SBApplication applicationWithBundleIdentifier:#"com.apple.systemevents"];
SystemEventsProcess* proc = [[sevApp applicationProcesses] objectWithName:appName];
for (SystemEventsMenuBar* menuBar in proc.menuBars) {
for (SystemEventsMenuBarItem* menuBaritem in menuBar.menuBarItems) {
NSLog(#"%#", menuBaritem.name);
}
}
will print out a list of menus available from the application's menu bar. Haven't found a way to get the contextual menu, so I won't call this answered just yet...
This was useful too: https://robnapier.net/scripting-bridge
You can use AppleScript to simulate clicking a menu item like shown here, but I'm not sure if it's possible to dynamically grab the names of all the menu items, to use that method you need to already have the names hardcoded into the app.

Document-based-like application + updating fields/menus/etc when switching document?

I have an application with multiple documents opened at once (as different tabs), but not implemented the Cocoa way, with NSDocument etc
So, let's say, that application is a text editor : e.g. an NSTextView in each document/tab and a menu (in the MainMenu) with options (on/off) related to that particular document.
Scenarios :
The user clicks a menu item (option) and the option is applied to the
current tab
Now, the user switches tab and the options (of the menu) should be update according to current document's settings
How should I go about that in the most Cocoa-friendly way?
(I certainly CAN do it; though I'm interested in what could be a more efficient way (and my definitely isn't))
You can implement validateUserInterfaceItem: on NSDocument. Menu and toolbar items call it to check if they should be enabled or not. Read about Implementing Validation in general and Enabling Menu Items specifically.

Closing frontmost window in Cocoa in an app without a menu bar

I am building a StatusBar App in Cocoa, therefore I have no menu. Having no menu implies not having a "File > Close" menu item, which normally listens to the shortcut "Command + W".
From my StatusBar App the user may open a window to change the preferences and that's where I'm running into problems: The user can only close the window by pressing the red dot with the mouse. However, like alle applications I want to support the "Command + W" shortcut as well.
At the moment I see two possibilities to solve this issue:
Place an invisible button on the window which listens to the shortcut.
Add an application-wide listener for the shortcut and contact the window manually.
Both solutions feel like a misuse of the system. The first solution can lead to unexpected behaviour (the window closes if the user hits the invisible button by chance) and the second solution will still result in a beep, since the window does not know that it handles such a shortcut.
Is there an elegant way to solve this problem? Since the view should know what to do, a solution with just Interface Builder would be perfect. If there is no elegant way, is there a way to enhance the solutions mentioned?
Thanks in advance!
If you put a File > Close menu item in your MainMenu nib, the shortcut will work, even though the menu isn't visible.
If you choose to implement an app-wide listener for the shortcut instead, you can get rid of the beep by returning nil from the block, so that the original event doesn't get passed on.