Guide to A good introduction to OneClick.
What Are Floating Palettes?
OneClick's Standard Palettes
How Does it Work?
What Can I Do With OneClick?
Working With OneClick
Don't Be Shy!
Is It Perfect?
|
Don't be shy! Okay, I know that "programming" scares off many people. But with OneClick one doesn't have to be scared. OneClick makes programming remarkably easy. If you have used other computer languages (like BASIC or AppleScript) you will find EasyScript a snap. It's similar to HyperCard's HyperScript, but easier. If you haven't programmed before you will have to learn some new concepts, but it's totally doable. Don't expect to be a master overnight -- give yourself a few weeks. West Code has published some tutorials on using OneClick, and there are more at the Mac Tutorials Site. If there's interest, I can publish some of my own on this site. In the meantime, here's a brief introduction to some basic programming concepts and some sample OneClick scripts.
Some Programming Concepts
Variables Variables are vital to programming because without them a program wouldn't change from one execution to the next. If you had a program that based calculations on the user's age, for instance, you'd have to write a separate program for every possible age -- a mind-numbing task. By assigning the user's age to a variable the program becomes much more flexible -- it now works with any age person. Every language has a way to assign a variable's contents. Typically this is with the "=" command, though languages differ in syntax. In OneClick it works like this:
myVar = 51 When I first started programming in BASIC, variable names were limited to one letter, like x or i. That made it difficult to remember what the variable represented. With modern languages, and OneClick in particular, you can use variable names that are much more descriptive. Use names like "theLoanRate" and "the_Count" instead of shorter, less descriptive names. OneClick variables can be any combination of letters and number, but may not contain spaces. A typical OneClick convention is to not capitalize the first letter of a variable -- since OneClick commands are automatically capitalized, it makes it easier to tell which items are commands and which are variables. Since you can't use spaces in a name, it's nice to use an underscore character ( _ ) between words in a name, or capitalize words after the first word. OneClick has a strict requirement that you define variables before you use them. This is good advice, even if OneClick didn't require it. Since you have the ability to create as many variables as you want, they can get out of hand. Declaring them is a good way to keep organized. In OneClick you declare variables by preceeding them with the "Variable" command:
Variable myVar If you have several variables you'd like to use, you can separate them by commas or put them after another "Variable" command:
Variable myVar, theLoanRate, theLoanAmount, the_Total Variable theTaxRate In OneClick, you can create as many variables as you need for your script, and you can put whatever you want inside them. To use a variable, you simply put its name any place where you would normally use a number or some text. For example, OneClick has a command that displays text within a dialog box. It's useful for displaying an error message.
Message "My message is all this text between double-quote marks." Here's the same command with a variable:
Variable theMessage theMessage = "My message is all this text between double-quote marks." Message theMessage The above scripts would perform exactly the same -- the only difference is that the second one uses a variable and the top one does not. The second one takes the text and stores it inside the variable "theMessage". The final line puts the variable where the text was originally, and OneClick, when it processes the "message" command, replaces the variable name with the contents of the variable -- meaning that the text is displayed in the dialog box. An important concept to understand when using variables is that you can use a variable within its own definition. That's a complicated way of saying that you can add a variable to itself. Look at the following to see why this is important:
Variable myVar myVar = 5 Message myVar myVar = myVar + 5 Message myVar What will be the value of "myVar" at the end of the script? At the first "Message" command, the dialog box will display 5. At the second, it will display 10 -- that is, 5 + 5. Do you see why this is important? If you couldn't add myVar to itself, you'd need an extra variable, like this:
Variable myVar, tempVar myVar = 5 Message myVar tempVar = myVar + 5 myVar = tempVar Message myVar Here we assign the total of myVar plus 5 to a temporary variable, tempVar. Then we put the contents of tempVar into myVar (erasing the original value of myVar). That's much more complicated. You can also do the above with strings -- that is, pieces of text. Since text isn't numbers we don't use mathematical symbols like the addition sign -- instead we use the ampersand: &.
Variable myVar myVar = "This is the first half" myVar = myVar & " and this is the second half." Message myVar The above script combines the two strings into one so the dialog displays "This is the first half and this is the second half." You can join as many text pieces as you'd like:
Message "This is some text: " & myTextVar & " and this is a number: " & myNumVar & "." If your variable contains numbers you can do math with the variable:
Variable myVar myVar = 5 myVar = (myVar * 25) - 6 + 4 - (myVar - 2) Message myVar
Conditonals Computers don't think like people. People think in vague terms and thus have trouble making decisions. Computers think in absolutes -- right/wrong, on/off, true/false. There's no approximate, maybe, or shades of gray. For a computer, any decision involves a mathematical equation equal to true or false. Therefore, for every decision, there are two possible responses. The way you program these is within an "if/then" construct. In OneClick an if/then looks like this:
If condition statements Else statements End If Notice that there isn't an actual "then" in OneClick syntax -- it's implied by the structure of the statement. Any lines between the "If" and the "Else" are executed if the condition is true, and commands between the "Else" and the "End If" (if any) are executed if condition evaluates as false. OneClick automatically handles in proper indenting of the lines -- you can't write the lines without them being indented properly! (Indenting the lines makes the function of the code much clearer.) Here's some actual OneClick code using conditionals:
If Time = "12:00 PM" Speak "It is noon." Else Speak "The time is now " & time End If
Loops OneClick has a number of different kinds of loops. The most basic is the For loop. For loops are used for counting. The basic structure of a For loop is as follows:
Variable i For i = 1 to 10 Beep End For The above script would beep ten times. You can put as many statements within the For loop as you want. You can even nest several loops together:
Variable i, j For i = 1 to 5 For j = 1 to 2 Beep End For End For This would beep ten times (5 multiplied by 2). If you wanted to loop with a variable instead of a fixed number, you could do the following:
Variable i, n n = Random 10 For i = 1 To n Beep End For This would beep a random number of times. The "Random 10" command sets the variable "n" to a number between 1 and 10. (Random 100 would return a value between 1 and 100.) Note that each time through the loop the variable i is upgraded by one. You can use that information within your loop (useful for counting). For example, this routine numbers the paragraphs on the clipboard:
Variable oldClip, newClip, theWord, i oldClip = Clipboard newClip = "" ListDelimiter = Return For i = 1 To (ListCount oldClip) theWord = ListItems oldClip, i, i theWord = i & " " & theWord newClip = newClip & Return & theWord End For Clipboard = newClip Beep Here's what the result looks like (I had the script on the clipboard):
1 Variable oldClip, newClip, theWord, i 2 3 oldClip = Clipboard 4 newClip = "" 5 ListDelimiter = Return 6 For i = 1 To (ListCount oldClip) 7 theWord = ListItems oldClip, i, i 8 theWord = i & " " & theWord 9 newClip = newClip & Return & theWord 10 End For 11 Clipboard = newClip 12 Beep Remember, while For loops are linear, they don't have to start at one or go up. The following examples are legal:
For i = 98 To 112 Beep End For
For i = 112 To 98 Beep End For There are two other kinds of loops: While and Repeat. They look like this:
Variable i, n Repeat simply does something X times. It is similar to a For loop, except there is no variable being incremented or decremented as the loop progresses. While repeats until a particular condition is true. This is useful for conditions you can't test in advance. One thing to remember about a While loop is that if the condition tested for returns false, the loop will never execute (not even once). The contents of a For loop are always executed at least once. (A "For i = 1 to 1" is perfectly valid, but the statements enclosed within the loop are executed only once.) If you know before the loop starts how many times you need it to repeat, you would use a For loop. For example, to search for JPEG files you could know in advance how many files you need to check. (You could use OneClick to ask the OS how many files are in a particular folder.)
Functions Some functions need more than one parameter. For these, you simply put a comma between the values. The "Find" command, for instance, needs two pieces of information: the string to search for, and the string to search inside. Here's the "Find" command at work:
If Find "the", "Is there a the here?" Message "Yes!" Else Message "Not found!" End If Quick quiz: which message will be displayed when this program is executed?
More The best way to learn programming is to try it. Experiment. With OneClick you can examine the scripts others have written, explore the script commands in the help file, and create some test buttons and just play. See how the different commands operate, what information various functions return. Start small -- don't assume you can write something huge and complicated on the first day. You'll just get frustrated and depressed. Try something simple, like making the computer beep. Then make it beep if certain conditions exist. Then try to make the beep useful, like warning you every hour that you need to stretch and take a break. Programs are made up of small parts linked together. Start small and grow -- you'll be pleasantly surprised how easy scripting is!
|