Chapter 14. Script Steps

Now that you know how to create scripts (you did read the previous chapter, didn’t you?), it’s time to expand your repertoire. FileMaker has a script step for just about everything you can do from the menus and status area. You can use any combination of these steps with script techniques like looping, branching, custom dialog boxes, and more to automate just about anything FileMaker can do. Major areas of scripting include working with field data and records, finding, sorting, working with windows and files, and printing. This chapter is a compendium of steps—and boatloads of scripting possibilities.

Go to Layout

The Go to Layout script step was introduced on Section 13.1.3. Its purpose is simple: Change layouts. It works just like making a choice from the Layout pop-up menu in the status area, except that the script can go to any layout (even if it doesn’t show in the menu).

Go to Layout has just one option, a pop-up menu labeled Specify. In addition to every layout in your database, this menu also has three special choices:

  • Theoriginal layout option causes FileMaker to switch to the layout your user was on when the script started. After all, you can run lots of scripts anywhere, especially if they’re on the Script menu. Since scripts often change layouts as they run, this option makes sure folks end up back where they started.

  • The Layout Name by calculation option lets you specify a typical FileMaker calculation. The result of the calculation must be text, and it should match the name of one of the layouts in the database. When the script runs, FileMaker evaluates the calculation and switches to the layout with the correct name.

  • The Layout Number by calculation option is similar. You specify a calculation with a number result. FileMaker numbers every layout sequentially, in the order in which they’re listed in Layout mode. The result of the calculation determines which layout to visit by number.

Note

Layout numbers don’t necessarily correspond to their position in the Layout pop-up menu. Remember that not all layouts show in this list and you can reorder them manually. To find out a layout’s number reliably, switch to Layout mode first. Then go to the layout and look at the Layout box in the status area. It shows which layout number you’re on.

See the box below to learn when to use Go to Layout with Layout Name and when to use Layout Number.

Scripting for Fields

Most people using FileMaker spend a lot of their time working with field data, so it’s no surprise that script steps devoted to fields abound. You can put the user in a specific field, select field text, and even play sounds and movies from container fields. You can perform a find and replace operation, run the Replace Field Contents command, and export field contents to a file. Finally, you get more than 10 ways to put data into a field.

Navigating Fields

FileMaker offers a couple of ways to field-hop—pressing Tab to move to the next field in order, or just clicking the desired field. It has script steps that mimic both techniques.

Go to Field

The simplest field navigation script step is called Go to Field. It’s really a two-purpose step, with two checkboxes to prove it. In its simplest form, you turn on the “Go to target field” checkbox and pick the field you want to go to. When the script runs, FileMaker puts the user in the specified field (provided it’s on the layout).

Tip

When you use Go to Field in a script, FileMaker dutifully ignores the field behavior specified on the layout and puts the user right into the field. Remember this point if you ever want a field that users can only get to by using your script.

The step also has a checkbox called “Select/perform.” When this option is turned on, the script either selects the contents of the field it goes to or does what’s in the field—if possible. For example, if the step goes to a container field that holds a sound or movie, FileMaker plays the contents instead. If the container field holds a reference to a picture or file, FileMaker opens the correct file, using the appropriate program.

The mildly weird thing about Go to Field is you can turn on “Select/perform” and turn off “Go to target field.” With the options set this way, Go to Field doesn’t go to a field at all. Instead, it simply selects or performs the contents of the field you happen to be in when the script runs.

Go to Next Field and Go to Previous Field

To mimic the process of tabbing through fields, FileMaker has two more script steps. The first, called Go to Next Field, just tabs to the next field in the tab order. You probably already figured out that Go to Previous Field goes to the previous field in the tab order. These steps don’t have a “Select/perform” option—they just go to the field.

Tip

To get the effect of Select/Perform, use Go to Next Field or Go to Previous Field to get to the field you want, then use Go to Field to select/perform it. When you add the Go to Field step, don’t turn on “Go to target field,” and it acts on the field you’re already in.

Editing Commands

FileMaker has all the classic commands in its Edit menu: Undo, Cut, Copy, Paste, and Clear. It also has a script step for each of these commands. The Undo step is the simplest. It has no options at all, and has exactly the same effect as choosing Edit → Undo. It’s rare that you want to Undo something you just scripted, so you’ll rarely use this script step. It’s quite handy, though, if you use Custom Menus (Section 19.5.3) to control users’ access to certain menu commands because you need to provide scripts for everything your users might want to do.

Cut, Copy, and Clear are slightly more complicated, with two options each. The first, “Select entire contents” lets you decide which part of a field’s value gets copied and/or cleared. If you turn this option on, FileMaker selects the entire field before acting. If this option’s turned off, FileMaker copies whatever happens to be selected in the field. You also get a “Go to target field” option, through which the script can tell FileMaker which field to act on. If it’s not turned on, it uses the current field—the one the user’s in when the step runs.

The Paste step is the most complicated of these four. In addition to specifying which field you want, and whether or not to select everything in the field before pasting, you get an option to “Paste without style.” When you turn this option on, FileMaker pastes the text on the clipboard, but throws away any style information. If you’re in Windows, you get a fourth option: “Link if available.” If the data on the clipboard comes from a source that supports object linking, FileMaker embeds the linked object. Changes to the original data shows up in FileMaker. Turn this option off if you just want the script to paste a copy of the data.

Selecting Text

FileMaker has two script steps to help you select text. The first, called Select All, selects everything in the current field, just like the Edit → Select All command. If you need more control, use Set Selection instead. This step has two options. First, you can specify a target field so that FileMaker operates on the current field. The step also has a separate Specify button (below the one associated with “Go to target field”) that brings up the Specify Set Selection window (Figure 14-1).

This window lets you tell FileMaker exactly what text you want the script to select. You can type numbers directly in the Start Position and End Position boxes, or click either Specify button to bring up the Specify Calculation dialog box.
Figure 14-1. This window lets you tell FileMaker exactly what text you want the script to select. You can type numbers directly in the Start Position and End Position boxes, or click either Specify button to bring up the Specify Calculation dialog box.

If you imagine the text in your field as a string of letters, numbers, spaces, and punctuation, then you can pretend each of these is numbered. For instance, the word “Missing” has letters numbered one through seven. You first tell FileMaker where the selection should start by putting the number of the first character in the Start Position box. Next, you put the number of the last character in the End Position box. When the step runs, FileMaker selects these two characters and everything in between.

Editing Field Data

Editing field data is such an important part of FileMaker that there are more than 10 ways to put stuff in fields with a script step. On Section 13.1.4, you learned that the scripted process for creating a printed report could actually be shorter and more efficient than the manual process. Editing field data via scripts is also different from manually editing data, and the following sections cover those differences.

The first thing to understand is that most of these steps work only if the field is on the current layout. This property’s no problem for scripts you use to structure a user’s data entry, but it can be a roadblock in other cases. Suppose you have a script that sets a “Paid in Full” flag on an invoice. You want this script to work no matter what layout you’re on (as long as it’s one that shows Invoice records), and whether the field is on that layout or not. In that case, you need to understand the Set Field script step (Section 14.2.4.2).

Using Cut, Copy, and Paste

Your first inclination might be to use Cut, Copy, and Paste in a script to move data from one field to another, and it works well. You can, for example, have a script copy the Customer ID field, then go to the Invoices layout, create a new invoice, and paste it into the Customer ID field there.

But most developers recommend against this approach for two reasons. First, many people consider the user’s clipboard to be sacred ground. They argue you should never change what’s on the clipboard unless the user asks you to. So it would be OK to have a Copy button by the Address fields, for example, that copies the address to the clipboard. But what if the user copies a long product description to the clipboard, and then runs the Create Invoice for Customer script? She then tries to paste the description in the invoice line item, only to find that the description’s gone, replaced by the Customer ID for some odd reason. To keep from frustrating the user (or yourself), you should generally avoid Cut and Copy.

Also, these steps don’t work if the field isn’t on the layout. People often show the customer’s name on an invoice, but not his ID. But if you delete the Customer ID field from the Invoice layout, any scripts that use Cut, Copy or Paste and the Customer ID field break. That’s where Set Field steps in to save the day.

Set Field

