Matt Gemmell

TOLL is available now!

An action-thriller novel — book 2 in the KESTREL series.

★★★★★ — Amazon

Cocoa Source Code

This page contains some of my Cocoa/Objective-C source code for fellow Mac OS X developers.

You’re welcome to download and use the source, subject to the License Agreement. It’s assumed that you’re a developer and you know how to make use of the source; I can’t provide support. Happy coding!

Note: all code is unmaintained and unsupported as of 2015.



Don’t panic: you can use all my code without paying anything! Having said that, a lot of work (and time which I should really be spending with my ever-patient wife) has gone into creating the code below. If my code saves you some time and trouble, and you feel like making a small donation to support my future development efforts, it would be very much appreciated.

You can also take a look at my Amazon Wishlist if you like; it’s always fun to receive a surprise package in the mail. I’ve found that many people prefer this method too.

Feel free to contact me (details here) if you have something else in mind.


If you use some of my code, I want to hear from you!

I’m always interested to hear from developers who have found some of my code useful, and I may link to your app too. If your app uses some of my code, get in touch and let me know about it.


License Agreement

The source code is released under a very flexible license, designed to let you use, modify and/or redistribute the code pretty much as you see fit, whilst providing me with basic protection and credit.

Before downloading any of the source code, please have a look at the Source Code License page. Thanks. :)


Using my code without attribution

My source code license agreement requires attribution, but I’m aware that some people need a license agreement which does not require attribution. If you’re in that situation, I can help: I’ve set up an online store where you can quickly and easily buy a non-attribution license to my various source code components. The online license store can be found here.

If you have any questions, feel free to contact me.


Source Code

Unless mentioned otherwise, all source code requires Mac OS X 10.2 (Jaguar) or later, and corresponding versions of the Developer Tools to open the projects. Some source may also be suitable for previous versions of Mac OS X.

Any special requirements a project has are mentioned in a note with the project description.



MGWordCounter provides live word-counting (of both the full text and the current selection) for NSTextView on OS X, and UITextView on iOS.

Read more about it here!
Note: this code requires iOS 5.0 or later, or OS X 10.7 Lion or later. In both cases, ARC is required.



MGTileMenu is a customisable tile-based contextual menu component, for iPad, iPhone and iPod touch. It's for iOS 5, and supports ARC, Retina Displays (and non-Retina displays) and VoiceOver accessibility.

Read more about it here!
Note: this code requires the iOS SDK 5.0 or later.



MGSplitViewController is an enhanced replacement for UISplitViewController on the iPad. It allows displaying the master view in either orientation, toggling it with animation, changing the split from horizontal to vertical, dragging the divider, and more.

Read more about it here!
Note: this code requires the iOS SDK 3.2 or later.



MGImageUtilities is a collection of useful UIImage categories for iPad and iPhone, including image-tinting and image-cropping functionality (which explicitly supports multiple orientations, and high-resolution Retina Displays).

Read more about it here!
Note: this code requires the iOS SDK 3.2 or later.


iPad VGA Output

iPad VGA Output is an open source iPad application showing how you can display content on an external display via the iPad VGA adapter. It includes a window which goes on the external screen, and a window which goes on the iPad, and shows how to detect supported external resolutions.

Read more about it here!
Note: this code requires the iOS SDK 3.2 or later.


iPad Multi Touch

iPad Multi Touch is an open source iPad application which lets you visually track multiple touches, up to the maximum number the iPad supports simultaneously (which is eleven, by the way). It has some nice Core Animation effects too.

Read more about it here (and see some videos too)!
Note: this code requires the iOS SDK 3.2 or later.



Shady is an open source Mac OS X application which lets you dim the screen beyond its usual limit, ideal for tired (or photo-sensitive) eyes.

Read more about it here!
Note: this code requires Mac OS X 10.5 (Leopard).



MGScopeBar provides a scope/filter bar much like you'll find in Mail, the Finder (in the Find/Spotlight window), iTunes and elsewhere. You can see a demo movie of it here.

