On Objective-C and Cocoa

Filed under: Apple,Cocoa | No Comments »

I recently delved into the world of desktop programming by learning Objective-C / Cocoa. My guide was the excellent Cocoa Programming for Mac OS X 3rd edition which was written by Aaron Hillegass of Big Nerd Ranch fame. I haven’t read a computer book cover-to-cover in a while which is a testament to the quality of Aaron’s book. One thing to know going in is that he doesn’t spend much time teaching Objective-C as it’s not a large language, especially if you know C. The book is mostly centered around the APIs you’ll be using to make applications. Apple has a good Objective-C primer that will get you up to speed on the language front. I wouldn’t recommend Objective-C/Cocoa as a first programming environment, but it’s easy enough to follow if you know some others.

I completed all the exercises in Cocoa Programming for Mac OS X, but there’s a difference between being book smart and actually writing applications. I decided to scratch an itch I’ve had for months and write a screensaver that displays meta data about the JPGs its displaying. Specifically the IPTC “Caption/Abstract” field which is utilized religiously by a feed of high-quality AP photos I subscribe to. The built in photo screensaver simply displays them, but I often want to know what the photo is of and it’s a pain to stop the screen saver, go to the directory, hunt through 200 images just to find the one I want the caption for.

ap screensaverIt’s is a really simple app which is perfect for a first time project. In fact it’s all a single subclass of ScreenSaverView. My first challenge was how to pull the meta information and I ended up going with the ImageIO framework. Then I needed to know how to draw it on the screen, that’s simple enough with drawInRect:withAttributes:, but calculating the rectangle proved a little tricky since it should be as wide as the screen and variably tall based on the amount of text. It’s still not perfect, but using sizeWithAttributes: let’s me know how much area it will take. It would be really handy to have a height:WithWidth:WithAttributes: method.

My images are all quite large (typically larger than 6 megapixels) so they have to be scaled correctly for display. I hit a wall here because the images were originally coming up much smaller than I knew they were, the reason was resolution (thanks!).

Then I hooked up ScreenSaverDefaults and configureSheet to allow users to choose a source folder and speed. Nothing special here. I’ll probably add an option for random display at some point.

The resulting app is exactly what I wanted. It looks spectacular on both my 24″ display and 50″ TV (hooked into a Mini running a separate instance of the feed and screensaver). I often have the AP photos on when people come over because it’s such a great conversation starter, now with having the captions available we might learn a thing or two. Sadly this could be a competitive cable news station–it’s already more informative than CNN/Fox.

Mission accomplished.

Read the latest posts

Leave a Reply