The one field-editing power step that doesn’t care a whit about what fields are on the layout is called Set Field. This step replaces the contents of a field with the result of a calculation. Its two options let you specify the field to set, and the calculation to use. The calculation result must be the same type as the field you’ve specified. For Text, Date, Time, and Timestamp fields, Set Field is usually the step of choice: It’s flexible and reliable, no matter what’s on the layout.

Like many of the steps you’ve seen so far, you can choose not to specify a field at all. When you do, Set Field changes the field it’s in at the moment the step runs. (The current-field method works only with text results; otherwise, you have to specify the field so FileMaker knows what type you have in mind.)

Insert Calculated Result

Set Field’s one weakness is that it always overwrites a field completely (but see the tip below). Another step, Insert Calculated Result, lets a script put data in a field while keeping the data that’s already there. It has three options. First you can specify a target field. You can also choose “Select entire contents” in the field first (in which case it overwrites the entire field, just like Set Field). Finally, you get to specify the calculation. Here are some variations on these options:

  • If you don’t turn on the “Select entire contents” option, FileMaker inserts the calculation result after whatever’s already in the field.

  • If you don’t specify a field at all, and you don’t turn on “Select entire contents,” then FileMaker inserts the result of the calculation into the current field. If you select data when the script step runs, the calculation result overwrites whatever’s highlighted. Otherwise, the text goes in wherever the insertion point happens to be, just as though you’d typed it from the keyboard.

Tip

You can, if you wish, use Set Field to append results to existing data: Just include the field’s data in the calculation. If you want to add “Esquire” to the end of the customer’s last name (in the Last Name field), just use this calculation in your Set Field step:

	Last Name & ", Esquire"

Inserting other values

FileMaker has six other Insert script steps that work like Insert Calculated Result. Each step lets you specify a target field and select the field contents if you want. They differ only in what gets inserted:

  • Insert Text lets you specify any arbitrary text value and add it to the field verbatim. Use this if you know ahead of time what you want your script to put in the field, and don’t need to calculate it.

  • Insert from Index makes your script show the same View Index window just like choosing Insert → From Index in Browse or Find mode. The user picks a value from the list and FileMaker inserts it into the field. This option’s especially valuable in Find mode, both to keep your user from having to type a value (and possibly making a typo) and to make sure that the search always finds records, since if a value’s in the index, it’s in a record somewhere.

  • Insert from Last Visited is an interesting step. It grabs the value from the same field on the last visited record and inserts it. This step is particularly useful on data entry layouts. Imagine, for example, you have to enter 300 Person records from 15 different companies. You could use this step to create a button that pops in the last company you typed into the Company Name field, rather than type it over again.

  • Insert Current Date and Insert Current Time do just as they say. Unfortunately, there’s no Insert Current Timestamp step (use Insert Calculated Result and the Get (CurrentTimeStamp) function instead).

  • Insert Current User Name puts the user name of the person using FileMaker into the field. Your operating system normally determines this user name, but you can easily change it from FileMaker’s Preferences’ General Tab.

See the box on Section 14.2.5 for advice on when to use Set Field and when to use an insert step.

Putting data in container fields

In Part 4, you learned that calculations can work with container data. Set Field and Insert Calculated Result are no exception: You can use either of them to move pictures, movies, sounds, and files from one container field to another. You can also use Cut, Copy, and Paste to work with container fields, provided you’re willing to live with the caveats for intruding on your user’s Clipboard described on Section 14.2.4.

But FileMaker has special commands in the Insert menu to get container data into your database in the first place, and these commands have script step equivalents:

  • Insert Picture lets you specify the file to insert, and whether you want to “Store only a reference” (see Section 2.7.4 for a refresher on references). If you don’t specify a file, FileMaker asks the user to pick one when the step runs.

    Note

    The dialog box the user sees when he’s asked to pick a picture file includes the “Store only a reference” checkbox, regardless of how you set this option on the script step itself. In a sense, your choice in the script becomes a suggestion to the user: It determines how the checkbox is set when the dialog box pops up. The user’s free to change it.

  • Insert QuickTime has only one option: the file you want to insert. Again, if no file is selected, the user gets to pick when the script runs. Since QuickTime files are always stored as a reference, you don’t get that choice this time.

  • Insert File has the most options of all. Of course you get to pick the file to be inserted. You also get a “Store only a reference” checkbox. This time, though, you also get a “Go to target field” checkbox. You can use it to tell FileMaker which field to put the file in.

Insert Picture and Insert QuickTime don’t have an option to tell FileMaker which field to use, which probably seems odd. They’re designed to put things in the current container field the user clicks before the script runs. If you want more control, just use Go to Field first, specifying the appropriate field. Then use Insert Picture or Insert QuickTime to insert into that field.

A Field Script in Action

Suppose you decide to add a Notes field to the Customer table in your database. You use this field to hold any arbitrary information you think is important about the customer. Unfortunately, you soon realize this field is a little too unorganized. You’ve no idea if the note that says, “Customer already paid” is from last Tuesday or last year. What you need is a consistent way to keep track of who left a note, and when. You decide everybody should record this information along with any note they leave. To make things even easier, you want to be sure people add new notesabove older notes. Thus, when a customer record has been around for a while, the Notes field would look something like this:

	--- 12/11/05 @ 3:30 PM by Summer ---
	Called the customer, confirmed order was received.

	--- 12/01/05 @ 1:25 PM by Stacey ---
	Customer called saying he never got his order. I checked and we have no
	record of shipment. I'm shipping again.

	--- 11/03/05 @ 4:58 PM by Dave ---
	Order came in really close to quittin' time. I'll deal with it later!

In this example, you create a script that “forces” all added comments into that format. This script adds a separator line with the date and time (plus a couple blank lines) and leaves the insertion point under the separator. You also create a button next to the Notes field that runs this script. All the user has to do is click and type.

Note

Like almost every problem you ever solve with a script, you can do this process in about 24,601 ways. One way’s described below, and another in the box on Section 14.2.5.1. You may prefer a different way, and that’s OK.

Before you start creating your script, think about what you need to do in sequence. Here’s a breakdown:

  • Put the insertion point at the start (top) of the Notes field.

  • Insert two blank lines—to create some space before the previous comment.

  • Put the insertion point back at the start, and add the separator line with the date and time.

  • At that point, the script ends and the user can start typing.

Your next mission is to translate these plain-English steps into ScriptMaker steps, which you’ll do in the next section.

Building the script

If your Customers table doesn’t already have a Notes field, create one before you write the script. Then proceed as follows:

  1. Choose Scripts → ScriptMaker. Create a new script called Add Note Separator .

    Develop the habit of giving your scripts descriptive names so you can remember what you want them to do.

  2. Add a Set Selection script step to the script.

    You can find this step under Editing in the list, or you can choose “all by name” from the View pop-up menu to see an alphabetical list. When you add the step, it appears in your script.

  3. Turn on the “Go to target field” checkbox. If necessary, choose Customers from the table pop-up menu, and then choose the Notes field and click OK.

    The Set Field step in your script updates to show the target field.

  4. In the Script Step Options area, click the second Specify button.

    It’s not labeled, but it’s below the first Specify button. When you click it, the Specify Set Selection window appears.

  5. In both boxes (Start Position and End Position) type 0 (zero).

    Zero in both boxes tells FileMaker you want the insertion point right at the start of the field, and you don’t want any text selected. (Positioning at the start of a field is the single exception to the box on Section 14.2.4, which discusses how to set the insertion point without selecting text.)

  6. Add an Insert Text step to the script and turn off the “Select entire contents” option.

    You don’t want the two blank lines you’re about to insert to replace everything in the field.

  7. Click the bottom Specify button.

    A window called simply Specify appears (Figure 14-2).

    The Specify dialog for Insert Text seems puzzling, because there aren’t any options for you to click. You just type the exact text you want the script step to insert. Note, this box isn’t a calculation dialog box. It only allows the insertion of literal text. Use Insert Calculated Result if you need to insert dynamic text with a script step.
    Figure 14-2. The Specify dialog for Insert Text seems puzzling, because there aren’t any options for you to click. You just type the exact text you want the script step to insert. Note, this box isn’t a calculation dialog box. It only allows the insertion of literal text. Use Insert Calculated Result if you need to insert dynamic text with a script step.
  8. In the Specify window, add two empty lines (press Return or Enter twice), and then click OK.

    That’s the regular Enter key, not the one in the numerical keypad: In this dialog box, that key just clicks the OK button. Anyway, after pressing it twice, you don’t see the returns you’ve typed in the dialog box, but you do see two blanks spaces inside quotes in the Insert Text script step. Those returns tell FileMaker to add two blank lines to the top of the Notes field.

  9. Select the Set Selection step at the top of the script, and then click the Duplicate button at the bottom of the window.

    FileMaker adds a second copy of the Set Selection step, right below the first.

  10. Drag either Set Selection step to the bottom of the script.

    Mouse junkies can use Ctrl+down arrow (⌘ -down arrow) to move script steps.

    The ultimate goal is to have a script with Set Selection, then Insert Text, and then Set Selection again. It doesn’t matter which Set Selection step you drag, as long as one’s on top and the other on the bottom when you’re done.

  11. Add the Insert Calculated Result step to the script and turn off the “Select entire contents” option.

    Make sure the step lands afterthe last Set Selection script. If it doesn’t, move it there. Turning off the selection option ensures that the calculation goes right in the middle of the text, not on top.

  12. Click the Specify button to the right of “Calculated result” and enter this calculation in the Specify Calculation window:

    	"--- " & Get(CurrentDate) & " @ " & Get(CurrentTime) & " by " &
    	Get(UserName) & " ---¶"

    You can use any method you want to enter the calculation, as long as it does the same thing as this one.

  13. Click OK until you’re back in your database.

    Your new script is ready to run. Now you just need to add the Notes field to the Customers layout, and a button by it that runs the new script. When you click the button, FileMaker adds the separator to the field, and puts the insertion point in place. Your users can now type notes that are nicely organized and separated.

