|Home » Learning Curve
OS X Menus 2
Smashing Cocoa for fun and profit part two: application menus.
|For these exercises you will need:|
- the ADC developers tools, specifically Interface Builder;
- a good file browser, such as this one, or a certain dexterity with the Finder or Terminal; and
- a bit of patience.
Cocoa application menus are stored as editable on-disk resources. It's perfectly possible to make minor changes in these resources so they better match your computing style.
This example will take a look at altering an Apple Mail keyboard shortcut used in Jaguar and earlier; if you're running Panther already, you can still follow along and learn how it's done.
Cocoa application menus are contained in so-called 'NIBs' - NeXTSTEP Interface Builder files. NIBs are actually file packages: the cover item is a directory with the 'nib' extension, and within are up to three files with the following names.
Later versions of the developers tools (and OS X) allow for keyedobjects.nib instead of objects.nib: this is an XML-based file which serves the same purpose. Later versions of Interface Builder contain the option of storing data either way.
Although the latter is human readable, you're not about to edit this file manually unless you have a graduate degree in rocket science, and the former is almost always more compact. OS X can read both files, so which of these you use is totally up to you, regardless of what file your application was previously using.
OS X prior to Panther had no real standard keyboard shortcut for preferences. Most often it was '⌘,' but not always. The Apple Mail shortcut was, for example, ⌥⌘; - this may not have been to your liking, and if you'd taken David Pogue's advice and downloaded the developers tools, you'd have been able to do something about it.
The first part of the task is to locate the NIB within the Apple Mail package that contains the menu. As the Jaguar version contains no less than 34 NIBs, this could take some doing. So basically you either search around or you get clever. Getting clever is the better approach.
Remembering the crucial role of Info.plist from the previous tutorial, you might want to start there, and if you did, you'd be right on track. If you open Mail's Info.plist and look for the key 'NSMainNibFile' you'll find:
The 'main' NIB file is normally the one containing the menu.
Locate the NIB package 'MailViewer.nib' in your language's lproj directory and copy it out to a neutral location. When you get it there, inspect its contents. You should find that it contains one file - objects.nib. This file is all OS X needs to run Mail, but it is not enough for Interface Builder to edit it. But luckily that is a matter easily taken care of.
If you don't yet have the developers tools, do like David Pogue nags you to: download them now. Then fire up Interface Builder. You'll find it in /Developer/Applications after you install the tools.
Interface Builder will immediately prompt you with a dialog usurping the centre of your screen. Click 'New' in this dialog to initialise a new NIB, then save it to disk - the default name 'Untitled' is fine. Now exit Interface Builder and look at your new NIB package.
MailViewer.nib might have had only one file inside, but Untitled.nib has three. Move Untitled.nib's classes.nib and info.nib into the MailViewer.nib folder. And you can now delete the Untitled.nib package - you won't be needing it again.
Open MailViewer.nib in Interface Builder. You'll find a control window in the lower left area of your screen - it's the one with 'MailViewer.nib' in its title bar.
Locate the resource 'MainMenu' in this window under the first tab ('Instances'). Double-click it and your menu should appear. Its title bar should say 'MailViewer.nib - MainMenu'.
The preferences menu item is on the 'Application' menu. (In Apple Mail this menu bar item is not actually named 'Mail'; the system inserts the application's name at runtime.)
Notice the keyboard shortcut next to the menu item. Select the item and hit ⌘1 - this brings up an 'inspector' with configuration information for the menu item.
Note in the middle of this panel how the shortcut is set with a semicolon and '⌥' ('⌘' is implied - it's always set).
All you need to do is change the semicolon to a comma and remove the tick for '⌥'. The menu changes immediately.
You're done - for all practical purposes. Save your changes, go into MailViewer.nib, delete the classes.nib and info.nib files, then copy objects.nib to its original location:
Right beside MailViewer.nib you should now find 'MailViewer~.nib'. This should contain your original objects.nib. You might want to save this file for reference or for whenever if ever you want to go back again.
Exit Mail if it's running and start it again. Your preferences menu item should sport your new keyboard shortcut.
One of the minor shortcomings with the Camino browser has been the lack of a keyboard shortcut for preferences; using the above information, you should be able to add it on your own.
A lot of people irk themselves over the pathological use of textured windows - the 'brushed metal' interface. Fortunately, now that you're partially acquainted with Interface Builder, you can do something about this without having to resort to downloads for additional software.
When you've found the NIB containing the offending window, select it in Interface Builder and again hit ⌘1. You'll see the inspector come up as follows.
Notice the 'Textured Window' option at the very bottom. For 'brushed metal' windows this will be ticked on. Simply remove the tick and save. Your window will go back to the default look.