Roll Your Own Mac App
Matt Cone March 12, 2007 Tutorials Mac Developer
Have you ever wondered how Mac applications are made? Then you’ve come to the right place! This article was written for anyone wanting to get into Mac programming or interface design. We’ll be discussing Cocoa, Apple’s native object-oriented application programming environment for Mac OS X, which is (for the true nerds out there) one of the five major APIs available for Mac OS X. We’ll be using Xcode for all of this.
What You Need - What You’ll Get
To make your very first Mac application, you’ll need the following:
- Mac OS 10.3 or later (Preferably Mac OS 10.4, since that’s what we used)
- Xcode (Available from the Apple Developer Connection website)
- A working knowledge of the Mac OS X interface
- iTunes, and a “chill” album to listen to (Optional)
When you make it to the end of this article, you should be able to:
- Create a basic application using Xcode
- Get around the Xcode and Interface Builder applications
- Use basic Cocoa objects
Before You Start…
This article was written on a Mac Pro running Mac OS X 10.4.8, and using Xcode 2.4.1. Our instructions should work on all Macs, but if you run into problems on your machine, please contact us. (Look for the email address at the end of the article.) Here’s a micro-glossary of terms you’ll need to understand:
- App: An application, with a .app file extension.
- Build: Compiles all the files in your project into a single .app file.
- Cocoa: One of the most popular Mac programming methods. It’s mainly object oriented, rather than code-based.
Make Your Mac App
Fire up Xcode. If you don’t have it installed, it may be available on your Mac OS X install disc. If all else fails, just download it from the ADC. (That’s Apple Developer Connection, for all you new programmers.) You may have to sign up for an account, but that’s not a bad thing. If you’re going to be an indie Mac Developer, you may as well go all out. If Xcode is installed, it will probably be hiding under
Yourharddrive/Developer/Applications. Open Xcode and you’ll be presented with… Yep, nothing. So, hop up to the File menu and select New Project.
Choose “Cocoa Application” and click next.
Type in a project name. Something text-y might be nice since we’re going to create a basic Text Editor. We’ll call ours “iWrite” (apologizes to Apple’s inventive set of names), but you can call yours anything. We’d also advise creating a folder on your hard drive for Xcode. Ours is called “Development," and is located in our Documents folder, as you can see.
Xcode will create your project!
Double click on “MainMenu.nib.” It should automatically open a little app called Interface Builder, an object based interface builder. Hide Xcode. Be sure to select Show Inspector from the Tools menu, before you continue.
Grab the “NSTextView” object from the Cocoa Objects Panel and drag it to an appropriate size in the blank window of your app using the blue handles. If you’re interested, or don’t quite understand these steps, check out our crash-course in the basic Cocoa-Objects below.
That’s about everything. Yes, you read correctly, but don’t sell it on the Internet for $8 a license just yet… Let’s add some niceties , starting with the contextual menus up on the top. You can double-click the titles and change them to whatever you like. You’ll find weird stuff, such as: “Quit NewApplication,” where it should say “Quit iWrite.” We’d also suggest deleting the Help menu and the File menu, because they won’t be active in your final app. (We haven’t messed with them yet.) Simply highlight them and hit Delete.
Let’s add one last feature, just for kicks. Double click inside your NSTextView and type something inventive, like we’ve done here…
Okay, maybe “text goes here, idiot” is a bit inappropriate, but now the application doesn’t need a help file, as this brilliant message will appear every time our application is opened. Simple, huh?
Sadly, there is one tricky (and annoying) thing about this whole interface business, and that’s scaling. Have you ever resized a window? If you haven’t, stop reading this and go take some evening classes - you need help. If you have, you know that there’s a little tab at the bottom of most windows that lets you resize them. To do this in Interface builder, choose your NSTextView, go to the inspector and hit “Size” in the drop-down menu. Click inside the little box inside the big box to make it scale the NSTextView when the window is resized. And yes, this does apply to all Cocoa Objects. Hopefully this image should clear things up…
Save your fancy new interface, and don’t worry about “Save As.” Interface Builder will save it in your project file - and in the right place - automatically. Let’s go back to Xcode: unhide the window. You’ll notice that the file “whateveryoucalledyourapp.app” is in red. Click “Build” up on the top, and viola! As long as you followed our instructions, it should build without a hitch.
Click “Build and Go” to launch your application! It should appear in the dock as a normal application, with the menu at the top, and the standard .app logo. Xcode will show a Run Log, giving you the details about the build. Now, if you fish around in your Development folder, and find the .app file, you can copy it into your Applications folder! Mine was camping out under Max\Documents\Development\iWrite\build\Debug\iwrite.app.
Some Words of Warning
Watch that you don’t just drag and drop the application into your Applications folder, as the Xcode project file will lose it, and you won’t be able to edit it again. Xcode is a huge and diverse program, and if you don’t know what you’re doing, you can crash the build of your application. Unless you really know your stuff, keep it simple, and use the minimum number of objects necessary to avoid clashes. After all, that’s why companies like Bare Bones Software make lots of money: They make good applications that don’t crash. That’s it! Until next time, keep experimenting with Cocoa objects, and let me know what you thought of this article.
Subscribe to our email newsletter
Sign up and get Macinstruct's tutorials delivered to your inbox. No spam, promise!