Other Steps that Work with Fields

Lots of times, you want to be able to write scripts that work on multiple records. You might need to change values across a found set of records, or you might want to let FileMaker handle the serial numbering of all the records in a table. The next script steps let you manage data in lots of records without lots of hassle.

Perform Find/Replace

The Perform Find/Replace script step brings all the power of the Find/Replace command (Section 2.5.1) to your scripts. You can include the find parameters in the script step, or create a calculation to determine the parameters on the fly, as shown in Figure 14-3.

When you use this step, you pick which action you want to perform from the Perform pop-up menu. Your choices are Find Next, Replace and Find, Replace, and Replace All (these correspond with the buttons you use to dismiss the normal Find/Replace dialog box). The “Find what” and “Replace with” boxes each have a Specify button by them. If you know exactly what you’re looking for, you can type it right in these boxes. If you click Specify, you get to use a calculation to set these values instead.

For example, say your Notes field is such a smashing success, you find some customers have pages of notes—and it’s becoming a problem to find what you want.

You’d like a quick way to search the material in the Notes fields. Figure 14-4 shows one solution—add a button to the Customers layout that runs a Find script.

The window behind the Specify button on a Perform Find/Replace script step looks a lot like the normal Find/Replace window. But this version has a new pop-up menu (called Perform) and a couple new Specify buttons that let you specify calculated values for what you’re looking for and what you want to replace the found results with.
Figure 14-3. The window behind the Specify button on a Perform Find/Replace script step looks a lot like the normal Find/Replace window. But this version has a new pop-up menu (called Perform) and a couple new Specify buttons that let you specify calculated values for what you’re looking for and what you want to replace the found results with.
Here’s the Customers layout with a Notes field and a search field added. To find what you’re looking for, you can type something in the search field and click Find. On the following pages, you’ll write the script behind that Find button.
Figure 14-4. Here’s the Customers layout with a Notes field and a search field added. To find what you’re looking for, you can type something in the search field and click Find. On the following pages, you’ll write the script behind that Find button.

The script is simple. It just needs to go to the Notes field and use the Perform Find/Replace script step. You need a field to type the search value into, which you could create before opening ScriptMaker, but in real life you probably think of that after you start writing your script. FileMaker makes it easy to create fields without leaving the warmth and protection of ScriptMaker, as you’ll see in the following steps.

Here’s how to make the script. Call it Search Notes Field:

  1. Add Go to Field as your first script step. Set it to go to the Customers::Notes field.

    First things first: You want to make sure the script searches the correct field. Now you create the Find script itself.

  2. Add the Perform Find/Replace step to the script, and turn on the “Perform without dialog” option. Click the Specify button.

    You see the Perform Find/Replace Options window.

  3. Make sure the Perform pop-up menu is set to Find Next, then click the Specify button to the right of the “Find what” box.

    The Specify Calculation window appears.

  4. From the pop-up menu above the field list, choose Define Database .

    It makes sense for this field to use global storage, and since it doesn’t really belong to a customer, you add a new table for it as well. You can do that right here in the Define Database window.

    Note

    Scripts often make use of global fields, so FileMaker lets you create a quick table to hold them without cluttering or interfering with your relationship graph (Section 8.7.3).

  5. Click the Tables tab and add a new table called Globals.

    This table holds global fields that aren’t tied to specific tables. You don’t need a relationship to a table in order to grab the value from a global field, so a table like this makes them very easy to find. If you’re working in the file you used back in Chapter 8, you may already have a table for your globals. If so, add your new global fields there.

  6. Click the Fields tab, and select Globals from the Table pop-up menu. Add a new text field called Note Search Value and set it to use global storage. Click OK.

    You now have the only field your script needs. See Section 3.3.4.2 for more information on global storage fields.

  7. Double-click the Globals::Note Search Value field to create its field reference as your calculation. Click OK.

    This calculation tells FileMaker where to look when it runs the Perform Find/Replace step—whatever’s in the new field, in this case. Clicking OK when you’re done returns you to the Perform Find/Replace Options window.

  8. In the From the Direction pop-up menu, choose All .

    This options tells FileMaker to look through the entire Notes field.

  9. Turn on the “Current record/request” and “Current field” radio buttons. Click OK until you’re back in your database.

    You don’t want the search to spill into different fields or records. These options restrict it to just the record the user’s viewing. Since the script first goes to the Notes field, FileMaker searches only that field.

To finish the job, add the new global field and a Find button to the Customers layout (as shown in Figure 14-4 on Section 14.2.6). Then type a value into the search field and click your script button to test your new script.

Replace Field Contents and Relookup Field Contents

It’s probably no surprise to you that you can execute the Replace Field Contents and Relookup Field Contents commands from a script. These steps let you specify a specific field to act upon. If you don’t specify a field, they act on the current field. You also get the typical "Perform without dialog” checkbox. When you turn this checkbox on, the action happens immediately when the step runs. If you leave this option off with the Replace Field Contents step, the user sees the typical Replace Field Contents dialog box (with all the settings you specified in the script). With the Relookup Field Contents step, the user just sees a confirmation message first, asking if she really wants to perform the relookup operation.

Set Next Serial Value

The Set Next Serial Value script step is invaluable—in the rare cases when anyone actually uses it. It’s a one-trick pony: If a field auto-enters a serial number, this step changes the “next value” stored in the Field Options dialog box. In other words, if you want to start your customer ID values over again, you can use Set Next Serial Value to do it from a script. (See the box on Section 14.3.1 for an example.)

Warning

Use extreme care when you change a field that’s used as a key in a relationship. You risk leaving related records orphaned if you don’t change their key fields, too. See the box on Section 15.5.6.3.4 for a script that helps you change key fields without losing related records (or your sanity).

The step has two options. First, you can specify the field to update. As usual, if you don’t specify a field, it works on the current field. You tell FileMaker what to set the “next value” to by entering a calculation.

Working with Records

You can get only so far with your scripts by working with field values. Eventually you need to deal with more than one record. Thankfully, FileMaker has script steps for creating, duplicating, and deleting records; navigating among existing records; and even managing the process of opening and editing a record, and saving (committing) or reverting the changes. You can also work directly with portal rows on the current layout—and the records they represent.

Creating, Duplicating, and Deleting Records

New Record/Request and Duplicate Record/Request have no options, and do exactly what you’d expect. The first script step creates a new record, just like the Records → New Record menu command. The second duplicates the current record. In either case, the new record becomes the current record, just like when you do it manually. You use these steps most often on buttons, when you’re taking away menu commands from people and providing them with buttons that only appear on the layouts where you want your users to be able to create records.

The Delete Record/Request script step deletes the current record. If you turn on its “Perform without dialog” option, the delete happens automatically with no warning. When this option is turned off, the user sees the same “Are you sure” message box he’d see when deleting a record manually.

Note

Each of these three script steps also works for find requests when a script runs in Find mode.

