Corbin Dunn and Louise Lovelle
Treehouse
Woodworking
About
Search

Archive for the 'Tips and Tricks' Category

Cocoa programmers: avoid writing to the user defaults when you don’t need to

Tuesday, August 26th, 2008

[Edit: ecto ate this post, so I’m typing it in again!]

I discovered that a lot of applications will unnecessarily write to NSUserDefaults. This causes your app to hit the disk when it shouldn’t, and is a slight performance penalty. AppKit is also susceptible to this problem; if you hit cmd-O to bring up the open panel in any application, you will see it writing things to the user defaults, when it probably doesn’t need to do so. I’m working on fixing that, and you should to!

So, how do you do it? It is easy — just add a breakpoint on -[NSUserDefaults(NSUserDefaults) setObject:forKey:]. You can do this with gdb:

b -[NSUserDefaults(NSUserDefaults) setObject:forKey:]

Or you can use the breakpoints window in Xcode (my preferred way):

Picture4 - breaking on user defaults.png

Then, reproduce whatever action might cause it to happen (ie: starting your application, or in my test case, cmd-o to bring up the open panel). Look at the backtrace in Xcode and figure out why you are doing too much work:

Picture5 - the callstack for a user default.png

Getting rid of the undo warning in Xcode after saving

Friday, August 22nd, 2008

One of the most annoying dialogs in Xcode is the undo warning dialog you get when attempting to undo after a save. I do this all the time, and I hate the warning. Luckily, there is a user default to turn it off:

defaults write com.apple.Xcode XCShowUndoPastSaveWarning NO

Xcode code completion and your code

Thursday, August 14th, 2008

How can you become a faster Cocoa programmer? One way is to adequately name your variables, enums and classes.

Let’s start with enums and take an example from something new to NSTableView in Leopard. This is copied from NSTableView.h with the comments stripped out for clarity.

enum {

NSTableViewSelectionHighlightStyleRegular = 0,

NSTableViewSelectionHighlightStyleSourceList = 1,

};

typedef NSInteger NSTableViewSelectionHighlightStyle;

- (NSTableViewSelectionHighlightStyle)selectionHighlightStyle;

- (void)setSelectionHighlightStyle:(NSTableViewSelectionHighlightStyle)selectionHighlightStyle;

There are several things to notice here, some of which are important to you. The most important thing (in my opinion) is the common prefix. Notice that the enum values fully contain the enum type name. Why? The answer is code completion, which you should be using. It is much easier to remember one key portion of the name than to remember all values. In this case, the key thing to remember is “selection”.
As a programmer working with NSTableView you know you want to change the selection highlight style, but you don’t remember the option for the specific style you want. You know the Cocoa convention is setFoo, so you type:

