Roll Your Own 1: The Ultimate Customization

Originally published in May 2002.

The Ultimate Customization

Hi there! Welcome to the first of what will be a series of articles on the ultimate customization of your Macintosh. I’m not talking about some slew of utilities that will allow you to change the look and feel of the Mac OS, nor am I speaking of which paints to use when giving that plain beige G3 a camouflage look; I’m talking about programming. If you’re like most people, the software you use on your computer was written by someone else. This column will take you through the process of “rolling your own” software instead of buying it off the shelf or from a catalog. There is no more powerful way to customize your computer’s capabilities than to write your own software.

First, let me tell you a bit about myself. I’ve been working with computers now for about 20 years, having cut my teeth on an Atari 800 with a cassette tape drive for storage, an old color television for a monitor, and a 300 baud modem that got me onto CompuServe. Eventually I took that computer apart to figure it out more. Unfortunately I never got it back together again; it’s actually sitting in my garage. I keep meaning to get back to it, poor thing. I sat at that computer for hours with the BASIC cartridge inserted, entering programs from Compute! magazine.

Eventually I moved to an Apple ][ and from there to a Macintosh SE. I purchased the SE used while I was a student at UCLA, and quickly purchased Think C (which came with the wonderful student discount) and used it to write the software for my classes in computer programming. That computer came apart too so that I could install a 40 MB hard drive, but it also came back together again.

After I finished at UCLA, I didn’t program much, but instead worked as a Macintosh technician both independently and for a number of companies. Over the years, my main computer switched from the SE (which is also in the garage next to the Atari) to an LC III, a PowerBook 180c, then to a Power Mac 7500, a PowerBook 1400ce, and now a PowerBook G3, each of which I still have (except the LC III, which I traded in for my brother-in-law’s Mac 512K, just because it was the second model Mac ever released meaning it’s so old it’s become cool again).

Eventually I found FileMaker Pro, which I’m sure many of you are familiar with. I found that it was simple to create powerful programs with FileMaker, and I decided that I liked building software more than I liked troubleshooting hardware. So, for the last four years or so, I’ve been making my living as a FileMaker developer, with a sprinkling of AppleScript and C thrown in for good measure.

What’s that? You don’t think FileMaker is programming? Well, let’s investigate that; just what does programming a computer mean?

Programming a computer is simply giving the computer instructions that it can follow in order to get it to do what you want. Using this simplistic definition, double-clicking on an icon on your desktop is programming, and so is selecting a menu item. After all, you’re giving the computer an instruction that you expect to be followed. But most people don’t think of double-clicking as programming, and that isn’t what we’ll be covering in this column, so let’s refine our definition a bit.

Programming involves giving the computer a series of instructions that are followed to produce the desired result that the programmer has in mind. This is the basic definition I’m going to use throughout this column. Given this, building a script with FileMaker’s ScriptMaker is programming, and so is recording a macro with QuicKeys or writing a script in AppleScript, or building a Web browser with C++. There are only two differences in these examples.

The first is the language used to give the computer instructions (AppleScript, C++, etc.). Computers don’t actually understand English or any other human language. All they understand are ones and zeros. Every e-mail you send to your mom, every picture you take with your digital camera, every song you listen to in iTunes is stored as a series of 1s and 0s. Software translates these binary digits into text, pictures, and music that we can interact with.

But people don’t tend to think in terms of 1s and 0s. We think in terms of nouns and verbs. We want the computer to perform actions on things. The “things” programs usually manipulate are variables and the actions are commands. Each computer language has its own syntax, its own way of describing to the computer what we want to accomplish.

The second difference between building something with QuicKeys and building it using AppleScript or C++ is what the language can do. Each language has tradeoffs. The easier the language is to use, generally the less it can do. For instance, AppleScript is one of the easiest languages to learn on the Mac, but there are some things that simply can’t be done with AppleScript, such as writing an operating system. For this kind of software you would need to use a much lower-level language such as C++ that has greater access to the computer’s hardware.

There are many languages to select from when creating software for the Macintosh. Apple has historically charged extra for development tools. A notable exception was when HyperCard was included with every Mac for free, but with the release of Mac OS X, Apple not only departed from the traditional operating system structure with the Mac OS, but also departed from the traditional practice of selling development tools separately. With the installation of the development tools you also get access to free C, C++, Objective-C and Java compilers, as well as shell and Perl scripting within the Unix command line.

In general, we’re going to work with AppleScript as our primary computer language. AppleScript is included with every Macintosh sold, so if you’re reading this column, you probably have a Macintosh computer and you probably, therefore, have AppleScript installed on it. Most of the programs that people write are applications, not operating systems, and AppleScript can create applications as effectively as C++. In fact, if you’re using Mac OS X and have installed the latest developer tools, you have installed a tool called AppleScript Studio which allows you to create applications that look and feel just like any application created with C++.

However, this series of columns isn’t meant to teach you AppleScript, although you will learn much of AppleScript by reading them. We’re going to use AppleScript as the language to teach programming. There are a number of good books available to learn AppleScript, such as AppleScript in a Nutshell and The AppleScript Handbook, and if you’re interested in learning all the ins and outs of AppleScript, either of these books will help you a great deal. So, this column won’t help you write an AppleScript that will take data from FileMaker and put it into a Quark layout, but it will help your thought process in translating what you want to happen into what the computer understands.

Although programming has generally become easier with the evolution of computers, one fact remains as true today as it was when the first computer was built: computers are really stupid. I don’t mean they aren’t very cool, nor that they can’t perform very complex tasks much more quickly than the human brain can. I mean they aren’t intelligent. A computer will do exactly what you tell it to do (or more precisely, what the software tells it to do), every time.

Let me give you an example. If I told you to give me the sum of the numbers 1 through 5, you would (hopefully) know how to do that. The computer doesn’t know unless you specifically give it instructions. You can’t say to a computer, “Sum of the numbers 1 through 5 and give me the result.” You have to be very precise in your instructions, and to do that you need to think clearly about how you would go about getting the result. You probably don’t even think about it when you give me the sum, so it takes a bit of work to consider what instructions you would need to give a computer in order for it to do the same task.

So how do we sum the first five integers? Well, we take the first one and add to it the second one. We take this sum and add the third number, take the sum of that and add the fourth number, and finally take that sum and add the fifth number. But how do we know which numbers to add? Well, we start with 1, the first integer, and we add 1 to it to get the rest of the integers, stopping when the integer we are adding is 5.

Do you see what I mean when I say that computers are stupid? Unless you give the computer instructions like I’ve outlined here, it won’t be able to do what you want it to do. Always remember that: computers are really stupid. Much of the technique of programming is translating what is so clear in our minds into instructions that this very unintelligent computer can understand.

Let’s use AppleScript to build a program that will give us exactly what we’ve just described. Open up AppleScript Editor on your Mac. If you’re running Mac OS 9 or earlier, you’ll probably find this program in “Hard Disk:Apple Extras:AppleScript:.” If you’re running Mac OS X then you should find the program in “/Applications/Utilities/.” If you can’t find it on your system, find it using Sherlock. If you’re still unsuccessful, you will probably have to reinstall it off of your Mac OS CD.

Here’s a program that accomplishes the task of reporting the sum of the numbers 1 through 5.

set sum to 0
repeat with i from 1 to 5
  set sum to sum + i
end repeat
-- Report the results of the process to the user.
display dialog "The sum of the first five number is " & sum & "." ¬
    buttons {"OK"} default button 1

Once you’ve either entered the above program into AppleScript Editor or copied and pasted it from here, click the Run button at the top of the Script Editor window. You should see a dialog box that says “The sum of the first five numbers is 15,” and an “OK” button that you can click to dismiss the dialog box.

This program, although very simple, introduces some important concepts, some of which we’ll get into later. First of all, notice that the statements take the form of commands. You are “commanding” the computer to do something, namely, “Set the variable ‘sum’ to the value of 0.” The computer is following your commands. Makes you feel pretty powerful, doesn’t it?

The next concept is the variable introduced in the first line of the program. The first line sets a variable called “sum” to 0. After that line, and before we set the “sum” variable to something else, referring to the variable “sum” is the same as referring to 0. “sum” holds a value of 0 which we can use elsewhere in the program.

As you program, you’re going to use lots of variables. Sometimes the data you place in a variable will be known to you in advance, as it is in the above program. Often, however, data that you place in a variable is something entered by the user. For instance, our program will only give the sum of the first five numbers, but what if we wanted to give the user the ability to tell the program how many numbers to total? We would get this information from the user and store that in a variable so we could access it later.

The second important concept is the “repeat” statement. This statement says, “Create a variable called ‘i’ and set it to 1. Execute the statements before the ‘end repeat’ statement. Add 1 to i and keep doing it until when you come to the ‘end repeat’ statement i is equal to 5.” Quite a lot to say for such a short line, isn’t it? One of the things you’ll find as you program is that computer languages tend to be compact, but not always. In fact, some computer languages are so compact that reading them is a horrid experience. Reading the above AppleScript makes a lot of sense, because it’s very close to English. Generally, the more compact the language, the more difficult it is to read.

Next we come to the line that reads “set sum to sum + i.” Since this is so much like English, you can probably figure out what that line does. It takes a look at the value in “sum,” adds 1 to that value and resets sum to the new value. Notice that we don’t manually change the value of i within the repeat loop. The repeat loop (when in this form) does this for us in AppleScript. There are other versions of the repeat loop in AppleScript, and similar structures in other languages, where we would have to manually change i. But in this case, every time the repeat loop executes the repeat statement, it increments i by 1.

We’ve already covered what the “end repeat” statement does. The next statement is called a “comment.” Comments are for programmers, not for computers. When the computer translates this program into machine instructions, it ignores the line that begins with two dashes. Every computer language that I’ve ever come across has the ability to enter comments. Comments are used to clarify what is happening in a program or why a certain programming technique was used. As you build programs, comment a lot. I tend to comment almost all of the statements in my program. This way, when I come back to it months or years later, my comments will remind me of what I was originally thinking. Comments will also help other programmers reading your program understand what you’re trying to do.

AppleScript offers two ways to comment your program. The first is to begin a line with two dashes as we did above. The second way is to begin a comment with “(*” and end it with “*)” Comments that begin with two dashes can only appear on one line. Comments that begin with “(*” and “*)” can span multiple lines. Our comment above could have been written as

(* Report the results of the process to the user. *)

Or even

(*
Report the results of the process
to the user.
*)

or perhaps

-- Report the results of the process
-- to the user.

Which style of comments you use is totally up to you. I tend to use the two dash method even when my comments span multiple lines. It doesn’t matter to AppleScript.

The last statement gives the results of our task to the user, showing a dialog box that tells the user what the sum of the numbers is. The “display dialog” command isn’t actually an AppleScript command but a scripting addition. For now, you can treat it as an AppleScript command. The “display dialog” command is the most complex command yet covered, and we’ll get into more detail about it and other scripting additions later. At this time, just keep in mind that if we didn’t report to the user what the results were, then the program would be pretty useless. As you write software, you always need to keep the user in mind, even if you’re going to be the only user. The only reason to write software is so that someone can make use of it.

Congratulations! You’ve built a program. Yes, I know, it isn’t a very complicated program, and I don’t think it’s going to be very much competition for Microsoft Excel, but hey, we’ve got to start somewhere.

Before I sign off for this month, let’s define some terms, in addition to the term “programming language” which we’ve already covered in detail. The first term that’s going to come up often is “source code” or simply “code.” Code is what we write in a computer language, and the process of writing a program is often called “coding.”

The “compiler” is the program that translates the code we write into something the computer can actually understand. The compiler takes our source code and produces “object code,” which is the actual instructions executed by the computer. Source code consists of “statements,” which are individual commands to the computer. Statements usually manipulate “variables,” which we discussed above. We can use a statement to set a variable to a piece of data, and then use another statement to use that data in something else.

That’s all the introductory material we’re going to cover this time. Now that we have an idea of exactly what programming is and have a few of its terms defined, next time we’ll move forward with a more complex program. If you have a great idea for a simple program you would like to write but don’t know how, go ahead and This email address is being protected from spambots. You need JavaScript enabled to view it. your suggestion. If I get some good ideas, we’ll go ahead and build those programs together. See you then.

Add comment


Security code
Refresh

Search

Products