Displaying articles with tag

Pulling out your Mac app version number

Posted by lori, Wed Sep 16 23:14:00 UTC 2009

File this one under useful snippets for Mac Cocoa development. How to extract the application version number out of your app's info.plist file.


[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

0 comments | Filed Under: | Tags:

Creating application support files on the Mac

Posted by lori, Sat Sep 05 09:04:00 UTC 2009


You are writing a Mac Cocoa application, and you know you want to store your user specific application support files in a predictable location, like ~/Library/Application Support/<Application>/. So... how exactly do we do that?

Once again, Google to the rescue, and we find the answer at Cocoa Dev Central. This will not only construct the file name for you, but it will create the directory if it doesn't exist, too.

- (NSString *) pathForDataFile { 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    NSString *folder = @"~/Library/Application Support/MyApplication/"; 
    folder = [folder stringByExpandingTildeInPath]; 

    if ([fileManager fileExistsAtPath: folder] == NO) { 
        [fileManager createDirectoryAtPath:folder attributes: nil]; 
    }

    NSString *fileName = @"MyApplication.mysettings"; 
    return [folder stringByAppendingPathComponent: fileName];
} 

Again, an example that just works. I'm on a roll today.

0 comments | Filed Under: | Tags:

Cocoa Sheets

Posted by lori, Wed Sep 02 15:28:00 UTC 2009

I was struggling with my Photoshop plugin again. The plugin window is modal. But then I need to pop up a modal window, in order to prompt the user for new name/description for the user-saved presets. I was puzzling and Googling, and then I stumbled across a reference to "sheets". Bingo!

This page has the best little example of sheet usage. I was able to copy/paste and then a couple of tweaks, and a couple IB bindings and it all just worked.

- (IBAction)openSheet:(id)sender
{
	[NSApp beginSheet: theSheet
			modalForWindow: theParent
			modalDelegate: self
			didEndSelector: @selector(sheetDidEnd: returnCode: contextInfo:)
			contextInfo:NULL];
}

- (IBAction)theSheetOK:(id)sender
{
	[NSApp endSheet:theSheet returnCode: NSOKButton];
	[theSheet orderOut:nil];
}

- (IBAction)theSheetCancel:(id)sender
{
	[NSApp endSheet:theSheet returnCode: NSCancelButton];
	[theSheet orderOut:nil];
}

- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
	if (returnCode == NSOKButton)
		NSBeep();
}

I really love code examples that just work.

0 comments | Filed Under: | Tags:

Interesting endless loop

Posted by lori, Wed Jul 08 14:24:00 UTC 2009

Cocoa-in-Carbon application. Note to self. Do NOT "freeze-dry" a controller object in your NIB file, especially if your controller object opens your NIB file in it's init method.

Just sayin.

0 comments | Filed Under: | Tags: