TweetFollow Us on Twitter

TinyViewer, Part Deux

Volume Number: 19 (2003)
Issue Number: 7
Column Tag: Programming

Getting Started

TinyViewer, Part Deux

by Dave Mark

Last month's column was a lot of fun. So much so, that I was really torn on what to do in this month's column. I really want to continue the iPod notes exploration. And I've been playing with these cool AppleScripts that I'm just dying to write about. Then there's TinyViewer, the app from my May column. I really want to finish playing with Interface Builder and updating the TinyViewer interface. So that's what I'm gonna do this month and we'll start on the other stuff next month.

TinyViewer Recap

In last month's column, we used Interface Builder to build the beginnings of an interface for TinyViewer, our Cocoa update of the old PictViewer app. We used Project Builder to create a Cocoa Application project, then double-clicked on the MainMenu.nib file to open the nib file using Interface Builder.

We went through the four interface windows that appear - the app's main window, the menu bar window, the palette window, and the window labeled MainMenu.nib, which is a central repository for all the resources and instances from your project.

We dragged out an NSImageView from the palette window into the main app window, then used the dashed, blue lines to align and resize the NSImageView inside the window. We selected Show Info from the Tools menu and used the Info window to customize our NSImageView so it was Editable and Enabled. Making it Editable was especially important to our project as it allowed us to drag-and-drop an image on the window.

We also used the Info window to set the Autosizing settings, so when we resized the window, the image resized in a pleasing way.

Finally, we did the coolest thing of all. We used Interface Builder to take the interface for a spin by selecting Test Interface from the File menu. I absolutely love this part of Interface Builder. They idea of prototyping an app, then test driving the interface, all without writing a single line of code, is amazingly useful. But beyond that, I love the fact that you can archive complex object hierarchies inside a .nib file, then bring them to life inside a completely different application. Think about this. You design and test the TinyViewer interface inside Interface Builder, then save the .nib file, wheel over to Project Builder, build and run the app, and the exact same interface appears. Again, all without writing a single line of code.

Think back to your ResEdit days. You could edit the elements that made up the interface, even do a bit of stringing them together (though not much), but when it came time to bring them to life, you had to write a lot of code. Even though PowerPlant allowed you to create all the usual interface elements and nest them within nestable views, you still had to write the code that instantiated all your objects and brought them to life.

Interface Builder brings this process to a new level. And I like it.

Now What Do We Do?

OK, so what's next? Take a look at Figure 1. This is the menu that appears when you ask Project Buillder to build and run the TinyViewer application. A couple of notes here. First, the most eagle-eyed among you might have spotted a slight discrepancy between the title of this menu and what you see on your screen. Nicely done. When you run your app inside of Interface Builder, Interface Builder actually breathes life into your objects and yields control. In effect, Interface Builder turns into your running app, until you quit. You'll know this is happening cause your app's menu title will actually say Interface Builder.


Figure 1. TinyViewer's main menu.

When you run your app in Project Builder, Project Builder actually launches your app. As you can see in Figure 1, this version of TinyViewer features a TinyViewer menu, riddled with the generic name NewApplication. Let's change this menu to something a little more appropriate.

    Note that when you run your app under Interface Builder, the IB dock icon changes into a cool, single-pole, double-throw switch (ya know, like the one Gene Wilder threw to bring life to Young Frankenstein). Quit the app and the old familiar IB icon returns. Since your app is not running as a separate process, there's no separate dock icon for your app.

But when ProjectBuilder runs your app, you'll find your app represented by an elegantly designed dock icon distinct and separate from the Project Builder icon (and by elegant, I mean generic and boring, unless of course you've designed one yourself).

Changing the TinyViewer Menu

Quit your app if it is running, then (if you are not already in Interface Builder) double-click MainMenu.nib in the project window (Files tab, TinyViewer/Resources) to open the .nib file in Interface Builder. Find the MainMenu.nib main window (see Figure 2) and double-click on the MainMenu icon.


Figure 2. The MainMenu.nib window, with MainMenu selected.

When the MainMenu window appears, start by double-clicking the left -most menu title, the one that says NewApplication, and changing it to say TinyViewer.


Figure 3. Editing the TinyViewer menu title.

Next, click on the menu title TinyViewer. A menu should appear. Find the three occurrences of NewApplication and change them all to say TinyViewer (see Figure 4). Make the same change to the single occurrence of NewApplication in the Help menu.


Figure 4. The complete TinyViewer menu.

Change the Window Title

Next step: close the menu window and click on the TinyViewer window to bring it to the front. Now select Show Info from the Tools menu to bring up the inspector window. If you did this correctly, the inspector window will be titled NSWindow Info. Make sure Attributes is selected from the popup menu and change the window's title to TinyViewer. As soon as you hit enter or click out of the field, the TinyViewer window's title should change to TinyViewer.

    Very important! Be sure to save your .nib file in Interface Builder if you want to go back to Project Builder and test your changes.

