TweetFollow Us on Twitter

Rez Is Your Friend

Volume Number: 14 (1998)
Issue Number: 9
Column Tag: Tools Of The Trade

Rez is Your Friend

by Marshall Clow
Edited by the MacTech Editorial Staff

What is Rez and why should I care?

What is Rez?

Every Macintosh program is made up of resources. Dialogs, Alerts, String Lists, the list goes on and on. These resources have to be created by the someone, and are part of the program. Every programmer is familiar with ResEdit or Resorcerer, which let you edit resources visually. Fewer programmers know about Rez, the resource compiler.

Rez is a compiler, like a C, Pascal or C++ compiler. It takes text files (usually with the suffix '.r' ) as input, and produces a resource file as output. MPW also ships with DeRez, which takes a resource file and produces a text file that can be input back into Rez. Originally, Rez ran only under MPW, but both Symantec and Metrowerks have shipped versions of Rez as part of their integrated development environments.

In this article, I'm not going to give a tutorial on Rez. Instead, I'm going to concentrate on the reasons that you should be using Rez to manage your resources. A full manual for Rez (and DeRez) can be found in Appendix C of "Building and Managing Programs in MPW, available at ftp://ftp.apple.com/devworld/Tool_Chest/Core_Mac_OS_Tools/MPW_etc./Documentation/MPW_Reference/Bldg%26Mng_Progs_In_MPW.sit.hqx.

A Little History

