Installing things to /usr/bin – Trampolines

Some Mac OS X applications can install useful tools to use with Terminal. PDFKey Pro is one of those applications. Currently I simply installed a binary called “pdfkeypro” in /usr/bin via a .pkg file. This had the obvious draw back of needing the user upgrade it manually on every release.

Some applications, like the excellent TextMate place a symlink to an executable inside the application bundle, solving said problem. But introducing another one: as soon as the application bundle is moved, the symlink breaks.

As I am about to update the command line tools for PDFKey Pro, I started thinking about a better way to install said binaries. One which would update automatically when the main bundle is updated, but wouldn’t break if said bundle is moved in another location.

Trampolines to the rescue! Instead of installing the real tools I install 2 trampoline applications in /usr/bin called respectively pdflock and pdfunlock. The trampoline then uses NSWorkspace to locate the bundle, and starts the real executable found in the application’s bundle. The code of a trampoline looks like this:

int main(int argc, char *argv[]) 
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSWorkspace *env = [NSWorkspace sharedWorkspace];
	NSString *app = [env absolutePathForAppBundleWithIdentifier:@"com.pdfkey.pdfkeypro"];
	NSString *targetPath = [[app stringByAppendingPathComponent:@"Contents/Resources/pdflock"] retain];

	const char *CStringPath = [targetPath UTF8String];
	[pool release];

	execv(CStringPath, argv);

	// You reach this code only if execv returns, which means that something wrong happened.
	[targetPath release];
	printf("PDFKey Pro is not installed. Please download it from http://pdfkey.com\n");
	return 0;
}

I can already read the comment that I could use argv[0] instead of hardcoding the application name. That’s true, but this trampoline once written should not be ever modified again and I want it to perform the minimum number of instructions required to work.

You can use this code liberally to make your very own trampoline, consider this to be of public domain. You can also find it at codebeach.

As a side note, I wonder why most application installing things in /usr/bin usually have their very own installer. Why wouldn’t a .pkg do? This is a sincere question, not an attempt to flame. In PDFKey Pro all I do is bundle a .pkg file and provide a menu item which launches it. It couldn’t be easier!

5 Responses to “Installing things to /usr/bin – Trampolines”

  1. Blake C. Says:

    This is what usr/local/bin is for. You may want to just have your app look for the tool there and copy it over if it’s missing. Tell your users to add usr/local/bin to their $PATH and you’re done…

  2. Michele Balistreri Says:

    /usr/local/bin still requires root privileges for writing to it 😀


    drwxr-xr-x 42 root wheel 1428 Jan 6 21:56 bin

  3. trampolines Says:

    Trampolines can be great fun – but don’t do what I did over Christmas and go for a bounce after a few too many drinks. I fell awkwardly and pulled some tendons – very painful

  4. Locksmith Knoxville TN Says:

    For reasons uknown i’m getting a blank page while i attempt to post a comment,do you know reason why its taking place?i’m employing oprea web-browser

  5. WilliamRamsai Says:

    My sister is quite frustrated. Nobody knew this, however the support is hard to reach.