It uses a delegate system to get its data, is extremely customizable and has a cool Smart Resize feature for when limited horizontal space is available.

Read more about it here!
Note: this code requires Mac OS X 10.5 (Leopard).



MGTemplateEngine is a native Cocoa system for generating text output based on templates and data. It's a close cousin of systems like Smarty, FreeMarker, Django's template language, and other such systems.

It's ideal for Cocoa or iPhone apps needing to generate text output using variable-substitution (with looping and/or conditional logic), including HTML for export or for apps with WebKit-based UIs, invoices or printable templates, mail merge, data export or any number of other things. It's also great (in combination with WebKit) for letting your users create themes/styles for your application.

Read more about it here!
Note: this code requires Mac OS X 10.5 (Leopard) or iPhone OS 2.0.


Skinnable App

This is a small application demonstrating how to use an embedded WebKit WebView to easily "skin" your applications, i.e. style your app's UI using standard CSS files. It shows how to switch CSS themes dynamically, how to use Objective-C to add HTML content into a WebView, how to retrieve data from the HTML document, how to replace existing content, and how to allow HTML controls (form elements) to call methods in Cocoa objects.
Note: this code was written on Mac OS X 10.5 (Leopard), and the project requires Xcode 3. The code itself should run just fine on Tiger too.


MGTwitterEngine (on github)

MGTwitterEngine is a class which lets you integrate Twitter support into your Cocoa application, by making use of the Twitter API. The entire API is covered, and appropriate data is returned as simple Cocoa objects (NSArrays, NSDictionarys, NSStrings, NSDates and so on), for very easy integration with your code. Creating a TwitterEngine is as easy as instantiating it, setting your Twitter username and password, and you're done. See this blog post for more.

If you're an iPhone developer, you'll be pleased to know that MGTwitterEngine works on both Mac OS X Leopard and on iPhone/iPod Touch (with the official SDK from Apple).
Note: this code was written and tested on Mac OS X 10.5 (Leopard), and the Xcode file requires Xcode 3.0. The code itself should be fine on Tiger too.



A category on NSImage allowing you to get an image containing a Quick Look preview of the file at a given path. You can specify the size, and whether the preview should be rendered as an icon (i.e. with a document border, drop-shadow, page-curl and file type/extension label superimposed). If Quick Look can't generate a preview for the specified file, you'll be given the file's Finder icon instead (which is how the Quick Look panel itself behaves in Leopard).
Note: this code requires Mac OS X 10.5 (Leopard) or later.



A cool NSWindow subclass which lets you easily attach a view to another window, displayed in a floating "speech-bubble"-like borderless window. Very handy for contextual help, showing options for a certain control, or just to draw attention to things. Have a look at this screenshot to see how it works. You can also grab a project showing how to use an MAAttachedWindow with an NSStatusItem.
Note: this project requires Xcode 2.4 and Mac OS X 10.4 (Tiger) or later, but should work on previous versions of Mac OS X if you include the files in your own project.



A simple category on NSView and an accompanying class which together allow you to add "tracking areas" to your views. Tracking areas are like tracking rectangles but are more robust and effective, and can be configured to track the mouse either at all times, only when the view's app is active, only when the view's window is key, or only when the view is First Responder. You can also choose whether to use the view's visible rect in preference to the rect you pass in. See the example for details.

Fair warning: it uses a polling timer to do its thing, but in my tests it consumes very, very little CPU (0.1% in Activity Monitor), and it will only poll if there are actually some tracking areas set up.

Note: this project requires Xcode 2.4 and Mac OS X 10.4 (Tiger) or later, but if you want to just take the source files and create your own project, it should work right back to Mac OS X 10.2 (Jaguar).



An NSAnimation subclass which provides more desirable behaviour than NSViewAnimation itself in most circumstances; i.e. it animates the views' frames itself in the same thread, leading to visually synchronised movement. Try out the included sample app to see the difference between regular NSViewAnimations and these.

