Forum Navigation
Forum breadcrumbs - You are here:WeLoveGod RallysPublic Forums: fbcocoaOpenWith
You need to log in to create posts and topics.

OpenWith

Posted by: svanvoorst <svanvoorst@...>

I am trying to create an objC editor which will open, edit, and save .m files.   I would like to have files that are saved by this app to open into the app when double clicked.  Currently each .m file that I create is opened into Xcode when double clicked.  When I try to change this behavior by using GetInfo or OpenWith... I receive the attached error message stating that "myApp" cannot open files in the "Whatever I set it to source code" format.  However, "myApp" is able to open the same file using NSOpenPanel without any problem, so in reality the error message is incorrect (apparently OSX thinks otherwise).   I have unsuccessfully tried adding a UTExportedTypeDeclarations key to the Info.plist (see below).  Furthermore, I set UTTypeConformsTo public.source-code which supposedly encompasses objective-c source code.  I also tried adding Document types to the Info.plist.  These settings are similar to TextWrangler, which will successfully open a .m file when set by GetInfo or OpenWith...  So I know that it can be done.  Obviously I do not have the correct settings, but by now have tried so many things that I have a giant mess.  As time permits, please use the source code below to create an application with an NSTextView, capable of opening and saving files and see if you can create a file which will open up into "myApp" when double clicked upon.  I prefer that it be a .m file, but right now would be happy for any file type that works correctly.  The code is in FB, but could be moved to Xcode if you prefer.  Also, I do not necessarily want to completely stop Xcode from opening .m files; only when I say that it's ok.

'--------- start -------

BeginCDeclaration
@interface AppDelegate : NSObject
{
  NSWindow *window;
  NSTextView *txtView;
}
- (void) createMenu;
- (void) createWindow;
- (void) openFile;
- (void) saveFile;
@end
EndC
BeginCFunction
@implementation AppDelegate : NSObject 
- (void) dealloc
{
  [window release];
  [super dealloc];
}
- (void)saveFile
{
 NSSavePanel *sp = [NSSavePanel savePanel];
 [sp setTitle:@"Save TextView contents to file"];
 [sp setAllowedFileTypes:[NSArray arrayWithObjects:@"txt",@"m",@"xyz", @"rtf",nil]];
 [sp setCanSelectHiddenExtension:YES];
 [sp setAllowsOtherFileTypes:YES];
 [sp setCanCreateDirectories:YES];
 NSInteger panelResult = [sp runModalForDirectory:nil file:@"Untitled.txt"];
  if ( panelResult == NSFileHandlingPanelOKButton )
  {
    NSString *viewStr = [[txtView textStorage] string];
    NSURL *url = [sp URL];
    [viewStr writeToURL:url atomically:YES encoding:NSUTF8StringEncoding error:NULL];
  }
    else
  {
    // User cancelled
  }
}
- (void)openFile
{
 NSOpenPanel *op = [NSOpenPanel openPanel];
 [op setTitle:@"Open generic text file"];
 [op setCanChooseFiles:YES];
 [op setCanChooseDirectories:NO];
 [op setAllowsMultipleSelection: NO];
 [op setAllowedFileTypes:[NSArray arrayWithObjects:@"rtf", @"rtfd",@"txt",@"m",@"",@"xyz", @"c", nil]];
 if ( [op runModal] == NSOKButton )
 {
    NSURL *url = [op URL];
    NSString *fileStr = [[NSString alloc] initWithContentsOfURL:url 
encoding:NSUTF8StringEncoding error:NULL];
    [txtView setString:fileStr];
  }
     else
  {
    // User cancelled
  }
}
- (void) createMenu
{
// ******** Menu and menubar ********//
  id menubar = [[NSMenu new] autorelease];
  id appMenuItem = [[NSMenuItem new] autorelease];
  [menubar addItem:appMenuItem];
  [NSApp setMainMenu:menubar];
  id appMenu = [[NSMenu new] autorelease];
  id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:@"Quit"
        action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
  [appMenu addItem:quitMenuItem];
  [appMenuItem setSubmenu:appMenu];
}
- (void) createWindow
{
  // ***** window ***** //
  #define _wndW  700
  #define _wndH  550
  window = [[NSWindow alloc] initWithContentRect: NSMakeRect( 0, 0, _wndW, _wndH )
                       styleMask: NSTitledWindowMask | NSMiniaturizableWindowMask | NSClosableWindowMask
                       backing: NSBackingStoreBuffered
                       defer: NO];
  [window center];
  [window setTitle: @"Test window"];
// ****** NSTextView with Scroll/Ruler ****** //
NSScrollView *scrlView = [[NSScrollView alloc] initWithFrame:NSMakeRect( 30, 70, _wndW - 60, _wndH - 70 )];
[scrlView setHasVerticalScroller: YES];
txtView = [[NSTextView alloc] initWithFrame:NSMakeRect( 0, 0, _wndW - 60, _wndH - 70 )];
[scrlView setDocumentView: txtView];
[[window contentView] addSubview:scrlView];
[txtView insertText:@"Enter text - Right click for menu."];
[scrlView setHasHorizontalRuler:YES];
[scrlView setRulersVisible:YES];
[window makeFirstResponder:txtView];
[scrlView release];
[txtView release];
// ***** Open btn ***** //
 NSButton *openBtn = [[NSButton alloc] initWithFrame:NSMakeRect( 350, 30, 100, 30 )];
 [openBtn setBezelStyle:NSRoundedBezelStyle ];
 [openBtn setTitle: @"Open" ];
 [openBtn setTarget:self];
 [openBtn setAction:@selector(openFile)];
 [[window contentView] addSubview: openBtn];
 [openBtn release];
// ***** Save btn ***** //
 NSButton *saveBtn = [[NSButton alloc] initWithFrame:NSMakeRect( 450, 30, 100, 30 )];
 [saveBtn setBezelStyle:NSRoundedBezelStyle ];
 [saveBtn setTitle: @"Save" ];
 [saveBtn setTarget:self];
 [saveBtn setAction:@selector(saveFile)];
 [[window contentView] addSubview: saveBtn];
 [saveBtn release];
// ***** Quit btn ***** //
 NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 130, 30, 95, 30 )];
 [quitBtn setBezelStyle:NSRoundedBezelStyle ];
 [quitBtn setTitle: @"Quit" ];
 [quitBtn setAction:@selector(terminate:)];
 [[window contentView] addSubview: quitBtn];
 [quitBtn release];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification
{
  [self createMenu];
  [self createWindow];
}
- (void) applicationDidFinishLaunching: (NSNotification *)notification
{
  [window makeKeyAndOrderFront: nil];
}
@end
EndC
BeginCCode
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
  [NSApplication sharedApplication];
  [NSApp setDelegate: [AppDelegate new]];
  [NSApp run];
  [pool drain];