Figure 5 shows my copy of TinyViewer with a picture dragged in and the Window's title changed. In a future column, we're going to add some code to the project to change the window title to the file name of the drag-and-dropped image. Any idea how to do this? One solution is to subclass NSImageView and insert your own dragging completion code. We'll also add in an NSScrollView to make the NSImageView scrollable. This one is doable completely within Interface Builder if you want to take a crack at this yourself.


Figure 5. TinyViewer in action. That's Ryan getting rid of my ball.

Create the About Panel

In the olden days of Mac development, the About Box was a modal dialog box with some text in it. Over time, people started customizing these dialogs and, eventually, replaced the canned About Box with customized windows. Some (Adobe PhotoShop, for example) even went so far as to write their own custom window procedures, leading to odd shaped windows, sometimes with holes in them.

Over time, the "official" method of producing About Boxes evolved, moving from About Boxes to About Windows to the current model, the About Panel. There's a terrific article on cocoadevcentral.com by Brock Brandeberg, that'll take you on a whirlwind tour of the modern About Panel:

http://cocoadevcentral.com/articles/000071.php

I'll give you the short version here, but definitely take a look at Brock's article. Well worth the read.

Just as the original Toolbox contained a canned routine for putting up an About Box, Cocoa's NSApplication class contains a member called orderFrontStandardAboutPanel:

- (void)orderFrontStandardAboutPanel:(id)sender;

This function is called automatically when About TinyViewer is selected from the TinyViewer menu (obviously, substitute your app name for TinyViewer). OrderFrontStandardAboutPanel: looks in your application's bundle for a set of elements it uses to build the About Panel. Alternatively, you can use the function orderFrontStandardAboutPanelWithOptions: to roll your own About Panel at run time:

- (void)orderFrontStandardAboutPanelWithOptions:
         (NSDictionary *)optionsDictionary;

This comment is from NSApplication.h. It describes the optionsDictionary parameter, but is useful in understanding what elements you can add to your application's bundle (more on this below):

/* Optional keys in optionsDictionary:
@"Credits": NSAttributedString displayed in the info area of the panel. If not specified, contents 
obtained from "Credits.rtf" in [NSBundle mainBundle];
if not available, blank.

@"ApplicationName": NSString displayed in place of the default app name. If not specified, uses the 
value of CFBundleName (localizable). Fallback is [[NSProcessInfo processInfo] processName].

@"ApplicationIcon": NSImage displayed in place of NSApplicationIcon. If not specified, use [NSImage 
imageNamed:@"NSApplicationIcon"]; if not available, generic icon.

@"Copyright": NSString containing the copyright string. If not specified, obtain from the value of 
NSHumanReadableCopyright (localizable) in infoDictionary; if not available, leave blank.

@"Version": NSString containing the build version number of the application ("58.4", "1.2d3"); 
displayed as "Version 58.4" or "1.0 (v58.4) depending on the presence of ApplicationVersion. If not 
specified, obtain from the CFBundleVersion key in infoDictionary; if not specified or empty string, 
leave blank.

@"ApplicationVersion": NSString displayed as the application version  ("1.0", "Mac OS X", "3", 
"WebObjects 3.5", ...), before the build version. If not specified, obtain from 
CFBundleShortVersionString key in infoDictionary..
*/

For starters, go into TextEdit and create your own About Panel text for TinyViewer. Save the file as Credits.rtf. Note that the name is case-sensitive and has to be exactly Credits.rtf. The file Credits.rtf has been used for About Panels since the birth of Mac OS X. Recent releases of the OS also recognize Credits.html and Credits.rtfd. Obviously, Credits.html allows you to include links in your About Panel and Credits.rtfd allows you to use an rtfd package. You can include all three files in your application's bundle, but priority is given to Credits.html, then Credits.rtf, then Credits.rtfd.

Why have more than one? Your best bet is to include Credits.rtf, in case your app is used by someone with an old version of Mac OS X installed. Whether you supplement this with Credits.rtfd or Credits.html is your call.

Adding Credits.rtf to the Project File

Let's add Credits.rtf to our project. Once you get that to work, it's an easy step to add Credits.html or Credits.rtfd to the mix.

In the project window, select the Files tab, then make sure the Resources triangle is open. In the Finder, click on your Credits.rtf file and drag it into the project window, into the Resources area. As you drag, you'll see a bold black line appear that indicates where the file will be dropped (see Figure 6).


Figure 6. Dragging Credits.rtf into the project file.

When you release your mouse button, the dialog in Figure 7 will appear. Be sure to check the "Copy items" checkbox at the top of the dialog. This tells Project Builder to copy the file into your project hierarchy. You can then do as you like with the original, cause Project Builder lets you edit the copy by double-clicking it.