Navigating Among Records

There are two ways to change to a different record. You can move to it with the Go to Record/Request/Page script step, which works a lot like the Book icon in the status area. Or you can switch to a related record, or a set of related records, using the Go to Related Record script step.

Go to Record/Request/Page

FileMaker has one script that handles changing records, find requests, and pages. This might seem strange at first, but it makes sense because it’s exactly how the Book icon in the status area works: If you’re in Browse mode, the step goes to a different record. If you’re in Find mode, it switches find requests instead. Finally, if you’re in Preview mode, it flips through pages.

Note

You can’t run scripts when in Layout mode, and a script can’t go to Layout mode, so it doesn’t apply here.

The Go to Record/Request/Page step has just one option. You get to pick which record, request, or page to go to from a simple list:

  • First

  • Last

  • Previous

  • Next

  • By Calculation

When you lock your users out of the usual interface and control everything they do through the script (see the box on Section 14.6.5), the First, Last, Previous, and Next options let you provide your own customized replacement for the status area and Book icon. For example: Make four buttons and arrange them in a horizontal line (like those on a tape recorder or CD player). Give each button a Go to Record step and set them up in the follwoing order, so they mimic the tape recorder concept:

  • Leftmost button – Go to Record/Request/Page [First]

  • Second button – Go to Record/Request/Page [Previous]

  • Third button – Go to Record/Request/Page [Next]

  • Rightmost button – Go to Record/Request/Page [Last]

This visual concept is so common that your users are likely to know how the buttons work without any instructions. But you can add a tooltip (Section 19.4) to each button if you want to provide that extra boost.

Another common, but more advanced use of this step is to provide a way for a looping script to end. When you choose the Next or Previous option, a new checkbox appears in the Script Step Options area, called “Exit after last.” When it’s turned on FileMaker knows to exit the loop after it’s finished with the last record (when you choose Go to Record [next]) or after the first record (when you choose Go to Record [previous]). (When to exit a loop is no small matter, as Section 13.4.2 explains.)

When you choose By Calculation, you get the chance to specify a calculation with a number result. FileMaker goes to exactly that record, request, or page. You could use this option if the record number’s in a field, or if you want to skip ahead 10 records each time the script is run, for example.

Go to Related Record

You were first introduced to this power-step when learning about relationships (Section 8.1.3). It can go to a different record, found set, layout, window, and even file; all in one step. This step’s job is simple: It takes you to a related record. But carrying out that job isn’t so simple. When you click the Specify button, you get a wealth of choices, as shown in Figure 14-5. To go to a related record, you first need to tell FileMaker which related table occurrence you’re interested in, by selecting it from the “Get related record from” pop-up menu. For example, if you’re on the Customer layout and you want to see a related invoice, you should choose Invoices from the menu.

With this done, FileMaker can find the right record. But how should it show it to you? If you’re visiting related records, chances are you can’t view them directly on the current layout (since the layout is associated with the wrong table occurrence). So a Go to Related Record command almost always involves changing layouts. You pick the layout you want from the “Show record using layout” pop-up menu.

This window’s the same one you saw when you attached a Go to Related Record script step to a button back on Section 6.7.2 → But inside a script you wield a whole new level of power when you tie this command to other processes, like printing a report from a found set that changes based on whichever record a user’s on when running the script.
Figure 14-5. This window’s the same one you saw when you attached a Go to Related Record script step to a button back on Section 6.7.2 → But inside a script you wield a whole new level of power when you tie this command to other processes, like printing a report from a found set that changes based on whichever record a user’s on when running the script.

At first you might be surprised by the menu choices. It shows all the layouts associated with the table occurrence in the first pop-up menu, as you might expect. But it also shows layouts associated with any other occurrence of the same table. FileMaker uses the specified table occurrence to find the right related record, and the specified layout to show it to you. You can show an Invoice record from any layout that shows records from the Invoices table, no matter what occurrence it uses:

  • If the table occurrence you picked in the first menu is an occurrence of a table from a different file, you can turn on “Use external table’s layout” to see layouts in the file the table comes from. When you use this option, FileMaker switches to a window for the other file instead of showing records in the current window.

  • If you want a new window (whether you’re using an external table or not), turn on the “Show in new window” checkbox. When you do, you see the New Window Options dialog box, which is explained alongside the New Window script step on Section 14.6.

Finally, you get to decide how to deal with the found set when the script step finishes. See the box on Section 14.3.3.2 to help you decide when each option makes sense.

  • If you don’t turn on “Show only related records,” FileMaker goes to the related table, but you see all the records in the related table, not just those that are children of the formerly active record. The first related record is active in your new found set. So, from the Customer layout, GTRR without “Show only related records” shows all your invoices, and the active record is the first one for the Customer that you’re viewing when the GTRR script step runs.

  • If you turn on “Show only related records” and “Match current record only,” FileMaker returns a found set of only those records that match the active parent record. From the Customers Invoice, GTRR set as described above shows you a found set of just the Invoices for the Customer record that was active when the script step ran. The first related record would be the active one.

  • Choosing “Show only related records” and “Match all records in current found set,” is most useful when you have a found set selected before the GTRR script step runs. In this scenario, FileMaker shows a new found set in which all the records are related to at least one of the records in the old found set. So, in the Customers layout, you’ve found your two highest volume customers. Use GTRR, matching all records in the current found set to find all the invoices related to either of these two customers. The first record that’s related to the Customer record that’s active when the GTRR script step runs is the active record in the new found set.

Opening, Reverting, and Committing Records

When you use your database in Browse mode, FileMaker does a lot of things automatically. When you start typing in a field, it locks the record. When you exit the record—click outside any field or press Enter—it commits the record. When you use a script, though, you’re not really clicking fields and pressing Enter. So how does FileMaker know when to lock a record and when to commit? You have to tell it, by including the appropriate script steps: Open Record/Request, Revert Record/Request, and Commit Record/Request.

Open Record/Request

The Open Record/Request step simply tells FileMaker you’re about to start editing a record. If the record is already open—or locked—it does nothing. That is, it doesn’t automatically commit the record first. It just locks it if it isn’t locked already.

Commit Record/Request

Whether you’ve used the Open Record/Request step or just let FileMaker lock the record for you, you can explicitly commit the record with the Commit Record/Request step. It has two options:

  • The “Skip data entry validation” option tells FileMaker to commit the record even if it violates field validation. This option works only when you turn on the “Only during data entry” radio button in the Validation options for the field. If you have set the validation to happen “Always,” then the script can’t get around it.

  • When the “Perform without dialog” option is turned off, and you turn off “Save record changes automatically” in the Layouts → Layout Setup window’s General tab, FileMaker shows the message in Figure 14-6 when the step runs.

Tip

You know how to handle dialog boxes when they come up, but they often confuse and confound your users. Most database designers try to avoid requiring their users to interact with dialog boxes while scripts are running, especially when the user could make a choice that circumvents the purpose of their carefully crafted script.

Revert Record/Request

The Revert Record/Request step has only one option: “Perform without dialog.” When this option is turned off, the user sees the message in Figure 14-7. Otherwise, FileMaker reverts the record directly when the script runs.

If the layout is set not to save record changes (Section 4.4.3.1), and you don’t turn on the “Perform without dialog” checkbox you see this warning when the Commit Record/Request step runs. Click Save to commit the record. If you click Don’t Save, FileMaker reverts the record instead. The Cancel button leaves the record open and locked.
Figure 14-6. If the layout is set not to save record changes (Section 4.4.3.1), and you don’t turn on the “Perform without dialog” checkbox you see this warning when the Commit Record/Request step runs. Click Save to commit the record. If you click Don’t Save, FileMaker reverts the record instead. The Cancel button leaves the record open and locked.
Unless you turn on the “Perform without dialog” option, your users see this dialog box when the Revert Record/Request step runs. If a user clicks Cancel, FileMaker leaves the record open and locked, which is probably not your intention.
Figure 14-7. Unless you turn on the “Perform without dialog” option, your users see this dialog box when the Revert Record/Request step runs. If a user clicks Cancel, FileMaker leaves the record open and locked, which is probably not your intention.

