oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

LinkBack: Applications Working Together

by Charles Jolley

Have you ever carefully created a chart or graphic and then pasted it into a report only to discover you needed to change it later? Have you ever wished that software would work together so you could make this sort of change with just a few clicks? Well, this is now possible thanks to a new open source technology called LinkBack. Even better, you can add this technology to your own application today and join some of the growing list of Mac developers already supporting it. This article shows you how.

The best part about LinkBack is that it is fairly easy to add it to your own application; it may take as little as a few dozen lines of code. I'll demonstrate this by adding LinkBack to “Sketch”, a sample drawing application that comes with Apple's developer tools. By the end of this tutorial, you'll be able to create a graphic, paste it into Keynote or TextEdit, and then edit the graphic again with just a double click.

To work through this example (or just to play with the results), you will need a copy of the resource kit, available at The kit contains a version of the Sketch source code, a free plugin developed by King Chung Huang that adds LinkBack support to Keynote, a LinkBack-enabled version of TextEdit, and a finished version of the example application. It's fun and informative, so go get the kit now (and run the plugin installer if you have Keynote). Done? OK then, let's get started.

How LinkBack Works

Related Reading

Cocoa in a Nutshell
A Desktop Quick Reference
By Michael Beam, James Duncan Davidson

LinkBack is built on the trusty client and server model. The server is the application that creates the content in the first place and then edits it later. The client is the application that requests an edit from the server. For example, if you create a graphic and then paste it into a word processing document, the graphics application would be the server and the word processor the client.

Communication between client and server applications occurs primarily through OS X's pasteboard mechanism. LinkBack provides additional methods to handle anything that cannot be done with pasteboards alone. This means that most of the code you need to support LinkBack is already done in your Copy and Paste code; it just needs to be tweaked a bit.

Creating a LinkBack Server

There are five major steps to creating a LinkBack server application:

  • Include the special LinkBack data type whenever you place data on the pasteboard.
  • Register to listen for edit requests coming from the client application.
  • Handle edit requests when you receive them.
  • Send the edited content back to the client application.
  • Handle closing conditions.

The rest of this tutorial will take you through these steps for the Sketch application.

Step 1: Share the Wealth

The first step in supporting LinkBack is to include the special LinkBack data type along with your normal data types whenever you place content on the pasteboard. This data will be stored by client applications for later use when they request an edit.

In Sketch, this happens in SKTGraphicView's -copy: method, which is used for both the Copy and Cut operations. Modify this method with the highlighted changes shown in the code below:

- (IBAction)copy:(id)sender {
	NSData* data ;
	NSArray *orderedSelection = [self orderedSelectedGraphics];
	if ([orderedSelection count] > 0) {
	  SKTDrawDocument *document = [self drawDocument];
	  NSPasteboard *pboard = [NSPasteboard generalPasteboard];

	  [pboard declareTypes:[NSArray arrayWithObjects:
	    SKTDrawDocumentType, NSTIFFPboardType, NSPDFPboardType, 
	    LinkBackPboardType, nil] owner:nil];	

	  // save the pboard data for LinkBack.
	  data = [document drawDocumentDataForGraphics: 
	    orderedSelection] ;
	  [pboard setPropertyList: [NSDictionary 
	   linkBackDataWithServerName: @"sketch" appData: data] 
	   forType: LinkBackPboardType] ;


The LinkBack data object you add to the pasteboard is a dictionary object. It contains a server name, which we will discuss in the next step, the application data that you provide, and some additional data provided by LinkBack.

The most important part of this is the application data you provide. When a client requests an edit, you will need to use this application data to display the content for editing. In the case of Sketch, we will simply write out the selected graphics in the format used for Sketch's private pasteboard type.

Run Sketch now, create a graphic, and paste it into a Keynote or TextEdit+LinkBack. Since the LinkBack data was included when you pasted it into the client, you can now double-click on the graphic and the client will try to request an edit. But you won't get an edit. You will get an error instead.

LinkBack error

Why is this? Well, you haven't completed the next step of our tutorial yet.

Step 2: Listening for Edit Requests

To be notified when a client wants your application to edit some content, Sketch needs to register as a LinkBack server. This step involves two parts. The first part requires one line of code and the second part requires no code at all.

Registering for Requests

To received edit requests, you need to tell LinkBack that you are interested in receiving edit requests. You do this with a line of code that needs to be called shortly after your application launches. The best place to put this is in the -applicationDidFinishLaunching: method of your application delegate, which is exactly what we will do for Sketch. Add the line of code highlighted below to this method in the SKTDrawAppDelegate:

- (void)applicationDidFinishLaunching:(NSNotification *)notification { 
	[LinkBack publishServerWithName: @"sketch" delegate: self] ;
	[self showToolPaletteAction:self];

By now you've probably noticed a trend with this “sketch” server name thing. The server name is used to identify the part of your application that responds to edit messages. It is actually possible to have more than one LinkBack server in each application, though not very common. The point here is you need to pick a server name and stick with it throughout your application.

Adding LinkBack to Your Info.plist

The Info.plist is a file found in your application bundle that contains information about your software. Mac OS X uses this information to find out what types of files your application can open, among other things. LinkBack uses this information to determine your level of LinkBack support. You need to add two special keys to this file:

  • The LinkBackServer key is used to identify the server name you will register with when launched. LinkBack uses this method to find the server application to launch when a client requests an edit. For Sketch the value of this key is...“sketch”, of course.
  • The LinkBackApplicationURL key contains a link to your software's home page. If a user tries to edit some content created by your application but your application is not installed, LinkBack will offer to take them to the URL you provided so they can download your software.

In the interest of keeping this tutorial short, the Sketch application in the resource kit already has these two keys added for you. If you add LinkBack to your own application, however, keep in mind that you will need to add these keys to your Info.plist as well.

That's it for step two. Now that you are listening for edit requests, you need to handle them. This is what we will do in step 3.

Pages: 1, 2

Next Pagearrow