macdevcenter.com
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

Mac Modding Shortcuts
Pages: 1, 2

Adding Shortcuts with Nonstandard Characters

As you may have noticed, some Mac OS X applications use an ellipsis (…) as part of their menu items. When you see an ellipsis tacked onto a menu item, it means that the item will open up another window in which you’ll have more work to do. Many of Calculator’s menus, for example, contain items that end with an ellipsis. Unfortunately, there are two ways that programmers can add an ellipsis to a menu item, and you have to take this into account when you define your keyboard shortcuts. Some programmers actually type in three dots, a triple repetition of the period (.) character. Other programmers use the Unicode ellipsis character, which is a single character that adds all three dots (...). It can be very hard to differentiate these visually, unless you know what you’re looking for.



Curiously enough (and rather conveniently, from the viewpoint of this technical author), Calculator 3.1 uses both approaches. If you launch Calculator and carefully look at the File and Convert menus, you’ll discover that the File menu uses three dots and the Convert menu uses an ellipsis, as shown in Figure 8-9. The dots are slightly heavier and spaced a little further apart than the ellipsis character. Convert menu uses the ellipsis character.

File menu uses 3 dots Figure 8-9. Version 3.1 of Apple’s Calculator application uses both dots and the ellipsis character in its menus.
File menu uses 3 dots Figure 8-9. Version 3.1 of Apple's Calculator application uses both dots and the ellipsis character in its menus.