These script steps are relatively easy to understand, but when to use them is hard to figure out. Here are some things to keep in mind when you’re trying to decide when you need to open or commit a record in a script:

  • When you use a script step that inserts data into a field (and leaves the user in the field), then FileMaker locks the record when the step runs, but doesn’t commit the change. You can then do more work with fields if you want. FileMaker commits the record later, when the user exits the record.

  • If your script changes to a different layout, switches to Find or Preview mode, or closes the window, FileMaker automatically commits the record if needed.

  • If you use a script step that modifies several records—Replace Field Contents, for example—FileMaker locks, edits, and commits each record in turn, as it goes.

  • If you perform a series of Set Field steps in a script, and you are not in the record when the script runs, FileMaker locks the record and makes the field changes. When the script is done, you’re not in the record (no field is active), but the record’s still locked and uncommitted. In other words, you can use the Records → Revert Record command to revert all the changes made by the script, which probably isn’t what you want to happen. Add a Commit Records/Requests script step at the end of the script to prevent the loss of data your script enters.

  • If your script changes some records and includes a step to revert them if something goes wrong, you should probably make sure to commit any changes your user was making before your script changes anything. That way, the script doesn’t undo any of the user’s work. Thus, put a Commit Records/Requests step at the beginning of your script.

And if you’re still not sure if your script really needs an Open or Commit step, go ahead and open the record at the beginning of your script and commit it at the end. Sure it takes a nanosecond or two extra to run a couple of steps that might not strictly be needed. But what’s a nanosecond on the grand scale of time when your data may be at risk?

Copying Records

FileMaker has two record-related script steps that do something you can’t easily do manually in Browse mode: Copy an entire record to the clipboard. One version copies just the current record, while the other copies every record in the found set at once.

Copy Record/Request

The first, called Copy Record/Request, copies data from every field on the layout and puts it on the clipboard. FileMaker puts a tab character between each field value.

If any field has more than one line, FileMaker converts the new line character into a funny character called a Vertical Tab. Some programs, like Microsoft Word, convert these characters back into new lines when you paste in a copied record. Figure 14-8 shows the result of pasting a Customer record into Microsoft Word.

The window in the back shows what it looks like when you copy a customer record, then paste it into a Word document. You see the first and last name twice because technically it appears on the layout twice (once in the header, and again in the editable fields). Although this block of text needs some serious cleanup, it’s often easier to copy/paste it all in one shot than to copy each field value individually. You can see a cleaned-up version in the front most window.
Figure 14-8. The window in the back shows what it looks like when you copy a customer record, then paste it into a Word document. You see the first and last name twice because technically it appears on the layout twice (once in the header, and again in the editable fields). Although this block of text needs some serious cleanup, it’s often easier to copy/paste it all in one shot than to copy each field value individually. You can see a cleaned-up version in the front most window.

Note

Some programs don’t recognize vertical tab characters, so when you paste into them, you see boxes, vertical lines, or nothing at all. Your multi-line field values are strung together in one long line.

Copy All Records/Requests

While Copy Record/Request copies the entire current record, its brother—Copy All Records/Requests—copies every record in the found set. Each individual record is added to the clipboard in the same format as the Copy Record/Request command produces, and FileMaker puts one record on each line.

Suppose, for example, you want to get all the Invoice IDs for a particular customer and put them in an email to a coworker. Or perhaps you’re compiling a list of all the zip codes in Phoenix where you have customers. Using Copy All Records/Requests, you can do this job with ease.

The trick is to create a new layout that has only one field on it. For example, make a new layout with just the Invoice ID field, or just the Zip Code field. Write a script that switches to this layout and run the Copy All Records/Requests command to get a simple list of values on the clipboard.

Working With Portals

FileMaker has a script step specifically designed to go to a certain portal row. Why would you want to do that? First, when you use the Go to Field script step to target a field in a portal, it goes to the first portal row, which may not have anything to do with what the user needs at that point. If you want to put the user in the field on a different row, you need to go to that row first.

When you use data from a related field, or put data in a related field, and there are multiple related records, FileMaker grabs the value form the first record. If you want to tell FileMaker to work with a different related record instead, use a portal and go to the right row. See the box below for tips (and a warning) on working with portals.

Go to Portal Row

The Go to Portal Row script step works a lot like the Go to Record/Request/Page step. You can go to the First, Last, Previous, or Next portal row, or specify the row number with a calculation. It even has an “Exit after last” option to help when looping through portal rows.

This step includes an option you can use to make what’s happening onscreen a little more obvious to your users—“Select entire contents.” When you turn this checkbox on, FileMaker highlights the whole portal row. Otherwise, it goes to the portal row without highlighting it onscreen. (After all, you don’t always want your user to see what’s going on.)

One setting you won’t find with Go to Portal Row is which portal to use. The guidelines below are how FileMaker knows which portal you have in mind:

  • If the current layout only has one portal, FileMaker uses it automatically.

  • If the layout has more than one portal, it assumes you want the portal that’s currently active.

  • If you’re writing a script and you want to be sure you go to a row in the right portal, just use Go to Field first, targeting a field that’s displayed in the portal.

Note

In general, a portal is active whenever a person (or a script) puts the cursor in one of its fields. If you aren’t sure what your user will be doing before your script runs, throw in a Go to Field script step, just to be safe.

Finding Records

FileMaker has three nearly identical script steps to handle the grunt work of finding records. You can let your user tell the script what to find, you can decide what the script finds (a hard-coded find), or you can script a dynamic find using calculations. Deciding which one to use depends on whether your users know what they’re looking for—or how much work you want to save them. The upcoming sections go into this topic in detail. You’ll also see a find script in action, and learn how to make a script pause and wait for information.

Performing the User’s Find Requests

The first, Perform Find, is the equivalent of a visit to Find mode, followed by a click of the Requests → Perform Find menu command. Perform Find’s single option lets you specify what find requests to use, but, surprisingly, you can skip it entirely. If you don’t turn this option on, then Perform Find assumes you’re already in Find mode with one or more requests, and works just like the Requests → Perform Find menu command (and the Find button in the status area). It looks for records that match the already defined find requests. All the matching records become the new found set.

But where do those find requests come from? Either the user creates them, or your script does. For example, many developers like to add special “Find” layouts to their databases. These layouts can show just the right fields, along with helpful text, to make things easier for the user. Figure 14-9 shows a Find layout for the Customers table.

This layout is designed specifically for Find mode. It shows people exactly what to do and provides nice big buttons to help them (the More button runs the New Record/Request step). Since this layout is for Find mode only, you need to make sure the user’s in Find mode when she comes here, and you need to take her to a different layout when she’s done. That calls for a script like the one shown in the next section.
Figure 14-9. This layout is designed specifically for Find mode. It shows people exactly what to do and provides nice big buttons to help them (the More button runs the New Record/Request step). Since this layout is for Find mode only, you need to make sure the user’s in Find mode when she comes here, and you need to take her to a different layout when she’s done. That calls for a script like the one shown in the next section.

The best way to deal with a layout like this is to add a “Find” button on your normal Customers layout. When the user clicks this button, it runs a script that goes to the Find layout and puts the user in Find mode. The script then pauses (see the box on Section 14.4.1), giving the user a chance to enter find requests. When she’s done, she clicks a button to Continue the script, which performs the find and switches back to the Customers layout.

To set this process up yourself, first create a Find Customers layout like the one shown in Figure 14-9. Then visit the Define Scripts window, click New, and name the script appropriately. Then follow these steps to define the script itself:

  1. Add the Go to Layout script step, targeting the Find Customers layout.

    After all, you want to make sure the user’s in the right context—the new Find Customers layout—before the script enters Find mode.

  2. Add the Enter Find Mode script step to the script.

    The step already has its Pause option turned on and its “Specify find requests” option turned off. That’s just what you want. This step switches to Find mode and pause the script.

    Tip

    If you want to start the user off with some basic criteria, you can specify them right in the Enter Find Mode step. FileMaker doesn’t perform the find now. It just puts the user in Find mode and creates the requests you specify. The user’s then free to modify or delete them as necessary.

  3. Add the Perform Find script step to the script.

    Once the script continues, you assume the user has added the necessary find requests, so you’re ready to use the Perform Find step with no find requests specified. (Be careful not to choose the Perform Find/Replace script step, which doesn’t work in this script.)

  4. Add another Go to Layout script step to the script. This time, pick the Customers layout.

    This step takes the user back to the Customers layout once FileMaker finds the correct records.

After you follow these steps, your completed Find Customer script should look like the one in Figure 14-10.