This is based on code by Todd Yandell - thanks, Todd!

Thanks to Joe Goh for a bug-fix which prevented animations from running unless at least one animation-dictionary had an effect specified.

Note: this project requires Xcode 2.1 and Mac OS X 10.4 (Tiger) or later.



A category on NSColor which returns another NSColor for use when drawing label text on top of the receiver. Either white or black is returned, depending on which will have most visual contrast with the receiver's color. In the event of the receiver's colorspace not being converted to RGB, black is returned by default.

Note: this project requires Xcode 2.1 and Mac OS X 10.4 (Tiger) or later.



A category on NSImage allowing you to obtain a resized version of the image to fit an arbitrary size, with a choice of cropping or scaling methods (crop center, crop start, crop end, or scale to fit). Uses high-quality interpolation too.

Here's a screenshot.

Note: this project requires Xcode 2.1 and Mac OS X 10.4 (Tiger) or later.



An NSPanel subclass which creates a translucent window like iPhoto "Adjust" palette. Useful for graphics applications where a non-intrusive, high-contrast UI style is needed. Or just to look cool.

Here's a screenshot.

Note: this project requires Xcode 2.1 and Mac OS X 10.4 (Tiger) or later.



An NSBox subclass which draws a pretty rounded-rectangle box with a gradient background; take a look at the screenshot to see how it looks. Includes lots of configurable options, and it's ready to drop right into your project.

Here's a screenshot.

Note: this project requires Xcode 2.1 and Mac OS X 10.4 (Tiger) or later.



A project which displays a window in the iTunes 5/6 (i.e. "polished metal") style. Like the Unified look, but darker. Get the iTunes look in your own apps easily.

Here's a screenshot.

Note: this project requires Xcode 2.1 and Mac OS X 10.4 (Tiger) or later.


iCal Calendars

A simple application which reads iCal's calendars and returns an NSDictionary whose keys are the names of the calendars, and whose values are Calendar objects, with fields "title" (NSString), "notes" (NSString), "color" (NSColor), and both GUID keys used by iCal to keep track of calendars and their source data.

Useful if you need a list of the user's iCal calendars with their names, descriptions and correct colors. This code parses the actual XML property list files which define the calendars; it doesn't use AppleScript, nor require launching iCal.

Note: this project requires Xcode 2.1 and Mac OS X 10.4 (Tiger) or later.



A simple category on NSBezierPath allowing you stroke inside a path, rather than stroking centered on the path.

From an idea by Rainer Brockerhoff.



An NSView subclass and accompanying NSWindow subclass which provide the same rounded floating panel overlay which the Panther application switcher uses.

Here's a screenshot.

Note: this project requires Xcode and Mac OS X 10.3 (Panther) or later.



An NSTableHeaderCell subclass providing the metallic column headers seen in iApps, including the Calendars list in iCal and the Source list in iTunes.

Here's a screenshot.

Note: this project requires Xcode and Mac OS X 10.3 (Panther) or later. Thanks to Heinrich Giesen for fixing the crasher.


iTableView (Source List and Colored Checkboxes)

A pair of NSTextFieldCell and NSImageCell subclasses (and a simple NSTableView subclass) which provide the blue-gradient selection highlighting behaviour you see in iCal's "Calendars" list, iTunes' "Source" list, and the sidebar in the Panther Finder. Apple calls this kind of control a Source List.

Also includes code to draw colored checkboxes as in iCal, using any color you want!

Here's a screenshot.

Note: this project requires Xcode and Mac OS X 10.3 (Panther) or later.


Address Book Test

Shows how to make use of the Mac OS X 10.2 Address Book API. Includes a live-filtering field which locates Address Book entries by forename, surname or company, and demonstrates finding the primary email address for a specified person, as well as their name and picture.

Here's a screenshot (email address purposely obscured for the sake of Mike's privacy!)


Better TextFinder

