Read Mac Hacks Online

Authors: Chris Seibold

Tags: #COMPUTERS / Operating Systems / Macintosh

Mac Hacks (18 page)

BOOK: Mac Hacks
13.14Mb size Format: txt, pdf, ePub
ads
Hack 25
. Speed Things Up with Keybindings

Keybindings
let you create keyboard shortcuts that are available in
almost any program where you can edit text.

Text input is a huge part of using any computer. Typing emails,
writing notes, even putting together books—it’s all part of every user’s
digital life. Fortunately, you can hack your system to make text-entry
easier. One of the most interesting and powerful of those is the
keyboard-shortcut system that OS X calls
keybindings
.
Keybindings assign keyboard shortcuts to commands. (If you’ve ever used
Command-C to copy or Command-S to save, you’ve used keybindings.) What few
people know is that you can create your own bindings. (One exception: you
can’t create keybindings for Microsoft programs like Word.)

Note: Keybindings are different from plain old keyboard shortcuts
because shortcuts only let you assign keystroke triggers to
existing
commands. But keybindings let you combine
commands however you like, and then assign keystroke triggers shortcuts
to those
combinations
. That means you’re not stuck
with just the commands that the app’s creator came up with—you can
create whatever command combos are the most useful to you.

On
your Mac, you can create a text file in
~/Library/KeyBindings
called
DefaultKeyBinding.dict
to which you can add
your own custom tricks. If you’re working in Finder, the easiest way to
get to this directory is to press Command-Shift-G, then type
~/Library
and press Return. If you see a folder
called KeyBindings, open it; if you don’t, use Command-Shift-N to create
it (make sure to capitalize it the way I’ve written it here). Next, open
up TextEdit (or your text editor of choice) and create a new file. Save it
as
DefaultKeyBinding.dict
in the
KeyBindings folder you just located or created (you can get to this folder
from a Save dialog box using the exact same procedure as above: press
Command-Shift-G, and then type
~/Library/KeyBindings
). Now you’re ready to
start adding your own bindings.