This is how your Find Customer script should look if you’ve been following the steps on Section 14.4. It first takes the user to the new Find Customers layout and switches to Find mode. Notice that the Enter Find Mode has its Pause option turned on—FileMaker automatically pauses the script after this step. When the user’s finished adding find requests and continues the script, FileMaker runs the Perform Find step (with no find requests specified, so it uses the ones the user created) and switches back to the Customers layout.
Figure 14-10. This is how your Find Customer script should look if you’ve been following the steps on Section 14.4. It first takes the user to the new Find Customers layout and switches to Find mode. Notice that the Enter Find Mode has its Pause option turned on—FileMaker automatically pauses the script after this step. When the user’s finished adding find requests and continues the script, FileMaker runs the Perform Find step (with no find requests specified, so it uses the ones the user created) and switches back to the Customers layout.

To complete your layout, you need the to put Find button on the Find Customers layout. Since the user’s always in the middle of a paused script when she sees this layout, you just need to wire this button to the Resume Script action.

Tip

It’s probably a good idea to hide the Find Customers layout from the Layouts pop-up menu so users don’t accidentally switch to it without running your script. In the Layout Setup dialog box, turn off the “Include in layouts menu” checkbox (Section 4.4.2.15).

Static Find Requests

With the script you created on the previous few pages, your users can search for just about anything they want, by entering find requests. More often than not, you don’t want to make users enter the find requests manually. After all, the whole point of a script is to have FileMaker do things so people don’t have to. If you know ahead of time exactly what you want the script to find, use the Perform Find step all by itself: Just turn on the “Specify find requests” option and put those requests right in the dialog box and the search options stay the same each time the script runs. When you specify find requests in a script, you see the window shown in Figure 14-12.

This window is pretty straightforward. To add a new find request, click New. To edit an existing request, select it first, and then click Edit (Figure 14-11). You can also delete or duplicate the selected step using the Delete and Duplicate buttons. Using the Specify Find Requests and Edit Find Request windows, you can tell the Perform Find step to do any find you can do from Find mode.

Tip

The Edit Find Requests dialog box is confusing until you get used to it. But there’s a way you can learn how it translates requests into its own particular syntax. Perform a find manually, then write a test script with a Perform Find script step. FileMaker sets the dialog box with the criteria for the search you just did.

When you add a new find request or edit an existing one, you see this dialog box. It’s just a more structured way to type data in fields in Find mode. Instead of using your layouts, you use this window, which gives you direct access to every field in the database (the Fields List) and makes it easy to see exactly what fields are being used in the Criteria List.
Figure 14-11. When you add a new find request or edit an existing one, you see this dialog box. It’s just a more structured way to type data in fields in Find mode. Instead of using your layouts, you use this window, which gives you direct access to every field in the database (the Fields List) and makes it easy to see exactly what fields are being used in the Criteria List.

To define a particular find request, you first select a field from the Field list (if it’s a repeating field, you can specify the repetition number in the Repetition box as well). Then you enter some text in the Criteria box. The Insert Symbol button gives you quick access to the same symbols you see in the status area in Find mode, and the Criteria box accepts all the standard symbols. Once you’ve finished entering the criteria, click Add to add it to the Criteria list.

  • To edit an existing item in the criteria list, first select it. When you do, FileMaker automatically selects the matching field in the Field list and puts the criterion in the Criteria box. You can then make any changes necessary, and click Change.

  • To remove a criterion from the list entirely, select it and click Remove.

  • Finally, you get to choose whether this request should be used to find matching records, or to omit them (see Section 2.2.1). To turn this into an omitting find request, choose Omit Records from the Action pop-up menu.

  • When you’re finished adding criteria, click OK. Just like Find mode, you can add more find requests if you want. In the Specify Find Requests window, just click the New button a second time. When you’re all finished adding requests, click OK again.

The Specify Find Requests window shows up when you tell FileMaker you want the Perform Find script step to perform a predetermined find. (Developers call this technique hard coding.) Right now the window’s empty because nobody’s added any requests. On your screen, it might not be empty; FileMaker automatically fills it with all the requests you used the last time you were in Find mode. If you have requests, select them all and click Delete.
Figure 14-12. The Specify Find Requests window shows up when you tell FileMaker you want the Perform Find script step to perform a predetermined find. (Developers call this technique hard coding.) Right now the window’s empty because nobody’s added any requests. On your screen, it might not be empty; FileMaker automatically fills it with all the requests you used the last time you were in Find mode. If you have requests, select them all and click Delete.

Dynamic Find Requests

As dependable as static finds are, you may not always be able to predict what the user wants to find. Or the criteria for finding the same thing over and over can change, like when you’re searching in a date field. For example, suppose you want to find all the invoices created one week ago. You can easily do so in Find mode: Just put the date from a week ago in the Invoices::Date field. But what you put in that field changes every day. For example, if today’s November 7, and you create a script to find invoices from a week ago, you could attach this request to the Perform Find step:

	Invoices::Date = "10/31/2005"

Unfortunately, as soon as November 8 rolls around, this script won’t find week-old invoices. It always finds invoices from October 31, 2005. When you’re faced with a situation like this, you do have an option: Make the script build the find request the same way you would in Find mode. In other words, tell your script to go to Find mode, have it put the right date in the field, and then perform the find. You can use the Set Field script step to put the date in the Invoices::Date field since it conveniently lets you use a calculation to determine the actual date.

The script would look like the one in Figure 14-13.

This script builds the find request itself since it needs to use a calculation to determine the proper date to search for. Notice that the second step uses the calculation Get(CurrentDate) - 7 to determine the date one week ago. The Perform Find step has no requests specified, so FileMaker uses the one the script created. This kind of find script, which changes as your data changes, is called a dynamic find.
Figure 14-13. This script builds the find request itself since it needs to use a calculation to determine the proper date to search for. Notice that the second step uses the calculation Get(CurrentDate) - 7 to determine the date one week ago. The Perform Find step has no requests specified, so FileMaker uses the one the script created. This kind of find script, which changes as your data changes, is called a dynamic find.

Note

If your find request can be handled using the symbols supported by Find mode (Section 2.2.1.3) then you can use Perform Find all by itself. For instance, to find invoices created today, tell Perform Find to find records with Invoices::Date = //. Since “//” stands for “today’s date,” it works on any day.

Constraining and Extending the Found Set

You may have already noticed that Perform Find doesn’t have an option for the Requests → Constrain Found Set and Requests → Extend Found Set commands. That’s because each of these is a separate script step. It makes sense really, because to Constrain or Extend a find, you have to have a find first. In other words, this process always takes two separate steps to complete.

The Extend Found Set and Constrain Found Set script step options work exactly like Perform Find. Everything you just learned about Perform Find still applies: You can hard-code the find requests, pause the script and let the user enter them, or build them in the script.

Omitting Records

The Omit Record script step lives a dual life. If you’re in Browse mode when it runs, it simply omits the current record from the found set. If you’re in Find mode, on the other hand, it turns on the Omit checkbox in the status area.

The Omit Multiple Records only works in Browse mode and does the same thing as the Records → Omit Multiple command. As usual, you can specify the number of records to omit in the script, either as a number or calculation. You also get a “Perform without dialog” option so you can decide whether or not the user gets to enter the number of records to omit.

Finally, the Show Omitted Only script step has the same effect as the Records → Show Omitted menu command.

Modify Last Find

The simplest find-related script step is Modify Last Find. It has exactly the same effect as the Records → Modify Last Find command: It puts you in Find mode with the same requests you created the last time you were in Find mode.

Save Records as…

Now that you’ve mastered the scripted find, you can keep right on going and automate the process of putting your found set to good use—outside of FileMaker. The Save/Send Records commands (covered in full in Chapter 17) let you export records into formats other programs can understand.

Save Records as Excel

As with its manual counterpart, your users will need export privileges (Section 16.2.1.1) to run the Save Records as Excel script step. (Actually, any user with full access privileges can do it.) You need to make the same choices in this script step as you do from the menu command, but the options are divided up amongst the various dialog boxes a little bit differently. The Save Records as Excel script step has three options:

  • Perform without dialog lets you control the step’s options without input from your users. Don’t turn on this option if you want users to be able to specify all the options they’d normally see in the Save Records as Excel dialog box.

  • The Specify output file option lets you determine a where the file is saved. You can click the Browse button to select a location through the Open dialog box or simply type a file path. You also have access to the Automatically open file and Create email with file as attachment options.

  • The Specify Options dialog is almost the same as the one you see when you chose this command manually and click the Options button. You can set a Worksheet name, document Title, Subject, and Author. Here’s also where you specify whether you want to save the Records being browsed or just the Current Record.