Easily implement Find and Replace in your application. Adapted and improved from TextEdit's implementation. Includes both the class and a Nib file containing an appropriate Find panel. Read more about the improvements here.

Here's a screenshot.



A class which lets you easily create Mail/iTunes/Safari-style Preferences windows, with toolbars and multiple panes loaded from bundles. Loads of configurable options and flexibility, and professional results. Take a look at the class documentation (also included in the download).

Here's a screenshot.


Window Fade-out

Ever felt the need to have a window fade out of existence, instead of just closing? Here's how.



Shows how to implement grab-scrolling, like in Preview, Photoshop, Acrobat and more. Includes the two requisite hand-cursors.

Here's a screenshot.


Screen Sharing

Source for two apps, a client and a server, letting you send full-screen screenshots of your Mac via Rendezvous. This is a modification of Apple's Picture Sharing code.


Localized Nibs

Not really source code, but just as good! This is a collection of "MainMenu.nib" files, with the menubars pre-localized into various languages. All items are connected to the proper actions, and several additional resources are included (such as flag graphics, ISO language tables, and more).

Currently, these languages are included: English, French, German, Dutch, Italian, Catalan, Danish, Brazilian Portuguese, Swedish, Finnish, Spanish, Serbian, Japanese and Korean.

Here's a screenshot (this is the Italian nib).


Snapping Window

Shows how to create proper snap-to-edge windows or palettes, which snap during the drag (as they should).



A class which gives you icon-buttons with pop-up menus. Configure all kinds of things like whether the menu appears when you click anywhere on the icon or just on the little pop-up arrow, whether the button shows the icon of the last-selected item (like in Photoshop's fly-out tool palette menus), and more.

Here's a screenshot.



Eric Wang's reimplementation of my PopUpImage class (see above), using a subclass of NSPopUpButton. In Eric's own words, the improvements include:

  • Reuse of NSPopUpButton's menu-related methods.
  • The class has been split into an NSControl/NSCell pair, allowing it to be used within NSTableView, NSMatrix, etc. The core functionality is implemented in a subclass of NSPopUpButtonCell.
  • There are also some subtle interface-related improvements.
Note: this project requires Xcode and Mac OS X 10.3 (Panther) or later.



Ever wanted to find the name (or full path, or icon) of the user's default web brower or email client? Or perhaps their default FTP app, or newsreader? This source shows you how to find the user's chosen default application for any protocol you like.

Here's a screenshot.


URL Test

A simple Cocoa application, showing how to launch URLs, use the standard About box with a scrolling field of credits, display alert panels, remember window positions, and more. Useful for developers who are new to Cocoa.


Icon For File Type

A little app which lets you type a file-extension (like "doc" or "html"), and have it show the icon for that file-type (the big, beautiful 128x128 pixels icon). Useful for display purposes.

Here's a screenshot.


Font Panel Test

Shows you how to choose a font with the standard Font Panel. This little app shows a text field with a font's name and size (like "LucidaGrande 13"). You can click a button to bring up the Font Panel, and when you choose a new font the text field is updated with the new name and size. Useful if your app needs to choose a default font for something.


Image In Table

Shows how to put images into a table column. We're constantly asked how to do this.

Here's a screenshot.


Mod Key Test

A little window with a button that doesn't do anything... unless you click it whilst holding the command, option and control keys. Shows how to detect that those keys are pressed. Useful in all kinds of situations for adding functionality without cluttering up your app's interface.


Shape Thing

A cool little app that lets you draw multi-coloured circles and ovals. Shows how to let the user draw shapes with the mouse, how to draw shapes with transparency, and how to let the user hold down the shift key to constrain a shape to be proportional. Just for fun!

Here's a screenshot.


Objective-C Services

One of my System Services. Source included.


iCal Birthday Shifter

A little Mac OS X application I made a few years ago to sync birthdays from Address Book to iCal, long before iCal was natively able to do that (as it is now). Source included.