Category > Mac OS X

NSCoder Night London

stompy » 21 September 2009 » In Mac OS X » Comments Off

This post has been moved

Continue reading...

Tags: , , ,

MacDev 2009 Announced

stompy » 23 September 2008 » In Mac OS X, miniblog » No Comments

The Mac Developer conference 2009 has been announced for the weekend of April 18-19, 2009.

This is great news for us who find  WWDC or C4 too much of a commitment.

Continue reading...

Tags: ,

Xcode and git – Another build script

stompy » 14 August 2008 » In Mac OS X » 8 Comments

Update: 17Oct09

Since the CFBundleVersion is now presented within brackets, I’ve changed this again.

You can copy and paste the version below, or you can get it directly from Github where it is gist 208825

Update 25Nov08: The script is also available from here

I’ve rewritten the script from Cocoa Is My Girlfriend in ruby to append the git commit hash into an application’s Info.plist file.

This way, it keeps the version string that is added in Xcode, so the default About panel looks like this:


Here is the script

#!/usr/bin/env ruby

# Xcode auto-versioning script for Subversion by Axel Andersson
# Updated for git by Marcus S. Zarra and Matt Long
# Converted to ruby by Abizer Nasir
# Appends the git sha to the version number set in Xcode.
# see for more details

# These are the common places where git is installed.
# Change this if your path isn’t here
common_git_paths = %w[/usr/local/bin/git /usr/local/git/bin/git /opt/local/bin/git]
git_path = ""

common_git_paths.each do |p|
  if File.exist?(p)
    git_path = p

if git_path == ""
  puts "Path to git not found"

command_line = git_path + " rev-parse –short HEAD"
sha = `#{command_line}`.chomp
puts sha


f =, "r").read
re = /([\t ]+<key>CFBundleVersion<\/key>\n[\t ]+<string>)(.*?)(<\/string>)/
f =~ re

# Get the version info from the source Info.plist file
# If the script has already been run we need to remove the git sha
# from the bundle’s Info.plist.
open = $1
orig_version = $2
close = $3

# If the git hash has not already been injected into the Info.plist, this will set version to nil
version = $2.sub!(/\s*git sha [\w]+/, )
if (!version)
  version = orig_version

# Inject the git hash into the bundle’s Info.plist
sub = "#{open}#{version}git sha #{sha}#{close}"
puts sub
f.gsub!(re, sub), "w") { |file| file.write(f) }

Make sure that the shell is set to /usr/bin/ruby


Continue reading...

Tags: , , , ,

Bonsoir Redux

stompy » 12 August 2008 » In Bonsoir, Mac OS X » 2 Comments

About 2 years ago R. Tyler Ballance went to BarCampTexas and, finding himself with no place to stay, holed himself up in a coffee shop for the night and knocked up a demo application Bonsoir in about six hours.

When this app is running, it uses Apple’s Bonjour service to find other running Bonsoir apps on the network and allows swapping of AddressBook cards. The idea is that it would be useful at get togethers such as BarCamp for people to swap contact details.

This code has been placed into the public domain but it doesn’t look like anybody is working on it. So I’ve put the code up on github and I’ll see if I can’t make this a more polished app.

Actually, this is prompted a little by guilt. I was looking through my iChat transcripts the other day and I said I’d do some work on it – and that was back in 2006.

Continue reading...

Tags: , , , ,

SOBezier – Getting started.

stompy » 30 July 2008 » In SOBezier » 2 Comments

The first part of this demonstration will create an application that draws a predefined curve in a window that is not re-sizeable. The idea is to get a simple working version up and running in the first place which can then be built upon.

Create the Project

In Xcode, create a new project of type “Cocoa Document-based application”. Save it wherever you like and call it SOBezier. The only project setting that needs to be set is to make sure that Garbage collection is turned on.


MVC Classes

Cocoa makes extensive use of the Model-View-Controller (MVC) paradigm. Model objects are responsible for the data or state of the application. View objects are responsible for displaying the data and the Controller objects keep the two in sync. For this basic application, the model class is SOBezierCurve, a subclass of NSObject, which contains the points of the curve and also knows how to draw itself. The view class is SOBezierView, a subclass of NSView, in which the curve is drawn. These are co-ordinated by the MyDocument class, a subclass of NSDocument, which is the controller.

SOBezierCurve – the model

From the Xcode File menu, choose New File and then select a Cocoa Objective-C class. Call this SOBezier curve. This will create the header and the implementation files. Edit them to look like these code listings.

//  SOBezierCurve.h

#import <Cocoa/Cocoa.h>