Figure 7. The Add File dialog. Be sure the "Copy items" checkbox is checked.

Fill in the Bundle

My Alert Panel is shown in Figure 8. It is made up of a collection of resources, including an icon, a collection of info.plist strings, and the Credits.rtf file. You already know how to add the Credits.rtf file. To create your application's icons, you'll want to use a program like PhotoShop to create the artwork, then import that artwork into IconComposer. IconComposer is part of the developer tools and is found in /Developer/Applications/.


Figure 8. My TinyViewer About Panel. Hey! That's Kelley!

Building an icon is entire column in itself. The key is to make sure you create an alpha channel for the 128-by-128 version of the icon, save as a tiff, then import into IconComposer. If your icon looks funky when you save and reopen it, you've probably not done the alpha channel magic properly. We'll tackle this in a future column. If you want to try your hand at doing this, be sure to save the icon as a .icns file, then drag it into the project, just as you did with Credits.rtf. I called my icon TinyViewer.icns.

Your next step is to click on the Targets tab in the Project Window, then click on the TinyViewer target. This will bring up a list of settings you'll need to edit in order to fill out the rest of your About Panel, add an icon to the project, etc. Figure 9 shows my settings.


Figure 9. TinyViewer's Info.plist entries.

    Note that the CFBundleSignature is set to GETs. I logged on to http://developer.apple.com and registered this creator code with Apple specifically for TinyViewer. Feel free to use this code yourself, since I'll only use it for Getting Started projects. We'll dig into registering creator codes in a future column.

Once those changes are made, go back to the Files tab and open the Resources triangle. Click on the InfoPlist.strings item. You'll see something that looks like this:

/* Localized versions of Info.plist keys */
CFBundleName = "TinyViewer";
CFBundleShortVersionString = "TinyViewer version 1.0";
CFBundleGetInfoString = "TinyViewer 1.0, Copyright 2003 by Dave Mark. All Rights Reserved.";
NSHumanReadableCopyright = "Copyright 2003 by Dave Mark. All Rights Reserved.";

I've edited my versions to reflect the settings I want for TinyViewer. Note that the InfoPlist.strings file is your localized strings file. You might contract out to a localizing firm to take your English plist file and translate it to a German plist file. You'd keep both localized files in your project and the right strings would be displayed, depending on the language setting on your computer.

Think of the Info.plist file as the true repository for your plist keys. Any strings that need to be localized should be kept in each localized InfoPlist.strings file. The localized version, if it exists, will override any matching key in the Info.plist file. When building your project, just do the Targets version first, then do the Files version and you should be fine.

    A couple of books that really helped with this month's column: Joe Zobkiw's excellent Mac OS X, Advanced Development Techniques really digs deep into a wide variety of Cocoa-related topics. And, as I've mentioned in a previous column, Bill Cheeseman's Cocoa Recipes for Mac OS X: The Vermont Recipes is an invaluable resource. I definitely owe both these guys a beverage of their choice. And an extra tall one for Mike Trent for helping to unravel all those pesky Cocoa mysteries. Thanks!

Till Next Month...

Man.

There was way more that I wanted to get into. I just ran out of space. I am just loving Project Builder, Interface Builder and Cocoa. Not sure what we'll dig into next month. There's a little more iPod madness left in me. I also just got this really good AppleScript book. Hrm. Maybe a column on scripting my iPod? See you soon...