These bindings can include shortcuts for cursor movement; text
selection; and deleting, copying, pasting, and inserting text. You can
even combine the shortcuts in sequences, which provides a wide variety of
possibilities. The
DefaultKeyBinding.dict
file is in a plain-text
PLIST format (see
[Hack #6]
), so it’s easy to
modify in any text editor.

You can see my custom keybindings file at
http://ttscoff.github.com/KeyBindings
to get some ideas.
You’ll probably want to start with a fresh one, though, and only add the
bindings that you need. It’s easy to lose track of what you’ve created if
you don’t use them often, and using bindings that another application has
assigned can lead to confusing behavior in that app. It’s best to create
bindings one or two at a time and get used to them before getting too
crazy. An empty
DefaultKeyBinding.dict
file simply looks like
this:

{
}

You
add bindings between the curly brackets. Here’s an example
binding:

"^T" = (capitalizeWord:, moveWordForward:, moveWordBackward:);

The first part of any keybinding—the bit before the equal sign—is
the key combination that will trigger the action. Here are the symbols you
use in this part of the keybinding to represent
modifier keys:

^
The
Control key
@
The
Command key
~
The
Option key
$
The
Shift key

These can be used in combination with other modifier keys and
regular characters to create shortcuts that won’t conflict with default
system ones.

Note: You
only need to include the Shift key symbol ($) in a
keybinding when you can’t represent the character that follows it in its
Shift-modified form. In all other cases, you can just use the character
that you get when typing it holding Shift, such as a capital letter or a
number-row symbol. For example, in the sample binding above,
^T
represents the key combination
Control-Shift-T, since you have to press Shift to type a capital T (if
it were written as
^t
instead, that
would mean you just need to press Control-T). Or if you wanted to use an
exclamation point (which you type by pressing Shift-1), you can just use
!
in your keybinding instead of
$1
. But, you can’t include arrow
characters in a keybinding, for instance, so if you want to include one,
you’d instead type an escape sequence such as \UF700 (up arrow). A
full
list of special-character escape sequences
is available.

The next part—the bit after the equal sign—is what happens when you
type the specified key combination in a text field. It consists of a
sequence of commands that will be performed in order. There
are quite a few commands available, including cursor
movement, text insertion/deletion, and copy-and-paste functions. Jacob Rus
has a helpful
list of
available commands
. The sample binding above (the one triggered by
pressing Control-Shift-T) capitalizes the word that your insertion point
is currently within and then moves the insertion point to the next word
when you type. It allows you to put your insertion point at the beginning
of a title and then press the key combination repeatedly to capitalize all
the words in the title.

One of my favorite and most useful keybindings is a replication of
the Command-Return keybinding found in the text editor TextMate. In
TextMate, this keybinding allows you to type Command-Return when your
insertion point is anywhere within a line to add a blank line below the
current paragraph, and your insertion point will jump to the new line
without breaking the text in the current line. The keybinding definition
looks like this:

// TextMate Command-Return (Command Enter)
"@\U000D" = (moveToEndOfParagraph:, insertNewline:);

Lines
beginning with
//
are
comments and are ignored by the system, but they’re useful for reminding
you what this keybinding is for. The
\U000D
part is the code you need to use for
Return, as there’s no character representation of it. The command
sequence, as you can probably tell, just moves the cursor to the end of
the paragraph and inserts a blank line. Simply placing these lines of code
between the curly brackets in your
DefaultKeyBinding.dict
file will create this
shortcut on your system, and it will be available to
any
application the next time you start it.
(Applications that are running when you save the file will need to be
restarted for it to take effect.)

Along the same lines, here’s a keybinding that does the opposite.
Pressing Command-Shift-Return will insert a new line
above
the current paragraph and jump to it .

// Insert blank line above paragraph (Command Shift Return)
"@$\U000D" = (moveToBeginningOfParagraph:, moveLeft:, insertNewline:);

Lastly, you can also nest commands to create sequences. For example,
I use nested commands to create
Markdown
links
using my clipboard contents. (Markdown is a plain-text syntax created by
John Gruber that lets you rapidly create HTML and rich-text documents.)
The following code creates a sequence that allows me to copy a URL to my
clipboard, select some text, and then type Control-Command-W, then l
(that’s a lowercase L, not the number one), then c to reformat the
selected text as “[selected text](URL from clipboard),” which becomes an
HTML link when the Markdown is converted:

"^@w" = { // Multi-stroke Markdown command
"l" = { // Markdown link
"c" = (setMark:, breakUndoCoalescing:, moveRight:, insertText:, " ", deleteToMark:, insertText:, " [", moveLeft:, deleteBackward:, moveRight:, yank:, moveLeft:, insertText:, "](", setMark:, pasteAsPlainText:, insertText:, ")", moveRight:, deleteBackward:, moveLeft:, selectToMark:); // link with clipboard
};
};

As you can see, following a keybinding definition with another set
of curly brackets creates the sequence. Every nested set of curly brackets
adds another keyboard shortcut that can be pressed after the parent
shortcut. This allows you to add a set of commands that only fire after
pressing the parent command, helping to prevent overlap with system
shortcuts and other custom shortcuts. The possibilities are vast. You can
find out more at
my blog
and at
the aforementioned GitHub URL. Also, see
“Customizing
the Cocoa Text System”
for more details on creating your own
commands.


Brett Terpstra

Hack 26
. Eject iTunes

You
don’t have to sacrifice iTunes library size if you’re using
a MacBook. Learn how to quit iTunes
and
eject the
external drive you store your iTunes content on all at once.

I’m going to venture a guess that most of you reading this use a
MacBook as your primary Mac, and that your iTunes library has grown too
big for its britches. You moved it to an external drive, didn’t you? And
now you wish there were an easy way to quit iTunes and eject your drive at
the same time to bring back that good ol’ fashioned “grab and go” feeling
that got you using a MacBook to begin with, right? I think I can help.

With
a little Automator and a dash of Terminal, you can create a
utility that does exactly what I just described, which can make your
portable life just a little easier. You can put this utility in your Dock
or give it a shortcut with a productivity utility like Alfred or
LaunchBar—whatever lets you work the fastest. Let’s get started.

Step 1: Quit iTunes

The first thing you want your Automator utility to do is quit
iTunes for you, and you just need one action for that. Here’s how to set
that up:

  1. Launch Automator.

  2. In the “Choose a type for your document” window, pick
    Application and then click Choose.

  3. On the left side of the Automator window, click the Library
    heading (if it’s not already selected).

  4. In the next column to the right, scroll down in the list of
    actions until you find Quit Application. Double-click it to add it
    as the first step of the Automator utility you’re creating.

  5. In the rightmost pane of the Automator window, in the
    pull-down menu in the Quit Application action you just added, select
    iTunes.

You’ve now created a utility that quits iTunes. So far, so
good.

Step 2: Tell Automator to Wait

I don’t know about you, but iTunes sometimes takes a couple
seconds to quit, even on my powerful, maxed out, SSD-slinging Retina
MacBook Pro. Maybe it’s tidying up some changes I made to the library
database, or maybe it’s just checking to see if it left the oven on.
Whatever the reason, I’ve been using a Mac long enough to know that if
an app is depending on an external drive, you should wait for the app to
truly
quit before you cut it off from that drive.
So let’s add a slight pause to your action:

  1. In the list of actions in the second-to-left column in
    Automator, find the Pause action and double-click it to add it to
    your utility.

  2. In the rightmost pane, specify how many seconds you want
    Automator to wait. I recommend a pause of three seconds, but this is
    really up to you. Enter your best guess as to how much time iTunes
    needs to do its thing.

Step 3: Eject Your Drive

Now comes that dash of Terminal I mentioned. Automator strangely
doesn’t have any kind of “eject external stuff” action, so we need to
get slightly messy with a single-line Terminal command:

  1. In Automator’s list of actions, find Run Shell Script and
    double-click it to add it to your utility. In the main Automator
    pane, and make sure the Shell menu in the upper-left corner of that
    action is set to “/bin/bash.”

  2. In
    the action’s text box, delete any existing text and
    then add this line of code:

    *hdiutil eject /Volumes/
    the name of your external iTunes drive

You’ll need to substitute the name of
your
external iTunes drive into the end of that command.

Step 4: Save Your Utility

You’re all set. In fact, if your external drive is connected right
now and iTunes is running, try clicking the big Run button in the
upper-right corner of Automator’s window. If everything’s set up right,
iTunes should quit and your drive will be ejected.

Now
it’s time to save your Automator utility as an
application that you can fit into your workflow. Go to File→Save, give
it a relevant name like “Eject iTunes,” pick a convenient location to
save it, and be sure the File Format menu is set to Application. When
everything looks good, click Save.

Congratulations! To the chagrin of many a music startup and
competitor, you just created an, ahem, iTunes killer. Now it’s time to
actually use it.

Step 5: Make Your Utility Useful

You
could place your new iTunes killer in the Applications
folder and then drag it to your Dock for easy access. Or, if you’re
really
feeling lazy, you could just put it on the
desktop and launch it from there. But there are fairly easy ways to
assign this utility a shortcut so it’s just a keystroke away.

In System Preferences→Keyboard→Keyboard Shortcuts, you can create
a new Application Shortcut by choosing Application Shortcuts in the
lefthand list and then clicking the + button. Then browse to your new
utility and assign it a shortcut that will make OS X launch your utility
and run that series of commands no matter what you’re doing or what app
you’re using.

Or, if you use one of the Mac’s popular productivity utilities
like Alfred, LaunchBar, or Quicksilver, they each have a variation on
the theme of assigning a shortcut to any file or folder. In my favorite,
Alfred, you do it by going to Preferences→Hotkeys. You can also tinker
with Alfred’s Extensions panel, where you can create bundles of
multiple
apps to open or close with a single
shortcut.

However you decide to work your new iTunes killer into your
workflow, I hope it saves you at least a little time fumbling around
with quitting the app and then ejecting the drive it depends on. After
all, those little bits of time do add
up.


David Chartier

BOOK: Mac Hacks
13.14Mb size Format: txt, pdf, ePub
ads

Other books

Dark Moon by Elizabeth Kelly
Rainbow Valley by Lucy Maud Montgomery
Of Metal and Wishes by Sarah Fine
Lone Star Millionaire by Susan Mallery
Betrayals in Spring by Leigh, Trisha
Murder at the Powderhorn Ranch by Jessica Fletcher