Note

It’d be great if you could suppress one of these dialog boxes and leave the other one open for business…but you can’t. If you tell the step to “Perform without dialog,” both dialog boxes get shut down. You can, however, set up the options you want, show your users the dialog boxes, and instruct them that they should change only certain items. (Use of the phrase “under pain of reprisal” while you deliver these instructions is entirely optional.)

Save Records as PDF

Like its popular Excel twin, the Save Records as PDF script step is the automated version of a menu-driven counterpart. It requires printing privileges (Section 16.2.1.1) or Full Access privileges to do its work. Similarly, you can control all the step’s myriad options by choosing the “Perform without dialog” option, or you can open the script up to input from your users. Check out the full instructions for “Save/Send as PDF” on Section 17.2.1.2 to see the ramifications of the full option set. The “Specify Options” button lets you choose output options appropriate to creating a PDF:

  • The Save pop-up menu lets you choose between Records being browsed and the Current Record.

  • The Options button produces a three-tabbed dialog box:

    • Document. Set the Title, Subject, Author, Keywords, Compatibility and page numbers here.

    • Security. Set passwords and access on this tab.

    • Initial View. Choose the setup your users see when the PDF is first opened.

Sorting Records

After all that fuss about finding records, the sorting script steps are refreshingly simple. There’re only two of them, and you already know how to use both. The Sort Records script step behaves in a now-familiar way. All by itself, it brings up the Sort dialog box when the script runs. If you turn on its “Specify sort order” option, you can preload the sort dialog box with a specific sort order. Finally, turn on the “Perform without dialog” option to sort the records without bothering the user with a dialog box at all.

If the records are already sorted, you can unsort them from a script. For instance, you might sort records for a report, but you want to return them to their unsorted order when the report is finished so your users don’t get confused. Just use the Unsort Records script step. It does its job with no options.

Working with Windows

Scripts give you complete control over the database windows on the screen. You can create new windows, close existing windows, bring any window to the front, and move or resize any window. (Why would a script need to monkey with someone’s onscreen windows? See the box on Section 14.6.3 for some ideas.)

Creating Windows

To make a new window on the screen, you use the New Window script step. With this step selected in your script, you see a Specify button in ScriptMaker’s Script Step Options area. Clicking this button brings up the New Window Options dialog box, pictured in Figure 14-14.

The first box—Window Name—gives your script control over the name of the window. (Window name is something you can control only from a script. When you create the window from the Window menu, FileMaker assigns it a name for you.) You can also tell FileMaker how big the window should be (Height and Width), and where to put the window on the screen (“Distance from top” and “Distance from left”).

Here are some tips on using the New Window Options dialog box:

  • If you leave any of the values blank, FileMaker uses the same value from the current window (it adds a number to the end of the window name so the new name is different). For example, if the current window’s called “My Database” and you run the New Window script step without specifying a name, the new window’s name is “My Database – 2.”

  • If you also don’t specify a size and position, FileMaker puts it right over the top of the current window (with the same size and position).

    Tip

    To avoid confusing your user, it’s usually best to offset the new window at least a little so he can see that there’s a new window on top of his old one.

  • You can directly set each of the values by typing in the box in the New Window Options dialog box or set them from a calculation by clicking the Specify button by any box. See the box on Section 14.6.3 for more detail.

The Window Options dialog box lets your script open and close windows and move them around the screen. You can use it to make family members think they have poltergeists in their PCs, or just make windows behave properly when your scripts run. See the box on Section 14.6.3 for advice.
Figure 14-14. The Window Options dialog box lets your script open and close windows and move them around the screen. You can use it to make family members think they have poltergeists in their PCs, or just make windows behave properly when your scripts run. See the box on Section 14.6.3 for advice.

Bringing a Window to the Front

In ScriptMaker parlance, you bring a window to the front by selecting it. As such, you use the Select Window script step. It has one option, which lets you specify a window by name, or select the current window, as shown in Figure 14-15. You also use the Select Window script step to show a hidden window and bring it to the front.

This is the window you see when you click the Specify button with a Select Window script step selected. From here, you can specify the Current Window, or choose Window Name and put the name of the window you want in the box. As usual, you can use a calculation to determine the window name as well.
Figure 14-15. This is the window you see when you click the Specify button with a Select Window script step selected. From here, you can specify the Current Window, or choose Window Name and put the name of the window you want in the box. As usual, you can use a calculation to determine the window name as well.

Note

Usually, the current window is already in the front, and you don’t have to worry about this step. But when you run scripts that involve more than one database, as you’ll learn in Chapter 15, you may need this step to bring forward the current window from another file.

Closing a Window

The Close Window script step has the same options as Select Window. You tell FileMaker which window to close: the current window, or one you specify by name.

Moving and Resizing Existing Windows

FileMaker has three ways to move and resize a window. You can opt for one of its canned window maneuvers or you can set the exact pixel size and location of the window just like you can with the New Window step. You can also hide the current window.

Adjust Window

The Adjust Window script step always operates on the current window, and it gives you just five simple choices:

  • Choose Resize to Fit, and FileMaker makes the window exactly the right size to fit its contents.

  • Choose Maximize to make the window as large as possible.

  • Choose Minimize to shrink the window to a little bar (Windows) or a Dock icon (Mac OS X).

  • Choose Restore to switch a window back to the last size it was, just before it was most recently resized.

  • Choose Hide to hide the window (just like the Window → Hide Window command).

Note

The Maximize options have slightly different behavior on Mac OS X and Windows. For example, when you maximize a window in Windows, and then select a different window, the second window also gets maximized. On Mac OS X, on the other hand, the second window keeps its original size. Also, a maximized window on Mac OS X fills as much of the screen as possible. On Windows, it fills FileMaker’s outer application window, whatever size it may be, and there’s no way to adjust the application window from a script.

Arrange All Windows

The Arrange All Windows script step is the equivalent of the four window arrangement options in the Window menu. You can choose to tile windows horizontally or vertically, or cascade them (see Section 1.7.2.1). On Mac OS X, you can also choose to bring all FileMaker windows to the front.

Move/Resize Window

For the ultimate in window control, call upon the Move/Resize Window script step. It can move and/or resize any window with pixel-perfect precision. Its Specify button shows the dialog box in Figure 14-16, where you can choose the window’s size and position. As with New Window, you can leave any of the size or position values empty. When you do, FileMaker leaves that part of the window’s size or position alone. For example, if you specify a new Width but leave Height blank, FileMaker makes the window wider or narrower, but its height doesn’t change.

The Move/Resize Window settings look like a combination of the Select Window settings and the New Window settings. First, you pick which window you want to work on (Current Window, or a window selected by name). You then specify the new size and position for the window.
Figure 14-16. The Move/Resize Window settings look like a combination of the Select Window settings and the New Window settings. First, you pick which window you want to work on (Current Window, or a window selected by name). You then specify the new size and position for the window.

Note

Move/Resize Window also selects the window it acts on, which always brings the window to the front, and, if it’s a hidden window, shows it.

Other Window-Related Script Steps