[tableView set

And hit escape (or whatever key combo invokes code completion for you. For me, I remapped the key to ctrl-space, since I was used to Delphi and Visual Studio. But, I also use escape).
You see this result:

TableViewSet_CodeComplete.png

and start typing “sel” to see the result you want:

TableViewSetSelResult.png
Which inserts this template:

TableViewSetTemplate.png
Now, I’m surprised, but most people don’t realize that they can type ctrl-/ (or maybe alt-/ depending on your key bindings) to select the placeholder and type over it. Memorize that keystroke, and use it.
Now, the common prefix name comes in really handy with code completion — just start typing in the type that the placeholder tells you and you’ll see what options you have:

TableViewSetOptions.png
In essence, you only have to remember “sel”, and from there you can derive exactly what option you want using code completion. Less memorization, and faster programming.
Unfortunately, a lot of Cocoa came along before code completion, and doesn’t follow this convention. But if you look at a new UI framework (ala: UIKit for the iPhone), you’ll find this pattern throughout it. It makes programming very fast with fewer trips to the header to find out what you need.

The bottom line: use a common prefix, wherever you have a list of options. Also note that the NSTableViewSelectionHighlightStyle has the prefix NSTableView, since it only applies to NSTableView. But, the property name is “selectionHighlightStyle”, since it doesn’t make sense to replicate the type name there.

New Leopard user features - Open and Save Panels

Tuesday, October 30th, 2007

Leopard, the new Mac OS 10.5, is out!

There are a lot of new features, but not everything is mentioned on the features page. Here are some of the cool “power user” features which you may not know about:

The Open and Save Panel (implemented in Cocoa, also known as the NSSavePanel and NSOpenPanel)

1. There is a new Icon View mode with options:

However, the way to change the icon size to be small (as seen above) isn’t obvious. Click and hold down on the icon view segmented cell and a popup will occur:

2. You can now insert items in the panel (and Finder) “side bar” by drag and dropping directly from the views (you couldn’t do this on Tiger!) You can also rearrange or remove items.

3. When you have an item selected, cmd-r will “reveal” it in Finder

4. When you have an item selected, cmd-i will give the Finder “info” window.

5. If you perform a search, you can save the search (just like in Finder). In addition, you can save it just for that application:

…and it appears in a special sidebar section for just that app (to remove it, drag it out):

That’s it for now. 5 new features not even included in the top 300!



Mac OS X tip: Apps in your Finder Toolbar!

Thursday, February 15th, 2007

Here is a very cool tip that I discovered from one of hour HI guys. You can put apps in your toolbar! For instance, I frequently drag files to TextEdit to open them. Having them in the toolbar is handy in case they aren’t in the Dock or I don’t want to drag too far:

Apps In Finder

To do this, right click on your toolbar (or ctrl-click if you don’t have a Mighty Mouse) and select “Customize Toolbar…”. Now, here is the trick; open up another Finder window and browse to the apps that you want to add to the toolbar. Simply drag them there, and they will stick! Very cool.

synergy on Intel MacOS X

Friday, January 13th, 2006

Hi All. I use synergy to share my keyboard/mouse on my computers. I recompiled the server for Intel MacOS. Feel free to grab it and use it:
synergyc_synergys_mac_intel.zip. It is an Intel ONLY binary. It will not work on PPC.

Tiger and the dictionary is a keyboard shortcut away

Tuesday, September 27th, 2005

A really cool “hidden” Tiger feature is the ability to quickly see the dictionary for (almost) any word in a Cocoa app. Move the cursor over a word, and press cmd-ctrl-d. You will see the dictionary definition:

Dictionary Image Screenshot

Very cool! Holding down cmd-ctrl-d and moving the mouse will move the dictionary item.

Safari Shortcuts

Thursday, September 22nd, 2005

Poking around in Safari’s Contents/Resources folder reveals an HTML page describing the shortcuts:

file:///Applications/Safari.app/Contents/Resources/Shortcuts.html

Enjoy!

Sharing your mouse, keyboard and pasteboard with several Macs (or PC's)

Saturday, August 13th, 2005

Like many people, I have several computers at work that I frequently switch among:

Corbinsofficecomputers

I recently started using Synergy to share my mouse, keyboard and pasteboard among my main work G5 and my home Powerbook. It is pretty cool, and fairly easy to setup. Once I pop into work, I run a little script to start the client on my PowerBook:


#!/bin/sh
prog=(/Users/corbin/synergy/synergyc neeb.apple.com)

### Stop any currently running Synergy client
killall ${prog[0]##*/}

### Start the new client
exec “${prog[@]}”

I set this to be always opened with Console, and I manually double click on it when I want to start the synergy client.

Read the documentation for the exact way to set it up..once you have it running, it is very cool!

Fixing the annoying focus stealing habit of Safari RSS feeds

Wednesday, June 22nd, 2005

Safari RSS support is way cool. Except for the fact that after a page has loaded, the search field receives focus, causing the space bar to not scroll down a page at a time. I wanted to fix this…but how?

Well, I browsed to an RSS feed and viewed the source in Safari. I noticed this line:

<script language=JavaScript src=“feed:///__rsrc__/Articles.js”>

So, Safari uses some internal file called Articles.js. Now, where is it?

Well, I dropped down to a command prompt in Console, logged in as root (su’d) and typed:

nibroc:/Users/corbin/Desktop root# fs_usage -w -f filesys Safari | grep Articles.js

console

Okay! It is in some framework directory, A/Resources/Articles.js. But which one? Again, a simple command can reveal the secrets:

nibroc:/Users/corbin/Desktop root# otool -L /Applications/Safari.app/Contents/MacOS/Safari

Picture 4

Okay, it probably is SyndicationUI.framework or WebKit.framework. Which one?

nibroc:/Users/corbin/Desktop root# ls /System/Library/PrivateFrameworks/SyndicationUI.framework/Versions/A/Resources/

Picture 6

Gotcha!
vi that file, search for focus, and comment out the offending line:

Picture 7

And we are done! No more focus stealing…


(c) 2008 Corbin Dunn

Corbin’s Treehouse is powered by WordPress. Made on a Mac

Entries (RSS) and Comments (RSS).

26 queries. 1.652 seconds.

Bad Behavior has blocked 445 access attempts in the last 7 days.