Dave Mark is a long-time Mac developer and MacTech contributor. Random facts about Dave? He is addicted to Age of Empires. He can't seem to stop camping. And his iPod has him hypnotized with its shiny silver grooviness.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Whitethorn Games combines two completely...
If you have ever gone fishing then you know that it is a lesson in patience, sitting around waiting for a bite that may never come. Well, that's because you have been doing it wrong, since as Whitehorn Games now demonstrates in new release Skate... | Read more »
Call of Duty Warzone is a Waiting Simula...
It's always fun when a splashy multiplayer game comes to mobile because they are few and far between, so I was excited to see the notification about Call of Duty: Warzone Mobile (finally) launching last week and wanted to try it out. As someone who... | Read more »
Albion Online introduces some massive ne...
Sandbox Interactive has announced an upcoming update to its flagship MMORPG Albion Online, containing massive updates to its existing guild Vs guild systems. Someone clearly rewatched the Helms Deep battle in Lord of the Rings and spent the next... | Read more »
Chucklefish announces launch date of the...
Chucklefish, the indie London-based team we probably all know from developing Terraria or their stint publishing Stardew Valley, has revealed the mobile release date for roguelike deck-builder Wildfrost. Developed by Gaziter and Deadpan Games, the... | Read more »
Netmarble opens pre-registration for act...
It has been close to three years since Netmarble announced they would be adapting the smash series Solo Leveling into a video game, and at last, they have announced the opening of pre-orders for Solo Leveling: Arise. [Read more] | Read more »
PUBG Mobile celebrates sixth anniversary...
For the past six years, PUBG Mobile has been one of the most popular shooters you can play in the palm of your hand, and Krafton is celebrating this milestone and many years of ups by teaming up with hit music man JVKE to create a special song for... | Read more »
ASTRA: Knights of Veda refuse to pump th...
In perhaps the most recent example of being incredibly eager, ASTRA: Knights of Veda has dropped its second collaboration with South Korean boyband Seventeen, named so as it consists of exactly thirteen members and a video collaboration with Lee... | Read more »
Collect all your cats and caterpillars a...
If you are growing tired of trying to build a town with your phone by using it as a tiny, ineffectual shover then fear no longer, as Independent Arts Software has announced the upcoming release of Construction Simulator 4, from the critically... | Read more »
Backbone complete its lineup of 2nd Gene...
With all the ports of big AAA games that have been coming to mobile, it is becoming more convenient than ever to own a good controller, and to help with this Backbone has announced the completion of their 2nd generation product lineup with their... | Read more »
Zenless Zone Zero opens entries for its...
miHoYo, aka HoYoverse, has become such a big name in mobile gaming that it's hard to believe that arguably their flagship title, Genshin Impact, is only three and a half years old. Now, they continue the road to the next title in their world, with... | Read more »

Price Scanner via MacPrices.net

B&H has Apple’s 13-inch M2 MacBook Airs o...
B&H Photo has 13″ MacBook Airs with M2 CPUs and 256GB of storage in stock and on sale for up to $150 off Apple’s new MSRP, starting at only $849. Free 1-2 day delivery is available to most US... Read more
M2 Mac minis on sale for $100-$200 off MSRP,...
B&H Photo has Apple’s M2-powered Mac minis back in stock and on sale today for $100-$200 off MSRP. Free 1-2 day shipping is available for most US addresses: – Mac mini M2/256GB SSD: $499, save $... Read more
Mac Studios with M2 Max and M2 Ultra CPUs on...
B&H Photo has standard-configuration Mac Studios with Apple’s M2 Max & Ultra CPUs in stock today and on Easter sale for $200 off MSRP. Their prices are the lowest available for these models... Read more
Deal Alert! B&H Photo has Apple’s 14-inch...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on sale for $200-$300 off MSRP, starting at only $1399. B&H offers free 1-2 day delivery to most US addresses: – 14″ 8... Read more
Department Of Justice Sets Sights On Apple In...
NEWS – The ball has finally dropped on the big Apple. The ball (metaphorically speaking) — an antitrust lawsuit filed in the U.S. on March 21 by the Department of Justice (DOJ) — came down following... Read more
New 13-inch M3 MacBook Air on sale for $999,...
Amazon has Apple’s new 13″ M3 MacBook Air on sale for $100 off MSRP for the first time, now just $999 shipped. Shipping is free: – 13″ MacBook Air (8GB RAM/256GB SSD/Space Gray): $999 $100 off MSRP... Read more
Amazon has Apple’s 9th-generation WiFi iPads...
Amazon has Apple’s 9th generation 10.2″ WiFi iPads on sale for $80-$100 off MSRP, starting only $249. Their prices are the lowest available for new iPads anywhere: – 10″ 64GB WiFi iPad (Space Gray or... Read more
Discounted 14-inch M3 MacBook Pros with 16GB...
Apple retailer Expercom has 14″ MacBook Pros with M3 CPUs and 16GB of standard memory discounted by up to $120 off Apple’s MSRP: – 14″ M3 MacBook Pro (16GB RAM/256GB SSD): $1691.06 $108 off MSRP – 14... Read more
Clearance 15-inch M2 MacBook Airs on sale for...
B&H Photo has Apple’s 15″ MacBook Airs with M2 CPUs (8GB RAM/256GB SSD) in stock today and on clearance sale for $999 in all four colors. Free 1-2 delivery is available to most US addresses.... Read more
Clearance 13-inch M1 MacBook Airs drop to onl...
B&H has Apple’s base 13″ M1 MacBook Air (Space Gray, Silver, & Gold) in stock and on clearance sale today for $300 off MSRP, only $699. Free 1-2 day shipping is available to most addresses in... Read more

Jobs Board

Medical Assistant - Surgical Oncology- *Apple...
Medical Assistant - Surgical Oncology- Apple Hill Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Business Analyst | *Apple* Pay - Banco Popu...
Business Analyst | Apple PayApply now " Apply now + Apply Now + Start applying with LinkedIn Start + Please wait Date:Mar 19, 2024 Location: San Juan-Cupey, PR Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.