Why does the following code work ? It's a small Cocoa program that uses NSOpenPanel to select a file and open it in Emacs.app. It can be run from the command line with the starting directory as an argument.
How does NSOpenPanel run without invoking NSApplication or NSRunLoop ? What are the limitations on a Cocoa program that doesn't explicitly start NSApplication or NSRunLoop ? I would have thought one of them was: you can't use any kind of GUI. Perhaps by invoking NSOpenPanel, some fallback code being called that invokes NSRunLoop ? I put breakpoints on +[NSApplication alloc] and +[NSRunLoop alloc] and they were not triggered.
main.m:
#import <Cocoa/Cocoa.h>
NSString *selectFileWithStartPath(NSString *path) {
  NSString *answer = nil;
  NSOpenPanel* panel = [NSOpenPanel openPanel];
  panel.allowsMultipleSelection = NO;
  panel.canChooseFiles = YES;
  panel.canChooseDirectories = NO;
  panel.resolvesAliases = YES;
  if([panel runModalForDirectory:path file:nil] == NSOKButton)
    answer = [[[panel URLs] objectAtIndex:0] path];
  return answer;
}
int main(int argc, const char * argv[]) {
  NSString *startPath = argc > 1 ? [NSString stringWithUTF8String:argv[1]] : @"/Users/Me/Docs";
  printf("%s\n", argv[1]);
  BOOL isDir;
  if([[NSFileManager defaultManager] fileExistsAtPath:startPath isDirectory:&isDir] && isDir) {
    system([[NSString stringWithFormat:@"find %@ -name \\*~ -exec rm {} \\;", startPath] UTF8String]);
    NSString *file = selectFileWithStartPath(startPath);
    if(file) [[NSWorkspace sharedWorkspace] openFile:file withApplication:@"Emacs.app"];
  }
}