Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

4.6. Writing a Basic Script

Now that you have a record number on the Lease Agreement List layout, and have sorted the list, you can start to see how useful the layout really is. The Lease Agreement layout is great for revealing detail, but when the Lease Agreement List layout is sorted by Last Name, it's a cinch to scroll to the record you need without entering Find mode, typing in search criteria, and then performing a find. But it could be easier sort the records on the layout. As it is, you've got to choose Records→Sort Records, select the field you want to sort by, and then click Sort. All this stuff is easy, but efficiency is king in your world. The solution is to write a script to do these things automatically.

If you're familiar with macros in other programs, then you already get the idea of scripts in FileMaker—you set up scripts to perform tasks for you. The task at hand—sorting—is just one command, but it has several steps. They're all quick steps, but when you have to repeat them several times a day and so does everyone else in your office, all that wasted time adds up to real inefficiency. Also, any manual process leaves room for human error. When you make a mistake, no matter how harmless, you have to undo or redo what you just did. A script that handles your sort is more efficient and less susceptible to error.

4.6.1. Creating a Sort Script

Here's how to write a script that sorts the records on your list layout alphabetically by Last Name and then First Name:

  1. Choose Records→Sort Records. Set up the window to sort by Last Name and then First Name (Section 1.6), and then click Sort.

    Every time you open the Sort window, the last sort order is already in the window. It works the same way for scripts. So save yourself some time by performing the sort first. That way, when you write the script, the order will already be in the Sort window.

  2. Choose Scripts→Manage Scripts.

    The Manage Scripts dialog box appears.

  3. Click New.

    The Edit Script dialog box appears (Figure 4-20).

    Figure 4-20. The Edit Script window contains everything you need to write a script, whether it's a simple one-line script or a hundred-line monster. Here the list of available script steps is filtered by View: Found Sets, but you can also sort all the script steps alphabetically if you prefer. Many of the script steps available are the same as the commands in FileMaker's menus. If you know how to use those commands manually, you know how they'll behave as script steps. But there are some commands that you can only access through scripting, and the subject is so deep and wide that this book has three chapters, 10, 11, and 16, devoted to the subject.

  4. In the Script Name field, type Sort by Last Name.

    As always, give everything you name in FileMaker a descriptive name. Mature databases can have hundreds of scripts, so good naming is the first step in keeping things organized.

  5. In the View pop-up menu, choose Found Sets.

    The filters the list of script steps so you can easily pick the one you need.

  6. Double-click the Sort Records script step.

    The Sort Records script step appears in the window's Script pane.

  7. Turn on "Perform without dialog".

    Without this option selected, you'd see the regular Sort window every time you run the script. Don't turn on this option when you're writing a script that lets the user choose a custom sort as the script runs.

  8. Turn on "Specify sort order".

    The regular Sort window appears, with Last Name and then First Name set up already. As you know, FileMaker remembers your most recent sort order, but it's good practice to verify everything when you're scripting. And if you wanted a different sort order, you can change it and the script will remember your changes.

  9. Click OK to close the Sort window, and then, in the Edit Script window, click Close.

    FileMaker asks if you want to save the script's changes.

  10. Click Save.

    FileMaker saves the script and closes the Edit Script window.

  11. Make sure "Include in Menu" is turned on for the Sort by Last Name script, and then close the Manage Scripts window.

    Now that the Manage Scripts window is closed, you need some way to run the script you just wrote. Look in the Scripts menu. It appears there, along with a shortcut. You can run the script by choosing Scripts→Sort by Last Name or by using the shortcut.


Tip:

Windows users can save scripts before closing the Edit Script window by choosing the Edit Script window's File→Save Script command. Both PC and Mac fans can use the Save Script shortcut Ctrl+S (⌘-S).


Now that the script is ready to go, Unsort your records (choose Records→Unsort), and then run the script.

4.6.2. Creating a Button

Running the script from the menu saved you a few steps, but you can make it even more convenient by attaching the script to a layout object, which then becomes a button. Then whenever you click the button in Browse mode, the script runs automatically. Here's how:

  1. In Layout mode, click the Last Name Field's label to select it.

    It's a common convention to click a column label to sort a column, so help your users out by adopting that principle.

  2. Choose Format→Button Setup.

    The Button Setup window appears (Figure 4-21).

    Figure 4-21. The Button Setup window lets you choose from most of the same steps you see in the Edit Script window. The difference is that you can only choose a single script step when you define a button this way. Any time you need a process that requires two or more script steps, create a script and then attach it to the button. But even if the process is a single step, you may still want a script, so you can format it to appear in the Script menu. Even better, if you apply a script to several buttons throughout your database, you can change the script and all the buttons will run the edited script automatically. But if you had attached script actions to those buttons instead of a script, you'd have to change each one manually.

  3. In the script step list, click Perform Script.

    This "controller" script step lets you run any script you've written by attaching it to a button.

  4. In the Options section of the window, click Specify.

    The Specify Script window appears, showing a list off all the scripts in your database. You've only got one, but it's not uncommon to have hundreds.

  5. Click the "Sort by Last Name" script, and then click OK until you're back on your layout.

    Your button is ready to use.

Switch to Browse mode, and then Unsort your records, if they're sorted. Finally, click the Last Name field label to see the script run. You haven't put anything on your layout to indicate to your users that the field label does anything useful. FileMaker changes the pointer to a hand icon when it's positioned over any button, but you have to give users a reason to wander up there with their mouses. So change the label's formatting (make it a contrasting color, or put a border around it so that it looks like a button) to help users out. (Learn more about buttons on Section 7.6.)


Tip:

Check out this chapter's Lease Agreement Finished.fp7 file to look under the hood at some formatting options and a beefed up script that can sort by different fields, depending on a script parameter (explained on Section 16.5).


4.6.3. Applying a Script Trigger

The script was nice, and the button improved things, but you're still not done learning how useful and intuitive scripts can be. Since the point of going to the list layout is to quickly scan a list so you can find a particular Lease Agreement record, it'd been even more convenient if the list just knew to sort itself every time you switch to the layout. And that kind of thing is what Script Triggers are for.

You've just seen that you can run a script from the Scripts menu or from a button. But you can also tell a script to run when you do other things, like enter data in a field or go so a specific layout. Here's how to make the Sort by Last Name script run every time you go to the Lease Agreement List layout:

  1. If you're not viewing the Lease Agreement List layout, switch to it. Then in Layout mode, choose Layouts→Layout Setup.

    You'll learn about this dialog box's other options in Chapter 7. For now, you're interested in the Script Triggers tab.

  2. Click the Script Triggers tab.

    The Script Triggers tab appears (Figure 4-22).

    Figure 4-22. Script triggers give you a more automated way to run a script than by using the menu or creating a button. This script trigger will run a script called "Sort by Last Name" every time the layout is visited in Browse mode. When you apply a script trigger with the Layout Setup dialog box, it only affects the layout you apply it to. Script triggers are enormously powerful and they can be tricky. Learn more about them on page 430.

  3. Select the OnLayoutEnter option in the Event list. Windows users may need to scroll the list to see that option.

    Selecting an Event tells FileMaker when to run a script. Once you make a selection, the Specify Script window appears.

  4. Click the "Sort by Last Name" script to select it, and then click OK.

    The Script Trigger tab is now set up. Notice that you're only enabling the script to run while you're viewing the layout in Browse mode.

  5. Click OK.

    The script will run each time you switch to the Lease Agreement List layout.

To test the script trigger, Unsort your list, and then switch to the Lease Agreement layout. Then switch back to the Lease Agreement List layout. The script runs and sorts your list for you.

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint