Getting your Mac to talk with NSSpeechSynthesizer

No Comments

When I got a Mac mini in 2008, one of the coolest things it could do was speaking. That is an accessibility feature, but not being disabled never prevented me from staying hours typing random stuff on terminal just to get to hear the computer talking.

It turns out we developers can use that feature too. And it just takes a few lines of code. Start by opening Xcode and creating a new project.

Screen Shot 2015-06-10 at 7.34.30 AM More

FVGetApp, an open source class to get and set default apps

No Comments

I’m pleased to announce the release of FVGetApp, an open source class to get and set default apps on Mac OS X. Say your Cocoa app needs to change the default browser. Not only FVGetApp can change it, but it also can get a list of apps that can handle the HTTP protocol. FVGetApp’s not limited to HTTP and HTTPS. It’s also possible to deal with any other protocol like mailto, ichat and ftp.

You can also deal with MIME types and extensions. So if you created an audio player and you want to let the user change the default app for MP3 files with a click, you will like my class too.

As a plus you can also check if an app can open a certain file.

I developed and tested it on Mountain Lion, but it should work on previous versions of Mac OS X. Adapting it to non-ARC code would take less than 1 minute. You can download a ZIP file containing the class, documentation and a test app by clicking here. To help you to understand the capabilities of this class before downloading the ZIP, here’s a method list:

+(BOOL)isAppCompatible:(NSURL *)appURL withFile:(NSURL *)fileURL;
+(NSDictionary *)appForProtocol:(NSString *)protocol;
+(NSDictionary *)appForMIMEType:(NSString *)MIMEType;
+(NSDictionary *)appForExtension:(NSString *)extension;
+(NSArray *)allAppsForProtocol:(NSString *)protocol;
+(NSArray *)allAppsForMIMEType:(NSString *)MIMEType;
+(NSArray *)allAppsForExtension:(NSString *)extension;

RIP Sensitive Code

No Comments

Hey folks, unfortunately, my blog Sensitive Code is dead. I had no time to update it and ended up not renewing the domain registration. It had a great page rank. For example, if you type UIFileSharing on Google, a post from Sensitive Code it is the first result(not sure for how long). I moved every post from there to this blog, so everything is still available. I hope to write about programming on this blog in the future.

Easy multithreading, a courtesy of NSOperation

No Comments

Threading is known for being very hard in any language. A foolish mistake can make your application crash. Programmers spend a lot of time to make sure everything works fine. On Mac OS X 10.5, Apple finally introduced a very easy way to deal with threading. NSOperation is The Apple Way® to deal with threading. All you have to do is to subclass the NSOperation, create and initialize a NSOperationQueue, create and initialize the NSOperation and add it to the NSOperationQueue. For this tutorial, I will teach you how to write a simple app that will load a RSS feed and display it on a NSTableView.

To get started, go to Xcode and create a new project. I will call mine RSS Reader, but feel free to call it whatever you want. Let’s start by editing our AppDelegate.h. Create 3 IBOutlets:


Audio streaming with QTKit

No Comments

Audio streaming can be a painful task. Download the audio data and feed an AudioQueueBuffer may be a risky operation and may involve many lines of code. So finally, on Mac OS X 10.6 Apple has introduced QuickTime X. We must remember that QuickTime doesn’t necessarily mean QuickTime Player. In this case, I’m talking about the QTKit framework. QTKit(AKA QuickTime Kit) is a high level framework for dealing with media. The QTKit framework was also updated and now includes this and other new features.


[Quickies]Shaking a NSWindow

No Comments

On the Mac OS X login window, when you don’t type the right password, the window shakes. It’s a very cool and classic way to tell people the password is wrong. Even WordPress copied it. You can reproduce the same behavior on an ordinary NSWindow with just 7 lines of code! Here’s how it works:

//Gets the window rect
NSRect rect = [window frame];
//Shake offset amount
int amount = -10;
//Shake the window 5 times
for(int i = 0; i < 5; i++){
//Changes the frame origin
[window setFrameOrigin:NSMakePoint((rect.origin.x + amount), rect.origin.y)];
//Pause the thread for 0.04 seconds
//Inverts the offset amount
amount *= -1;
//Restore the window’s original position
[window setFrame:rect display:NO];

Download the sample app here.

[Quickies]IRCClient, IRC made easy

No Comments

Have you ever needed/wanted to implement an IRC chat on your Cocoa application? It might be easier than you think! There’s a Cocoa “bridge” for the libircclient library. The lib was written by Georgy Yunaev. Get the files on this link. These few lines of code will make it work:

IRCClientSession *session = [[IRCClientSession alloc] init];
MyIRCClientSessionDelegate *controller = [[MyIRCClientSessionDelegate alloc] init];

[session setDelegate:controller];
[controller setSession:session];

[session setServer:@””];
[session setPort:@”6667″];
[session setNickname:@”test”];
[session setUsername:@”test”];
[session setRealname:@”test”];
[session connect];

[session run]; //starts the thread

[Book]Cocoa Programming for Mac OS X

1 Comment

People ask me very often what they should do to get started in programming. I usually tell them to read books. And what’s the best book for those who want to learn Mac programming? It’s Cocoa Programming forMac OS X by Aaron Hillegass. This book is the best, it simple defines what we are, it’s our bible. The book considers you already know a little C or C++ and teaches you everything a decent Mac developer should know: from a hello world to printing, web services and Core Animation.

The guy who wrote this book can be considered the world’s best Cocoa teacher. He taught Objective-C at companies like Intel, NeXT, Apple and Microsoft. He is also the own of Big Nerd Ranch, a very famous training center.

Now let me tell you guys about my faith testimony. I started to try to learn Cocoa in the beginning of 2008, I had gave up and started to look for alternatives to Objective-C. I mean, that syntax looked so weird. A very long time after that, in the end of 2009, Lars Bergstrom told me about this book. In less than a month I was writing my first commercial app.

At the time I’m writing this, it costs $30.24 USD on Amazon. It’s not the world’s cheapest book, but it isn’t the most expensive one. It’s worth investing on a copy. Get it on Amazon.

Share and Enjoy:

[Quickies]Opening URLs in external applications

No Comments

Opening URLs on external applications is easy, no matter if you’re on an iPhone/iPod touch/iPad or on a Mac. On the Mac, we’d do something like this:

NSURL *URL = [NSURL URLWithString:@””];
[[NSWorkspace sharedWorkspace] openURL:URL];

Simple, uh? Now let’s see how to do this on the iOS:

NSURL *URL = [NSURL URLWithString:@””];
[[UIApplication sharedApplication] openURL:URL];

Easy peasy 🙂

[Quickies]Putting a NSXMLDocument into a NSTextView

Comments Off on [Quickies]Putting a NSXMLDocument into a NSTextView

Say you want to display a XML to the user on a NSTextView. You have a NSXMLDocument called “xmldocument” and you need to put into a NSTextView(you created an IBOutlet called “xmlTextView” in order to make your code interact with the NSTextView). You’d just use this code:

//Convert the NSXMLDocument to NSData
NSData *data = [xmldocument XMLDataWithOptions:NSXMLNodePrettyPrint];
//Create a string from the NSData object you have
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//Create a NSAttributedString from it
NSAttributedString *attributedString = [[[NSAttributedString alloc] initWithString:string] autorelease];
//Now set the attributed string for the NSTextView
[[xmlTextView textStorage] setAttributedString:attributedString];

Older Entries