FileMaker has a handful of other window-related script steps, listed below. They come in handy if you need to exert more control over what your users see. (Not to imply that you’re a control freak or anything.)

  • Freeze Window tells FileMaker to stop showing changes in the window while the script runs. For example, if your script is looping through all the records in the found set, you normally see each record on the screen as it runs. If you add a Freeze Window script step before the loop, the user sees only the first record while it runs. When the script is finished, FileMaker updates the window again. Looping scripts that have to visit lots of records run decidedly faster when the window is frozen.

  • Refresh Window forces FileMaker to update what’s inside the window when it normally wouldn’t. This can be because you previously ran the Freeze Window step, or because FileMaker’s simply being conservative. If you want to make sure the user sees a particular record or filed value on the screen while a script is running, add a Refresh Window step after making the change.

  • Scroll Window lets you simulate a vertical scroll bar click in a window. You can scroll to the Home (top) or End of a window, or move up or down one screen-ful. You also get a To Selection option, which scrolls the window so that the selected record and field value both show. You’ll design most of your window so that scrolling isn’t necessary, which means you may never need this step.

  • Show/Hide Status Area lets you decide if the user should see the status area or not. You can explicitly show or hide the status area, or ask that it be toggled (shown if it’s hidden, and hidden if it’s showing). You can also turn on the “Lock” option to prevent the user from manually changing the status area. For example, if you never want the user to see the status area at all, Hide and Lock it in the script that runs when your database opens. Then the status area is locked, its toggle icon and menu command are grayed out. More commonly, though, the status area’s turned off and locked during a process, such as a scripted find. You don’t want your users using the Cancel button during a Pause step to cancel a script and then end up dumped on a layout that you meant them to see only while a script is running.

  • Show/Hide Text Ruler can toggle, show, or hide, the Text Ruler. Unlike Show/Hide Status Area, this step doesn’t have a Lock option. The user can always override your setting, so this step is rarely worth the trouble.

  • Set Window Title lets you change any window’s name. You can specify the current window, or any window name, as well as the window’s new name. FileMaker normally names a window with the file’s name, but you can tailor each window to your user. Write a script that runs when the file is opened and use the Set Window Title script step with this calculation: Get ( FileName & " " & Get ( AccountName ). See Section 16.2.3.7 to learn about Account Names.

  • Set Zoom Level sets the window zoom level, just like the zoom controls in the bottom-left corner of the window. You can pick a specific zoom level, or choose to zoom in or out to the next level. Again, you get a Lock option. If you set the zoom level and turn on the Lock checkbox, the user can’t manually change the zoom level.

  • View As step is in the Windows section of the script step list, but it isn’t really a window-related step. It changes the view option for the current layout. You can pick Form View, List View, or Table View. You also get a choice called Cycle that tells FileMaker to switch to the next view setting in the list. If you really want to control how your users see your database, use Layout → Layout Setting (View Tab) to turn off the views you don’t want them seeing. You can then let users override those settings with this script step.

Working with Files

The Files section in the Script Step list contains some of the least often used script steps in all of FileMaker. But if you work in a school, say, the day may come when you need to automate the process of formatting files or saving backup copies for every student in a class. You can also script the process of converting older databases to FileMaker 7/8 format and recovering damaged databases, but these are sensitive processes that are better handled manually.

Opening and Closing Files

The Open File script step lets you open another FileMaker file. You can pick any of your previously defined file references (see Section 8.5.1) or define a new file reference if necessary. When the step runs, the specified file opens and appears in a new window. If the file has a script set to run when it opens, it runs.

If you want the file to open, but you don’t want to see a window on the screen, turn on the “Open hidden” option for this step. The file opens, but it’s listed in the Show Window submenu of the Window menu, with its name in parentheses.

The Close File script step closes an already open file. When you add the step, you get to pick any file reference or add a new one. You can also choose Current File to close the file the script is in. If the file is open when the script runs, all its windows close and its closing script runs.

Note

In general, FileMaker is very smart about when to open and close files. It opens a file when it needs to and closes it again when it no longer needs it. You usually don’t need to open and close files from a script, but there’s one important exception—when the file has an opening script that should run before the user can see the file. If you jump directly to a related record in another file, FileMaker bypasses the opening script, so use an Open File script step to ensure that the open script runs.

Save a Copy As

If you have a database you don’t want to close, but you want to make a copy of it, you can do so with the Save a Copy As script step. It works just like the File → Save a Copy As command. You might want to script this command if you want to automatically back up several files at once without closing them.

Other File-Related Script Steps

The rest of the file-related script steps are almost never used…but that doesn’t mean you won’t find a good reason to use them.

  • Convert File lets you convert an older FileMaker database to a FileMaker 7 database. There is very little reason to do this from a script.

  • Set User System Formats toggles the Use System Formats file option on or off. When you first create a database, FileMaker remembers how your system expects dates, times, and numbers to be formatted. If someone opens the file on a computer with different settings (usually a different language), FileMaker has to decide if it should use the original format settings for the file, or those specified by the new system. You usually set this choice in the Edit → File Options window. Again, there’s not much reason to change this from a script.

  • Recover File runs FileMaker’s automatic file repair process on a selected database. Recovering a file is a rare thing in general since FileMaker is careful to avoid damaging databases even when your computer crashes. It’s even more rare to do this on the same database so often you’d need a script to do it for you.

Printing

Printing typically involves two specific commands on Windows or Mac OS X: File → Print Setup or File → Page Setup and File → Print. ScriptMaker sees it this way, too.

You use the Print Setup script step to set the options in the Print Setup or Page Setup dialog box. Turn on “Specify print setup” or “Specify page setup” to see the standard dialog box. You can pick the option you want to associate with the script step. When the script runs, FileMaker restores these same options. You can also turn on a “Perform without dialog” checkbox. When this option is off, the user sees—and gets a chance to configure—the dialog box when the script runs. If you just want to set the options without user intervention, turn “Perform without dialog” on.

The Print script step works very similarly. You can specify the exact print options (number of pages and so forth) or let the user make the choice. Either way, when the step runs, FileMaker prints the current record or found set.

Note

Many of the options you can set in the Print dialog box are specific to a particular printer model. For example, some printers let you pick color or black-and-white printing or have settings for different kinds of paper. If someone uses your database and script on a different printer, these options don’t work.

Other Script Steps

You’ve now seen most of the often-used script steps (and a few of the not-so-often used ones). You’ll see even more as they come up in the next few chapters. The rest of this chapter covers a few oddball steps that don’t seem to fit in anywhere:

Open URL

You first saw the Open URL script step as a button command when learning about text calculations. When FileMaker runs this step, it asks the your computer’s operating system to open the URL you specify. Most often the URL is a web address (HTTP), but it can be any URL type your computer supports, including FTP, MAILTO, SSH, and even FMP7 (to open a file on a network server). As usual, you can specify the URL or let the user do it, and you can use a calculation if needed.

Dial Phone

The Dial Phone script step tells your modem to dial the telephone using a phone number you provide. You can use a calculation to specify the phone number or enter it directly.

Note

This script step doesn’t work on Mac OS X.

Send Email

If you have an email program set up on your computer, FileMaker can use it to send email from a script. The Send Email script step has two options, the standard “Perform without dialog” checkbox and a Specify button. Clicking Specify shows the Send Mail Options window in Figure 14-17.

These are the settings for an outgoing email message. You get to tell FileMaker whom to send the message to (using To, CC, and BCC) and what the Subject and Body of the message should be. Finally, you can pick a single file to include as an attachment.
Figure 14-17. These are the settings for an outgoing email message. You get to tell FileMaker whom to send the message to (using To, CC, and BCC) and what the Subject and Body of the message should be. Finally, you can pick a single file to include as an attachment.

Although you probably wouldn’t guess it, you can use a calculation for each of the values in the Specify Mail dialog box. The little button to the right of each entry box shows a menu when clicked with two choices:

  • Specify Field Name.

  • Specify Calculation.

Whatever you specify for the To, CC, and BCC values, you can also select the associated “Get values from every record in found set” checkbox. When you turn any one of these boxes on, FileMaker creates one outgoing email message for each found record. Each message uses field data specific to one of the records. In this way, you can email all your customers in one shot, addressing each one individually, and including a custom-crafted email message for each.

The “Perform without dialog” option for this script step has an unexpected behavior. Instead of showing the user FileMaker’s Specify Mail dialog box when this option is turned off, FileMaker opens her email program, creates the message, and opens it onscreen. The user can then modify the message as needed and send it. If you turn “Perform without dialog” on, FileMaker puts the message directly in your email program’s outbox with no user interaction.

Flush Cache to Disk

Any time changes are made to a record, FileMaker records those changes in your computer’s memory first, and writes the change to the hard drive later—when it gets some free time. If you want to force the changes to be written to disk immediately, you can run the Flush Cache to Disk script step. For instance, you might add this script step after a script that creates a new customer order. That way, if your computer crashes while you’re busily taking orders, you lose only the last one you were working on.

Exit Application

Last but not least, you can have a script exit or quit the FileMaker application on your behalf. Of course this causes every open window and file to be closed as well. People usually use this when they have just one database, and they never use FileMaker unless they’re in that database. They add the Exit Application step to the very end of their database’s closing script so that when they close the last database window FileMaker goes away too. (Of course you find FileMaker so useful you never want to close it, right?)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.59