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

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 »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »

Price Scanner via MacPrices.net

You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.