Matt Legend Gemmell Modesty is Lying

Mac OS X Cocoa and iPhone Development Services available at Instinctive Code.
Mac OS X Cocoa and iPhone Developer for hire

Other Pages

Categories

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!

(And if you’d like to hire me for your own Mac or iPhone development projects, take a look at Instinctive Code.)

 

Donations

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 fiancée) 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 use the button above to donate via PayPal, or 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’d like to link to your app too. Please read this blog post if your app uses some of my code, and let me know about it. I’ll be glad to add it to the Featured Apps page here on the site.

 

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. :)

 

Subversion repository

Note: some of this code is also available from my public subversion repository at svn.cocoasourcecode.com.

You are encouraged to use the versions in the repository instead of the snapshots here, wherever possible. The repository is always guaranteed to be up to date, and you can obtain updates very easily.

 

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.

 

svn MGTemplateEngine
  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 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).

 

Download Skinnable App
 
screenshot

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.

 

svn MGTwitterEngine
  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.

 

Download NSImage+QuickLook
 
screenshot

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.

 

Download MAAttachedWindow
 
screenshot

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.

 

Download MATrackingArea
  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).

 

Download MGViewAnimation
  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.

 

Download NSColor+ContrastingLabelExtensions
  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.

 

Download ImageCrop
  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.

 

Download HUDWindow
 
screenshot

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.

 

Download RoundedBox
 
screenshot

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.

 

Download TunesWindow
 
screenshot

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.

 

Download 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.

 

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

From an idea by Rainer Brockerhoff.

 

Download RoundedFloatingPanel
 
screenshot

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.

 

Download iTableColumnHeader
 
screenshot

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.

 

Download iTableView (Source List and Colored Checkboxes)
 
screenshot

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.

 

Download 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!)

 

Download 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.

 

Download SS_PrefsController
 
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.

 

Download Window Fade-out
  Ever felt the need to have a window fade out of existence, instead of just closing? Here’s how.

 

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

Here’s a screenshot.

 

Download 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.

 

Download 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).

 

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

 

Download PopUpImage
  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.

 

Download ImagePopUpButton
  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.

 

Download GetNetApps
  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.

 

Download 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.

 

Download 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 128×128 pixels icon). Useful for display purposes.

Here’s a screenshot.

 

Download 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.

 

Download Image In Table
  Shows how to put images into a table column. We’re constantly asked how to do this.

Here’s a screenshot.

 

Download 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.

 

Download 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.

 

Download Objective-C Services
  One of my System Services. Source included.

 

Download iCal Birthday Shifter
  One of my Mac OS X applications; see the iCal Birthday Shifter page for details. Source included.

Back to Home