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

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, 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
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
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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.