Subscribe to
Posts
Comments
NSLog(); Header Image

The Hacks We Write

Sometimes we write stand-in code: code that does basically the same thing as what we want, but in a cheap, easy fashion. Have a look at this example from MailDrop 2. Instead of putting up a dialog box we'd have to create in Interface Builder, wire up, properly handle and dispose of, etc., we took the easy way out until we could get around to doing it properly.

NSAppleScript *as = [[NSAppleScript alloc] initWithSource:@"text returned of
    (display dialog \"Name It!\" default answer \"Custom Variable\" giving
    up after 10)"];
NSAppleEventDescriptor *aed = [as executeAndReturnError:NULL];
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:
    ([aed stringValue] ? [aed stringValue] : @"Custom Variable")
    action:nil keyEquivalent:@""];

Cheap and easy. And quite a bit of a hack. 😀

3 Responses to "The Hacks We Write"

  1. Cool hack. I've never used AppleScript from Cocoa, and hadn't realized it was that easy. I'll have to remember that... 🙂

  2. Isn't it stunning? The entire Cocoa framework, XCode / IB and the ObjC language are what other people (you know, on that "other" platform) would consider RAD tools, and probably the most powerful ones at that. I have already seen the expression in the face of Windows people when you show them what you can do in Cocoa. Just when they thought .NET and C# were the greatest thing since sliced bread... Now imagine the look when you tell them that sometimes, you allow yourself the luxury of being even lazier than that by patching your code with Applescript "stand-ins" - you gotta love this 😉

  3. Were it not for the fact that NSAppleScript has a leak (http://blog.kung-foo.tv/archives/001135.php).