In the previous entry, I mentioned that I had no experience with the Macintosh Toolbox, the application development “library” of sorts for old (pre-OS X) Macs. Recently, I had a bit of free time, and sat down to work it out in Mini vMac, an excellent early Mac emulator.

I found out there were a lot of things that aren’t quite obvious as they would be in today’s more modern IDEs. A lot of the example code I found crashed, but I eventually persevered by just reading the fine manual.

This article isn’t so much an in-depth explainer of classic Macintosh development as it is a quick how-to on how I got my emulated Macintosh to do a graphical Hello World.

Get the Mac running

There are plenty of guides out there on how to set up an emulated Macintosh, and setting up a copy of THINK C is not much harder.

The Project File

When you first open THINK C’s “Think Project Manager,” it asks you for a project file and gives the option to create a New one. It helps to have a folder already set aside to store all your project files in, because “New” only creates the project file and not a nice folder in which to keep your junk.

Creating a new project in THINK C

I set up a new THINK C project, called Hello Toolbox. Remember that name - it’s important for later. Unlike modern IDEs, it didn’t really ask me any questions or prepare the project for use as an application.

For a classic Mac Toolbox app, you want to include the MacTraps library, which came with THINK C for me. Just navigate to THINK C -> Mac Libraries -> MacTraps and add it to your project from the Source -> Add menu.

Next, do File -> New and create your new source file. Note that it won’t be automatically added to your project: you’ll want to do Source -> Add again for it.

MacTraps and source file added

The Source File

Finally, we get to write some code. Not too much, though:

int main(void) {
  WindowPtr TheWindow;
  InitGraf(&thePort);
  InitFonts();
  InitWindows();

  TheWindow = GetNewWindow(128, 0L, (WindowPtr)-1L);
  SetPort(TheWindow);
  MoveTo(30, 50);
  DrawString("\pHello, World");
  while(!Button()) { }

  return 0;
}

Code ready to go

Resources

Everything in the classic Mac world is based on resources. A good early way to think of them is as sort of the “non-code” or asset components of your app. Anyone who used a classic Mac a lot when they were contemporary probably has fond memories of using ResEdit to butcher other people’s software and games to insert your own art, sounds, rude dialogue boxes, etc.

The 128 mentioned above in the call to GetNewWindow is a resource ID: rather than define the window entirely in code, we can use ResEdit’s window editor to build it, and then tell the code where to look for it.

Obviously, we now have to create a window resource with ID=128. Quit THINK Project Manager and open your copy of ResEdit (often distributed with THINK C in the “Utilities” folder).

ResEdit is ready to go

That’s the stuff.

Navigate to the same folder you put your project file in, and click “New” to create a new resource file. Call it Hello Toolbox.rsrc. The name has to match that of the project, otherwise THINK C won’t pick it up: this is the critical part that I missed in previous attempts.

Pick Resource -> Create New Resource and tell it we want a WIND (Window) resource.

Select Create New Resource Choose a WIND resource

All the defaults the window editor has picked for us are perfect, but if you want a fancy window you can change the style of it from the gallery up above. I chose to make mine look like a Desk Accessory (DA) because that’s somehow even more retro.

ResEdit window editor

By default, the first resource that you add will be ID = 128, so we don’t have to do anything else here.

Quit ResEdit, but save all your changes. Now let’s head back into our THINK C project.

Putting it all together

Pick “Build Application” from the Project menu inside THINK Project Manager. Pick a place to put it.

If, during the linking process, you do not see the message “Copying Hello Toolbox.rsrc,” something has gone wrong with the creation of your resource file. Make sure it is in the same folder and named the same as the project file, otherwise THINK C will not pick it up and your app will crash when it tries to dereference the missing WIND resource.

Run your app

Look at that, you just made Hello World in a GUI.

Hello World, on an old Mac