@interface SOBezierCurve : NSObject {
    NSPoint pt1;
    NSPoint pt2;
    NSPoint ControlPt1;
    NSPoint ControlPt2;

@property (readwrite) NSPoint pt1;
@property (readwrite) NSPoint pt2;
@property (readwrite) NSPoint ControlPt1;
@property (readwrite) NSPoint ControlPt2;

- (id)initWithPt1:(NSPoint)point1
- (void)drawCurve;


Here, the four points that define the curve are declared and I am using the Objective-C 2.0 @property syntax to make these properties. Two methods are declared. First, an initialiser that creates the curve based on the four points being passed in, and a method that draws the curve.

The implementation is quite simple:

//  SOBezierCurve.m

#import "SOBezierCurve.h"

@implementation SOBezierCurve
@synthesize pt1, pt2, ControlPt1, ControlPt2;

- (id)initWithPt1:(NSPoint)point1
    // Designated initialiser.
    if (![super init]) {
        return nil;
    pt1 = point1;
    pt2 = point2;
    ControlPt1 = cPoint1;
    ControlPt2 = cPoint2;
    return self;

- (void)drawCurve
    NSBezierPath *path = [NSBezierPath bezierPath];
    // Move to the first control point
    [path moveToPoint:ControlPt1];
    // Add a line from the control point to the first point of the curve
    [path lineToPoint:pt1];
    // Create the curve
    [path curveToPoint:pt2 controlPoint1:ControlPt1 controlPoint2:ControlPt2];
    // Draw a line from the end of the curve to the second control point
    [path lineToPoint:ControlPt2];
    // Now draw the curve
    [[NSColor blackColor] set];
    [path stroke];


The initialiser just sets the points of the curve to the points that are passed in. the @sythesize directive lets Objective-C 2.0 create accessors for the properties that are Key Value Coding (KVC) compliant.

The DrawCurve: method is responsible for drawing the curve represented by these points. This is where the terminology becomes confusing with the term ‘bezier’ appearing everywhere. The NSBezierPath class is used extensively in Cocoa graphics, (which is why it is so easy to draw bezier curves), so to draw the curve a series of bezier paths are linked together as a single path and then drawn on the view by stroking this single path.

SOBezierView – the view

This class needs to be creates as we did with the SOBezierCurve class. From the Xcode File menu, choose New File and then select a Cocoa Objective-C NSView subclass. Call this file SOBezierView. This will create the header and the implementation files. Edit them to look like these code listings.

//  SOBezierView.h

#import <Cocoa/Cocoa.h>
@class MyDocument;

@interface SOBezierView : NSView {
    IBOutlet MyDocument *doc;


This class doesn’t do much at the moment so it is quite small. The interface file just declares an outlet called doc to the controller, which is the MyDocument object in this case.

The implementation file only has two methods, both of which come from the superclass so they don’t need to be declared in the header file.

//  SOBezierView.m

#import "SOBezierView.h"
#import "MyDocument.h"
#import "SOBezierCurve.h"

@implementation SOBezierView

- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    return self;

- (void)drawRect:(NSRect)rect {
    [[NSColor yellowColor] set];
    [NSBezierPath fillRect:[self bounds]];
    [[doc curve] drawCurve];


The initWithFrame: method is where the initialisation code will go. But the important method is the drawRect: method which is invoked whenever the view needs to draw itself.

The first thing that the drawRect: method does is set the colour to be used and then calls the NSBezierPath fillRect: method to fill the entire view window with this colour. The rect paramater that is passed into the drawRect: method is the rectangle that is ‘dirty’ and needs to be redrawn, but to keep things simple, this implementation just fills in the whole view. If the view was drawing a number of curves, this dirty rectangle could be used to determine which curves are within this rectangle and the drawing code could be optimised to only draw those curves. The method then uses the outlet to the MyDocument object outlet doc to access curve object which is then asked to draw itself.

MyDocument – the controller

The MyDocument class was created by Xcode as part of the project.

Edit the header file so that it looks like this listing.

//  MyDocument.h
//  SOBezier

#import <Cocoa/Cocoa.h>
@class SOBezierCurve;                          // 1

@interface MyDocument : NSDocument
    SOBezierCurve *curve;                      // 2
@property (readwrite) SOBezierCurve *curve;    // 3
  1. Here SOBezierCurve is declared to be a class.
  2. The curve attribute is declared as a pointer to an SOBezierCurve object.
  3. This attribute is declared as a property.

The MyDocument.m file contains a lot of generated code. For now the only changes need to be made to the beginning of the file. Edit the code so it looks like this listing.

//  MyDocument.m
//  SOBezier

#import "MyDocument.h"
#import "SOBezierCurve.h"                                   
// 1

@implementation MyDocument
@synthesize curve;                                          // 2

- (id)init                                                  // 3
    self = [super init];
    if (self) {
        NSPoint p1 = NSMakePoint(100, 150);
        NSPoint p2 = NSMakePoint(300, 150);
        NSPoint cP1 = NSMakePoint(100, 225);
        NSPoint cP2 = NSMakePoint(300, 75);
        curve = [[SOBezierCurve alloc] initWithPt1:p1
    return self;



  1. Import the header file for the SOBezierCurve class.
  2. This creates KVC compliant accessors for the curve attribute.
  3. The init method, called when the object is created, sets up the points to be used for the default curve and then creates an SOBezierCurve object with these points.

Create the User Interface

Here’s a short screencast of how the UI is created. (If a picture is worth a thousand words, how many more is a video worth?).

Once that is done, you can click build and go to see a simple bezier curve drawn on a yellow background. Not very interesting so far, but it’s a good starting point.

Next Steps

The next step will be to add handles to the points so they can be moved around the view causing the curve to change.

Finally, thanks for your attention and I’d appreciate any comments you have, particularly about things you don’t like, so that I can improve this demonstration.


You can get the source code up to this point from the project’s git repository here. A checkout of the Stage1_1 tag will get the code up to this point.

Alternatively, you can download a zip file of the completed version up to this point here: (60 kb)

Continue reading...

Tags: , , , , ,