In the following steps, you’ll use the Keyboard Shortcuts preferences pane to set shortcuts for both types of ellipsis.

  1. If you started up Calculator to check out the differences between the ellipses in the File and Convert menus, you’ll need to quit (c-Q) before going forward with this example.

    WARNING
    You should never modify an application that’s currently running. The application won’t recognize the changes until it relaunches, and it may overwrite the changes you’ve made by exporting its current set of preferences when the application quits.
  2. Launch System Preferences by clicking on its icon in the Dock or by choosing a ? System Preferences.
  3. Click on the icon for the International preferences panel; the International icon looks like the flag for the United Nations.
  4. Click on the tab for the Input Menu; it is the rightmost of the three buttons near the top of the International pane.
  5. Locate the Character Palette option and place a checkmark next to it. (The “Show input menu in menu bar” option at the bottom of the window automatically checks itself when the Character Palette is in use.) Checking this option lets you access the Character Palette from a Finder menu. The Input Menu, headed by the flag of your current localization settings, appears in the menu bar, as shown in Figure 8-10. You’ll need the Character Palette to type the Unicode ellipsis characters in as your keyboard shortcut.

    Figure 8-10. Use the International
    Figure 8-10. Use the International preferences Input Menu pane to provide quick access to the Character Palette.

  6. Choose View ? Show All Preferences (c-L) to return to System Preferences’ main display, which shows all of the available preferences panels.
  7. Click on the icon for the Keyboard & Mouse preferences panel. When the Keyboard pane appears, click on the tab for the Keyboard Shortcuts pane.
  8. Below the list of keyboard shortcuts, click on the plus sign (+) button; the Keyboard Shortcut specification dialog appears.
  9. Click on the Application pop-up menu and select the Calculator application.
  10. Enter “Page Setup…” in the Menu Title text field. There are three periods that follow “Setup” to form its ellipsis.
  11. In the Keyboard Shortcut text field, hold down the Option key and press P to assign Option-P as the keyboard shortcut for “Page Setup…”.
  12. Click on the Add button, as shown in Figure 8-11. The new shortcut appears in Application Keyboard Shortcuts under Calculator.

    Figure 8-11. Adding a shortcut using
    Figure 8-11. Adding a shortcut using three period characters.

  13. Click on the + button again; a new Keyboard Shortcut dialog opens.
  14. Select Calculator from the Application pop-up. (In all likelihood, after setting the Page Setup shortcut, the Application pop-up will default to Calculator.)
  15. Type “Area” in the Menu Title text field. Leave the cursor where it is, to preserve the focus in the text field; we’re going to use the Character Palette to add the Unicode version of the ellipsis character after “Area”.
  16. Click on the Input Menu in the menu bar, and select the Character Palette; the palette opens in a separate window.

    Mac OS X’s Character Palette lets you add the full range of Unicode characters to your applications and documents. The Character Palette is indispensable when it comes to working with foreign languages and special fonts. To learn more, search for “typing special characters and symbols” in Mac Help. Choose Help -> Mac Help (Command-?) from the Finder.
  1. In the Character Palette, choose Roman from the View pop-up at the top of the palette.
  2. Beneath the View pop-up menu, you’ll see two tabbed panes: “by Category” and “Favorites.” Click on the “by Category” tab.
  3. On the left side of the “by Category” view, select the Punctuation category. To the right of the category list appear the Unicode characters for various punctuation marks. In the top row, click on the ellipsis character (…). At the bottom of the Character Palette, you’ll see the ellipsis displayed in an array of fonts; don’t worry about this right now, but keep it in mind for when you might want or need a character in a different font.
  4. At the bottom of the Character Palette, click on the Insert button, as shown in Figure 8-12. Mac OS X adds the ellipsis to the Menu Title text field. Close the Character Palette or move it out of the way.
    Figure 8-12. The ellipsis character Figure 8-12. The ellipsis character appears in the Punctuation set of the Roman character palette.
  5. Tab down to the Keyboard Shortcut text field. Hold down the Option key and press A to assign Option-A as the keyboard shortcut.
  6. Click the Add button. The new shortcut joins the list, as shown in Figure 8-13.

    Figure 8-13. The shortcuts defined by
    Figure 8-13. The shortcuts defined by these steps.

  7. Return to the Applications folder and launch Calculator by double-clicking on its icon.
  8. In Calculator, open the File and Convert menus to confirm that your keyboard shortcuts have been added correctly, as shown in Figure 8-14. Recall that the shortcuts will not appear unless the menu item text exactly matches the text you typed in the Keyboard Shortcuts preferences pane. Enter the shortcuts (Option-A and Option-P) to test that they work as expected.
  9. Quit Calculator (c-Q).
  10. Open a new Terminal window. You’ll use this new window to test how your actions affected the Calculator defaults.
  11. Enter defaults read com.apple.calculator | grep UserKey. This command searches for Calculator’s keyboard shortcuts:

    $ defaults read com.apple.calculator | grep UserKey
        NSUserKeyEquivalents = {"Area\\U2026" = "~A"; "Page Setup..."
    = "~P"; "Speak Total" = "~T"; };

    Figure 8-14. The updated Calculator replaces the File -> Save Tape As menu shortcut with Shift-c-S and adds Option-A as the Convert -> Area shortcut.
    Figure 8-14. The updated Calculator replaces the File -> Save Tape As menu shortcut with Shift-Command-S and adds Option-A as the Convert -> Area shortcut.

    As you can see, the Page Setup shortcut simply uses the three typed dots. In contrast, the Area shortcut uses the Unicode ellipsis character U2026. This code is found on the second page of the Unicode standard code charts (http://www.unicode.org/charts/charindex2.html; the list starts at http://www.unicode.org/charts/ charindex.html), as shown in Figure 8-15. Unicode escape sequences allow you to add and edit exotic equivalents via your keyboard without using Mac OS X’s Character Palette.

    Figure 8-15. The Unicode site allows you to look up the Unicode equivalent for many common symbols.
    Figure 8-15. The Unicode site allows you to look up the Unicode equivalent for many common symbols.

    You won’t see this spelled-out Unicode shortcut if you open Calculator’s plist file (~/Library/ Preferences/com.apple.calculator.plist) in TextEdit or Property List Editor, because TextEdit can read and display Unicode natively. As Figure 8-16 shows, the ellipsis looks like an ellipsis in TextEdit.

    Figure 8-16. TextEdit is fully Unicode compliant and displays special symbols directly.
    Figure 8-16. TextEdit is fully Unicode compliant and displays special symbols directly.

  12. Return to the Keyboard Shortcuts preferences window.
  13. Select the Calculator heading above the new shortcuts.
  14. Click the minus (–) button to remove Calculator and its shortcuts from the Application Keyboard Shortcuts list. Be careful; you cannot undo this action.

WARNING
You might be tempted to define a keyboard shortcut in System Preferences and then modify the shortcut by editing the resulting application preferences file. Do NOT. System Preferences will overwrite your changes every time you log in, reverting the shortcuts to the ones stored in its database.

Adding Global Application Shortcuts

Keyboard shortcuts can apply to individual applications or across the board to every application on the system. When you choose a particular application, as you did in the previous example, the shortcut affects only the domain that you specify; you selected Calculator, and the shortcuts were then mapped to that application only. The Keyboard Shortcut preferences also allow you to define shortcuts for All Applications. When you do this, the keyboard shortcut is added to the global domain (also called the NSGlobalDomain), defining a shortcut that affects all matching menu items in all applications. The following example shows you how to create a global shortcut.

  1. Launch the System Preferences application by clicking on its icon in the Dock or by choosing a ? System Preferences.
  2. Click on the Keyboard & Mouse preferences panel and then go to the Keyboard Shortcuts pane.
  3. Look at System Preferences’ Edit menu, and confirm that the Clear option doesn’t have a keyboard shortcut.
Figure 8-17. Adding a global  keyboard shortcut. Figure 8-17. Adding a global keyboard shortcut.
  1. Back in the Keyboard Shortcuts pane, click on the plus-sign (+) button to add a new shortcut.
  2. Set the Application pop-up menu to All Applications.
  3. Enter “Clear” in the Menu Title text field, and then assign Control-X (^X) as the shortcut in the Keyboard Shortcut field.
  4. Click Add (Figure 8-17). The new Clear shortcut appears under Application Keyboard Shortcuts ? All Applications.
  5. Close the System Preferences window (c-W), and then launch System Preferences again by clicking on its icon in the Dock or choosing a ? System Preferences.
  6. Look at the System Preferences Edit menu; you’ll see that the Clear option now includes the ^-X keyboard shortcut. Launch another Mac OS X application (such as Excel) and look at the Edit menu to confirm that the keyboard shortcut for the Clear option was applied globally.
  7. Launch Terminal and start a new shell. Enter the following defaults command. This command reads all the preferences from the global domain and searches for any keys that include UserKey.

    $ defaults read NSGlobalDomain | grep UserKey 
        NSUserKeyEquivalents = {Clear = "^X"; };

    Instead of typing out NSGlobalDomain, you can use the defaults -g flag; for example, try using defaults read -g | grep UserKey.
  8. Return to System Preferences and to the Keyboard Shortcuts preferences pane.
  9. Select the Clear shortcut and click on the minus (–) button to remove the shortcut.
Erica Sadun has written, co-written, and contributed to almost two dozen books about technology, particularly in the areas of programming, digital video, and digital photography.


Return to MacDevCenter.com.