EndC

'------- end -------

'------ Info.plist xml ------

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

<plist version="1.0">

<dict>

<key>CFBundleDocumentTypes</key>

<array>

<dict>

<key>CFBundleTypeName</key>

<string>My Special Source Code</string>

<key>CFBundleTypeRole</key>

<string>Editor</string>

<key>CFBundleTypeOSTypes</key>

<array>

<string>txt</string>

<string>TEXT</string>

<string>m</string>

</array>

<key>CFBundleTypeExtensions</key>

<array>

<string>m</string>

<string>xyz</string>

<string>txt</string>

<string>bas</string>

</array>

<key>LSHandlerRank</key>

<string>Owner</string>

</dict>

</array>

<key>CFBundleExecutable</key>

<string>myApp</string>

<key>CFBundleIconFile</key>

<string></string>

<key>CFBundleIconFiles</key>

<string></string>

<key>CFBundleName</key>

<string>myApp</string>

<key>CFBundleIdentifier</key>

<string>com.myCompany.myApp</string>

<key>CFBundleSignature</key>

<string>Yapp</string>

<key>CFBundleVersion</key>

<string>1.0</string>

<key>CFBundlePackageType</key>

<string>APPL</string>

<key>CFBundleInfoDictionaryVersion</key>

<string>6.0</string>

<key>NSHumanReadableCopyright</key>

<string>S.Van Voorst 2013</string>

<key>NSPrincipalClass</key>

<string>NSApplication</string>

<key>UTExportedTypeDeclarations</key>

<array>

<dict>

<key>UTTypeDescription</key>

<string>myApp source file</string>

<key>UTTypeConformsTo</key>

<array>

<string>public.source-code</string>

</array>

<key>UTTypeIdentifier</key>

<string>com.myCompany.myApp-source</string>

<key>UTTypeTagSpecification</key>

<dict/>

</dict>

</array>

</dict>

</plist>

'------ end plist -------

Thanks in advance for any help.

Steve Van Voorst


To unsubscribe, e-mail: fbcocoa-unsubscribe@welovegod.org
For additional commands, e-mail: fbcocoa-help@freegroups.net