To get a feel for how scripting works, you'll create a really simple script. Suppose you want to find all invoices with a balance due and view them in a sorted list. The following pages show how to go about preparing your database, planning, creating and polishing the script, and finishing off with a way to run it.
13.1.1. Preparing the Database
FileMaker is flexible enough to let you create fields and layouts on the fly, at the same time as you write a script, but it's easier to focus on the script if you create all the supporting material you need before you even open ScriptMaker. In this example, you use a layout that shows a list of invoices, so create that first:
From the "Show records from" pop-up menu, choose Invoices. In the Layout Name box, enter Invoice List.
Since you'll tell the script which layout it needs to use, it helps to name the layout appropriately so that you can pick it from the list of layouts later.
Now you get to pick which fields to include in the list.
FileMaker asks how you want the list sorted. Since this list isn't a report, you can simply ignore this setting. Click Next again.
You now see the header and footer choices. You're ignoring these too, so click Next yet again. FileMaker then asks if it should create a script for you.
You're going to do that yourself, of course. The final screen asks if you'd like to view the layout in Preview mode or Layout mode.
FileMaker creates your new layout and shows it to you. (Feel free to decorate the layout now if you want, using any of the features described in Chapter 4.)
13.1.2. Planning Your Script
Before you dive in and create the script, you should review what you think it's supposed to do. Everything you can do yourself in FileMaker, you can have a script do insteadand this instance is no exception. A script is just a series of steps. When you're planning a script, it often helps to actually do what you want the script to do, so you can see whether it works. So go through the steps necessary to find the balance due, and take notes. You probably end up with something like the following:
Now that you know all the steps involved, you're ready to get acquainted with ScriptMaker.
13.1.3. Creating Your Script
You create, edit, run, and delete scripts in FileMaker using ScriptMaker. It's always available as the first item in the Scripts menu. When you choose this item, you see the window shown in Figure 13-1.
Now it's time to build the script. FileMaker wants you to set up and name the script before adding the actual steps.
|
|||
|
Your script's name is important. It's how you'll identify it when you want to run it later.
The list of available script steps reorganizes to show you every step alphabetized by name.
You're now ready to add steps to your script. You'll be adding a script step for each of the actions you performed when you manually created the list of unpaid invoices back on Section 13.1.2.
FileMaker adds (and selects) a "Go to Layout step to the list of current steps. This script step makes FileMaker go to a layout when the script is run. Your job is to choose which layout it should visit.
Now your script has just one step, or line. If you were to run it as is, it would simply take you to the Invoice List layout. (If you were already on the Invoice List layout, it wouldn't do anything.)
FileMaker adds a second line to your script. The Script Step Options area now has new options (those that make sense with the Enter Find Mode step). Notice that the Pause option at the left side of the Script Step Options area is turned on.
Turning off this option tells the script not to wait for user input when it's run. (You'll learn more about pausing a script on Section 14.4.1.)
The procedure is similar to the previous two steps: Select Set Field in the available field list and click Move.
The Set Field step lets you put (set) something into that field. You can use Set Field in Browse or Find mode. In Browse mode, it enters data; in Find mode it specifies find requests. In this case, you're specifying a find request for the invoices.
This tells FileMaker you want to indicate exactly which field to set. As soon as you turn this option on, FileMaker shows a slightly different Specify Field window (Figure 13-3).
The Set Field step in your script (Figure 13-4) shows the option you've selected. In the Script Step Options area, click the bottom Specify button (the one beside "Calculated result.")
|
|
FileMaker shows you the Specify Calculation dialog box, the same one you used to build calculation fields in the previous few chapters. Here, though, you're using a calculation to tell FileMaker what to put in the Total Due field. When this step runs, you're in Find mode thanks to the second step in this script, so the next step tells FileMaker what to look for.
This bit of computerese sets the Total Due field to the appropriate value for finding unpaid invoices. FileMaker now shows the calculation beside the Set Field step as well. (Spoken out loud, this step reads, "Set the Invoices::Total Due field to greater than zero.")
This step carries out the find operation. It's the equivalent of clicking Find or pressing Enter if you were doing it yourself.
This tells FileMaker you want the script to go ahead and sort, instead of opening the Sort dialog box every time it runs. Instead, since the sort's the same every time, you put it right here in the script.
Assuming you just went through the sort as described on Section 13.1.3, the Sort dialog box is already configured properly. (When you add a Sort Records step to your script, FileMaker configures it to match the last sort you performed.)
FileMaker shows you the Define Scripts window again. This time, though, your new scriptFind Unpaid Invoicesis in the otherwise empty list. Time to check your work.
FileMaker runs your script. You should immediately see the correct list of invoices, oroops!a message telling you no records were found. In the next section, you'll refine your script to help deal with these message boxes if they pop up when someone runs the script. (See the box on Section 13.1.4.)
Note: You can adapt the basic structure of this script for a host of purposes. Just edit the script's find or sort criteria and specify a different layout.
13.1.4. Improving Your Script
On the preceding pages, you created a script that mimics what you would do to get an unpaid invoice list, step by step. But ScriptMaker has a lot of options to make writing scripts fast and easy. Often, a script can get the results you want with fewer steps than it would take to do it yourself. For example, to see your unpaid invoice list, the steps boil down to the following:
This version has just three steps instead of five because it assumes you can "find the right invoices" in one step. Luckily, ScriptMaker's Perform Find script step really can find what you want in one step. Here's how to revise your script to use the simpler form:
Select the second and third steps in your script (Enter Find Mode and Set Field).
You can select both by clicking Enter Find Mode in your script, then Shift-clicking Set Field.
UP TO SPEED To Be Continued |
If your database has no unpaid invoices when you run the Find Unpaid Invoices script (Section 13.1.3), you see the message shown here. The message is the same as the one you see if you perform the find manually, with one exception: This time you get a Continue button. Since FileMaker was running a script when the error occurred, it gives you the choice to cancel the script (in other words, stop in the middle and return control to the user) or continue the script (or keep going and pretend nothing went wrong). In this case, Cancel and Continue do the same thing since the error happened on the last step in the script. If you don't want the user to make this choice, you can tell FileMaker to capture the errors as they happen and let you deal with them inside the script. You'll learn about this process on Section 15.4. |
FileMaker removes the two selected steps from the script. It now has two steps that look a lot like the simplified process above. Also notice that the Perform Find step is already selected, since FileMaker automatically selects the next step when you clear one or more steps from the script.
The Specify Find Requests dialog box (Figure 13-5) appears. Since the find request FileMaker created for you is exactly what you want, you don't need to do anything in the Specify Find Requests window.
When you click OK, you see the Edit Script window again. Figure 13-6 shows how your script should look now. The script is now finishedand it's just three lines.
|
|
You see your unpaid invoice list again.
This new, simpler script does everything the first script did. It's simpler because you didn't have to script all the steps involved in performing a find: Enter Find Mode; Set Field; Perform Find. Instead, you let FileMaker do all of that in one Perform Find step. You'll find that you can reduce many of FileMaker's common multi-step operations to a single step in ScriptMaker.
Part I: Introduction to FileMaker Pro
Your First Database
Organizing and Editing Records
Building a New Database
Part II: Layout Basics
Layout Basics
Creating Layouts
Advanced Layouts and Reports
Part III: Multiple Tables and Relationships
Multiple Tables and Relationships
Advanced Relationship Techniques
Part IV: Calculations
Introduction to Calculations
Calculations and Data Types
Advanced Calculations
Extending Calculations
Part V: Scripting
Scripting Basics
Script Steps
Advanced Scripting
Part VI: Security and Integration
Security
Exporting and Importing
Sharing Your Database
Developer Utilities
Part VII: Appendixes
Appendix A. Getting Help