When first Macintosh development environments shipped (back in 1985), they included a tool called RMaker. (ResEdit didn't appear until late 1985 or early 1986) RMaker (short for Resource Maker), like Rez, was a resource compiler, translating a text file into a resource file. However, RMaker knew about only a few resource types, and was not easily extensible. So, when Apple shipped MPW 1.0 in 1986, they provided a completely new set of resource tools. Rez, DeRez, and RezDet. Joel West wrote an article for this magazine about the new tools in MPW 1.0. See www.mactech.com/articles/mactech/Vol.03/03.02/Rez-ervations/index.html for a description of those tools as they appeared in MPW 1.0.

An improved version of Rez shipped with MPW 2.0 in 1988, and Rez 3.0 included support for QuickDraw pictures.

Symantec shipped stand-alone versions of Rez and DeRez (cunningly named SA-Rez and SA-DeRez) with Think C 6, and incorporated Rez into the IDE with the THINK Project Manager 7.0 (68K) and the Symantec Project Manager (PPC).

Metrowerks incorporated Rez into their IDE starting with CodeWarrior 6.

Why Rez?

At this point you are probably asking yourself, "Why should I care about Rez? I do all my resource editing in ResEdit/Resorcerer, why would anyone use Rez?". There are several reasons:

Comments

Since Rez source files are text files, they can contain comments about why a resource contains some information. A change history is another good thing to put here. Unless you have a great memory, you will want to put comments in your resource files so that when you look back a year (or two later), you can tell why this number in a table was changed from a 23 to a 42. Since resource files are binary data, there is no place to put comments there.

Don't underestimate the importance of comments. Would you write code without comments? They have saved me hours (or days!) of debugging on many occasions.

Macros

This is a big feature. Since Rez supports C-style macro processing, you can give symbolic names to your resource types and IDs, and use these definitions in a header file for both your resources and your C/C++ code. Whenever I see a bit of code that says something like:

   h = Get1Resource ( 'Foo!', 234 );

I cringe. I just know that somewhere down the line, someone will need to change the ID of that resource, and they may or may not find all the places that load/use that resource. I use a header file that is shared between my resource files and my source files.

// File Foo.h
#define   kNormalFooResID

// File Foo.c
   h = Get1Resource ( 'Foo!', kNormalFooResID );

// File Foo.r
resource 'Foo!' ( kNormalFooResID, purgeable ) {

If you prefer a less made-up example, here's one that uses familiar resource types:

// File Foo.h
#define   kNoAppleEventsAlert      130

// File Foo.c
   if (( Gestalt( gestaltAppleEventsAttr, &attrs )) != noErr )
      Alert ( kNoAppleEventsAlert, nil );
   else { // normal processing

// File Foo.r
resource 'ALRT' ( kNoAppleEventsAlert ) {
   { 0, 0, 200, 400 },    // bounds for the alert
   kNoAppleEventsAlert,   // the DITL associated with this alert
   {                      // Alert Stages
      OK, visible, silent,
      OK, visible, silent,
      OK, visible, silent,
      OK, visible, silent
   },
   centerMainScreen       // Where to show the alert
   };

resource 'DITL' ( kNoAppleEventsAlert ) {
// Dialog items go here
   };

To renumber the alert, all I have to do is change one line in Foo.h and rebuild. The resources will be renumbered automatically, even the reference to the DITL inside the ALRT resource.

Not convinced yet? Here's another example. Suppose that you want all the buttons in your application to have the same "look". For the sake of argument, let's say that you want them all to have the same height. Using ResEdit or Resorcerer, this is a nightmare, because you would have to go through each and every dialog and find each button, and check (and possibly change) the height of each button, to conform to your standard. Heaven help you if some UI designer or product manager decides that all the buttons need to be two pixels higher (to aid in translation to Urdu, no doubt).

Using Rez, (and a little foresight), it's a one line change!

// File Foo.h
#define   kButtonHeight      20

// File Foo.r
resource 'ALRT' ( kNoAppleEventsAlert ) {
// We've seen this before.....
   };

resource 'DITL' ( kNoAppleEventsAlert ) {{
   {260, 129, 260 + kButtonHeight, 209},
                  Button { enabled, "OK" };
   {260,  40, 260 + kButtonHeight, 120},
                  Button { enabled, "Cancel" };
   {8, 72, 23, 264},            StaticText { disabled, "Hi Mom!" };
   };
   };

When I change the kButtonHeight constant in the header file, all the buttons in the application change to match the new regime.

Of course, you can extend this for button widths, height and widths of other items.

Extensibility

Rez is extensible, i.e, you can write resource definitions for you own resource types. In fact, Rez comes with a set of header files that define the structure of all the common Mac resources types; Rez does not have any "built-in" resource types. It does have a set of built-in types that you can use as fields in your resources, such as bytes, 16 and 32 bit integers, C and Pascal-style strings, and a few others. Here's an example of a C struct definition and a corresponding Rez definition:

// In a header file
struct FooListEntry {
   short   fooIndex;
   short   fooValue;
   };

struct FooResource {
   short   fooVersion;
   short   fooMinorVersion;
   long    fooCount;
   FooListEntry   fooList [ 1 ];
//   Really a list of 'fooCount'
   };

typedef   struct FooResource FooResource, *FooResourcePtr,             **FooResourceHandle;

// --------------- Rez ---------------
type 'Foo!' {
   integer;   // fooVersion
   integer;   // minor Version
   long = $$CountOf ( fooList );   // size filled in by Rez
   array fooList {
      integer;      // index
      integer;      // value
      };
   };

// Usage
resource 'Foo!" ( kDefaultFooList ) {
   kCurrentFooVersion, kCurrentFooMinorVersion,
   {
      12, 23;      // See bug #32
      34, 45;
      45, 56;      // 54 was wrong! changed to 56 mtc 07/02/98
      123, 987;
   };
};

In a perfect world, Rez would read C/C++ structure definitions directly, and we wouldn't need to have different definitions for the same data structure. However, a Rez definition specifies exactly how to lay out the bits in the data structure, while C/C++ compilers are allowed considerable leeway in structure alignments and padding. Look at Apple's header files. All those lines that start out '#pragma options align' are there to control structure alignment so that 68K and PowerPC programs can share data structures and data files.

Source and configuration control

There are several source control systems available today. Projector, SourceSafe, SCCS, are CVS are just a few. Most of them handle binary files, but they all handle text files much better. Diffing tools are designed to work on text files. Having your resources in text files makes it much easier to track changes, additions, and deletions of resources.

Batch Processing

Since Rez files are text files, it is easy to generate them from other sources. A long time ago, I worked on a project to print to plotters. We had a database (in Hypercard!) that contained information on a variety of plotters (margins, paper sizes, commands supported, etc.). We had many people gather this information and enter it into the stack. This stack generated a ".r" file that we then compiled.

Today, a great deal of information is contained in databases. Sometimes you have to include information from a database into your application. Most databases have extensive report-writing facilities. It is usually fairly easy to make a database divulge the data that you are interested in formatted in Rez format. This also makes it easy to "refresh" the data in your application, by including up-to-date information as part of the build process. The database could hold anything, from sample data for the user to manipulate to the current structure of your web site.

Translation

One of the main reasons for the existence of resources is to decouple the user interface from the code, thereby making changes (especially translation between languages) easier. When I wrote printer drivers at HP we put all of our literal strings into a single header file, along with comments about how the string was used. When the time came for translation, we sent this single text file to the people doing the translations. They sent us back a translated file, and we built a translated driver. After that, all that was required of the translators was to adjust the layout of the dialogs, to take into account the changed sizes of some of the dialog items. Using this scheme, we were able to ship 12 different language versions of our driver software within 3 weeks of releasing our English version.

When Not to Use Rez

There are some resource types for which Rez is not appropriate. For example, there is no real structure for ICON (or icl8, etc.) resources. They are really just small bitmaps. So a Rez definition of a ICON resource is just a string of hex digits. Icon files are better kept as binary files, and edited with a resource editor. However, you can still have your cake and eat it too; by using Rez to assemble the final resource file. Suppose that you have an icon in a file called "Foo.rsrc", and in that file it is 'ICN#'(128). You can include it using Rez thusly:

include "Foo.rsrc" 'ICN#' ( 128 ) as 'ICN#' ( kFooIcon );

This will include the icon into your output file, while giving it the resource ID that you want (which is defined by a symbolic constant).

Conclusion

Keeping your resources as text files and using a resource compiler has many advantages over using a resource file editor. You can add comments to your resources, use symbolic constants, define your own resource types, and use all the tools that exist for maintaining and manipulating text files. The next time you find yourself reaching for ResEdit or Resorcerer, consider a third option. Sometimes the old tricks work best, and often Rez is the right tool.

Bibliography and References

West, Joel. "No Rez-ervations Needed!". MacTech Magazine (formerly MacTutor) 3:2 (February 1986).


Marshall Clow is a programmer. He has worked for Palomar Software, Hewlett-Packard, and Aladdin Systems. Among other things, he has written PICT Detective, Aladdin's Resource Compression Toolkit, and way too many resource processing tools. He currently works for Adobe Systems, where his title is "Bad Influence". When he's not coding, he can be found mountain biking or checking out micro breweries. He can be reached at mclow@mailhost2.csusm.edu

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

VOX 3.5.5 - Music player that supports m...
VOX just sounds better! The beauty is in its simplicity, yet behind the minimal exterior lies a powerful music player with a ton of features and support for all audio formats you should ever need.... Read more
WiFiSpoof 3.9 - Change your WiFi MAC add...
WiFiSpoof quickly and easily allows you to change your WiFi MAC address via hot-key or the system menu bar. Startup optimizations Fixed an issue with getting the correct Address for some devices... Read more
FileMaker Pro 19.6.3.302 - Quickly build...
FileMaker Pro is the tool you use to create a custom app. You also use FileMaker Pro to access your app on a computer. Start by importing data from a spreadsheet or using a built-in Starter app to... Read more
RapidWeaver 9.0.0 - Create template-base...
RapidWeaver is a next-generation Web design application to help you easily create professional-looking Web sites in minutes. No knowledge of complex code is required, RapidWeaver will take care of... Read more
Burn 3.1.6 - Easily burn data, audio, vi...
Burn... There are a lot of ways to approach burning discs. Burn keeps it simple, but still offers a lot of advanced options. Create data discs with advanced data settings like, file permissions, the... Read more
XMind Pro 22.11.3656 - Popular mind mapp...
XMind Pro claims to be the most popular mind-mapping tool on the planet. You can use it to work through an individual brainstorming session and it will record and collect inspirations as you go. It... Read more
A Better Finder Rename 11.58 - File, pho...
A Better Finder Rename is the most complete renaming solution available on the market today. That's why, since 1996, tens of thousands of hobbyists, professionals and businesses depend on A Better... Read more
Duet 3.2.0.0 - Use your iPad as an exter...
Duet is the first app that allows you to use your iDevice as an extra display for your Mac using the Lightning or 30-pin cable. Note: This app requires a iOS companion app. Release notes were... Read more
iStat Menus 6.70 - Monitor your system r...
iStat Menus lets you monitor your system right from the menubar. Included are 8 menu extras that let you monitor every aspect of your system. Features: CPU - Monitor cpu usage. 7 display modes,... Read more
Opera 95.0.4635.25 - High-performance We...
Opera is a fast and secure browser trusted by millions of users. With the intuitive interface, Speed Dial and visual bookmarks for organizing favorite sites, news feature with fresh, relevant content... Read more

Latest Forum Discussions

See All

Here’s Your Stinkin’ Proof of Life – The...
Yes, we are alive. After taking most of December off from recording we did our big end of year show with the intention of coming back in full force in the new year, but instead we just sort of… took another month off. I really apologize to those who... | Read more »
TouchArcade Game of the Week: ‘1 Bit Sur...
Well, if you’ve been following TouchArcade for a while and you generally have an idea of what our tastes are, then this week’s pick shouldn’t really be much of a surprise. Cool retro-style pixel graphics? Dungeon crawling mechanics? Roguelike... | Read more »
SwitchArcade Round-Up: Reviews Featuring...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for February 3rd, 2023. We start things off with a review of Fashion Police Squad from our pal Mikhail. Gosh, he’s quite the keener. After that, we look at the dozen or so new releases... | Read more »
The 10 Best Arcade Archives Beat-Em-Ups...
A little while ago, we put up a list of our favorite shoot-em-ups in Hamster’s Arcade Archives line-up. There are hundreds of games available thanks the weekly releases, so it can be a little hard to find the cream of the crop if you’re coming in a... | Read more »
Cyan announces iOS port for Myst to cele...
One of the best things about old games being remastered and ported is it gives people the chance to try those old games that were slightly too difficult when they were younger, and thanks to indie studio Cyan, mobile users can now experience the... | Read more »
‘Castle Crumble’ From ‘Spire Blast’ Deve...
Spire Blast developers Orbital Knight have released Castle Crumble () on Apple Arcade today. If you played Spire Blast you will right at home here, and I think I like it more based on the first few levels I played. It features cannonballs, magic... | Read more »
SwitchArcade Round-Up: ‘Life is Strange...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for February 2nd, 2023. We’ve actually got a little news to look at today, and that’s where we’ll start things. There are also a ton of new games, some of them very good (Life is Strange... | Read more »
‘Dead Space’ Remake Steam Deck Review –...
Need for Speed Unbound was one of the first new current generation only games to impress me on Steam Deck. I’ve played many games that have been ported to PC from other platforms and also games built for PC from the start, but it is always great... | Read more »
The Stunning Remake of ‘Myst’ Is Coming...
Indie developer Cyan who developed the 2021 reimagining of the classic Myst have just announced that it is coming to iOS as Myst Mobile. Myst Mobile will bring the complete experience to iPhone and iPad devices in the future. Considering the... | Read more »
Psychedelic Horror Experience ‘Happy Gam...
Amanita Design’s psychedelic horror experience Happy Game is out now on iOS and Android worldwide following its debut on Nintendo Switch and PC platforms a little while ago. Happy Game is a standard paid game on iOS and Android, and not included... | Read more »

Price Scanner via MacPrices.net

Get an Apple Pencil 2 today for 30% off MSRP,...
Amazon has the Apple Pencil 2 on sale for $40 (30%) off MSRP for a limited time. Shipping is free: – Apple Pencil 2: $89.99 $40 off MSRP (30%) Their discount reduces the price of this Pencil to $10... Read more
Apple Watch SE GPS models on sale for $30 off...
Amazon has Apple Watch SE GPS models on sale for $30 off MSRP for a limited time, each including free shipping. Their prices are the lowest currently available for SE Watches: – 40mm Apple Watch SE... Read more
Sunday Sale: Apple AirPods Pro for $199, save...
Amazon has new 2022 Apple AirPods Pro on sale for $199.99 including free shipping. Their price is $50 off MSRP, and it’s currently the lowest price available for new AirPods Pro from any of the... Read more
Apple exploring the possibility of adding a h...
Mark Gurman, in this weeks’s Power On newsletter discusses a comment made by Apple Inc. CEO Tim Cook during Apple’s most recent earnings call that could be a clue to the company’s future. When asked... Read more
Deal Alert! 13″ M2 Midnight MacBook Air with...
Tiger Direct is continuing their Apple fire sale with this new 13″ M2 MacBook Air with 16GB of RAM and a 512GB SSD, in Midnight, for only $858.99 including $7 shipping. Their price is an amazing $841... Read more
Deal Alert! 13″ M2 Midnight MacBook Air with...
Tiger Direct is continuing their Apple fire sale with this new 13″ M2 MacBook Air with 16GB of RAM and a 256GB SSD, in Midnight, for only $707.99 including $7 shipping. Their price is a whopping $692... Read more
Deal Alert! 13″ M2 MacBook Pros with 16GB of...
Tiger Direct continues their Apple Fire Sale (see our home page for these latest amazing deals) by offering a $650 discount on new 13″ M2 MacBook Pros with 16GB of RAM and 256GB SSDs: – 13″ M2... Read more
Deal Alert! 13″ M2 MacBook Air with 16GB RAM...
Tiger Direct is continuing their Apple fire sale with this new 13″ M2 MacBook Air with 16GB of RAM and a 256GB SSD, in Space Gray, for only $757.99 including $7 shipping. Their price is a whopping $... Read more
Deal Alert! Apple iPad fire sale continues, n...
Apple retailer Tiger Direct continues to offer fire sale prices on new 2022 10th-generation iPads with prices as low as 50% off MSRP. This is am amazing deal, and we don’t know how long it will last... Read more
Deal Alert! Select Apple M1 10.9″ iPad Airs o...
Tiger Direct is offering a 48% discount on select Apple M1-powered 10.9″ iPad Airs. This is an amazing sale, and we don’t know how long it will last. Their prices are the lowest available among the... Read more

Jobs Board

Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Optometrist- *Apple* Valley, CA- Target Opt...
Optometrist- Apple Valley, CA- Target Optical Date: Feb 3, 2023 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 796045 At Target Read more
Sales Floor Associate - *Apple* Blossom Mal...
Sales Floor Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Sales Floor Assistant - *Apple* Blossom Mal...
Sales Floor Assistant - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
*Apple* Tech Support Analyst - Sesame Worksh...
Sesame Workshop is looking for an Apple Tech Support Analyst. We're looking for someone with an enthusiastic personality, articulate communication skills and a Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.