Your First Script

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:

  1. Switch to Layout mode in your database and choose Layout New Layout/Report.

    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.

  2. In the "Select a layout type" list, select Columnar list/report, and then click Next. Leave the "Columnar list/report" radio button turned on, and then click Next again.

    Now you get to pick which fields to include in the list.

  3. Using the techniques described on Section 4.4.4.4, add the following fields to the "Layout fields" list: Invoice ID, Jobs::Name, Date, Date Due, and Total Due. When you're finished, click Next.

    FileMaker asks how you want the list sorted. Since this list isn't a report, you can simply ignore this setting. Click Next again.

  4. On the Themes screen, choose one that pleases you, and then click Next.

    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.

  5. Choose "Do not create a script" and click Next.

    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.

  6. Select "View the report in Layout mode," and then click Finish.

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.

Figure 13-1. The Define Scripts window is more affectionately known as ScriptMaker and is accessed via the Scripts ScriptMaker menu command. The window itself is dominated by an empty white space. As you add scripts, theyre listed here. You use the buttons along the right edge to create, edit, run, and delete scripts (and a few other things). You'll learn all about these options in the next section.

 

Now it's time to build the script. FileMaker wants you to set up and name the script before adding the actual steps.

  1. Choose Scripts ScriptMaker. When the Define Scripts window appears, click New.

Figure 13-2. In the Edit Script window, you build your script by choosing steps from the Available Script Steps list and use the Move button to add them to your script. Once they're there, you can configure them using buttons, checkboxes, and radio buttons that appear in the Script Step Options area (it's empty now because you don't have any script steps).


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:

  1. Choose Scripts ScriptMaker. In the Define Scripts window, select the Find Unpaid Invoices script, and then click Edit.

    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.


    Note: You should be selecting the steps in your script (in the window on the right), not the steps in the available script steps list (on the left side of the window).
    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.

     
  2. Click the Clear button.

    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.

  3. Turn on the "Specify find requests" checkbox in the Script Step Options area.

    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.

  4. Click OK.

    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.

    Figure 13-5. This window opens whenever you attach specific find requests to a Perform Find script step. From here you can add new find requests to your script. If you look in the list, you see that FileMaker's already added a line for you. It has an Action of Find Records and Criteria that look like this: Invoices::Total Due: [>0]. Like the sort order, this is the last find you performed, and it's also just what you want.

     

    Figure 13-6. Your finished script should look like this. It wasn't too hard to build, and it's very easy to understand. No other scripting tool makes writing scripts this easy. And as you'll find out soon, this ease doesn't mean the scripts themselves have to be simple.

     
  5. Click OK again, and then click the Perform button.

    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.

Категории