Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 91 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
91
Dung lượng
1,24 MB
Nội dung
431 : Running Scripts • When you exit a field that has Field Validation options. • When you press a key on the keyboard while in a field, or while a button or tab is selected. • When you change the contents of a field in any way. • When you switch from one tab panel to another. • When you leave a field or tab away from a button or tab panel. • When you leave a field after you’ve made changes to it (in other words, when FileMaker has to save changes to the field). You’ll learn how to use script triggers and more about how they behave at the end of this chapter. Performing Scripts When a File Is Opened and Closed You can configure a script to run each time the database is opened or closed. (These opening and closing scripts are a lot like script triggers, but FileMaker doesn’t call them that.) The opening script runs when you first open the file. You can use this script if you want to be greeted with the list of unpaid invoices first thing every morning. The closing script runs when you close the last window for an open file. This option is a little less common, but it has its uses: If you want to make sure other related files close whenever the main file closes (even if they have open windows), then you can write a script to close them all whenever you close the main file. UP TO SPEED A Script Action or a Script? Attaching a single script action to a button is so easy that it’s tempting to use that technique often (page 318). But even if your buttons do only one thing, you’ll still save time in the long run if you create a single-line script, and attach that to your button instead. Yes, it’s a little more work upfront, but scripts have a lot more flexibility over the long haul. Here’s why. Lots of the initial development work in a database has to do with helping the users get around. So, many of the first buttons you create just go to a layout, or to a set of related records. But what if your business rules change—like from now on, only managers can see a customer’s payment history? You have to figure out how to stop unauthorized folks from seeing payment data. With an If statement (page 436) that checks Get (PrivilegeSetName), a script can see who’s logged in to the file before it allows the “Go to Lay- out” to run. So you have to write the script, and then find all the 14 places you created a button with a plain-vanilla “Go to Layout” script action. If you’d written a “Go to Lay- out” script, and attached that to your 14 buttons, you could handle this new wrinkle just by editing the script. No time spent finding all your “Go to Layout” buttons, or worrying about what happens if you missed one, because the script handles it all. 432 FM P : T M M Running Scripts To make the selection, you need to visit the File Options window (File➝File Options). In the File Options window, make sure the Open/Close tab is selected. In this window, you first turn on one of the “Perform script” checkboxes (there’s one under “When opening this file” and another under “When closing this file”). Then you can select one of your scripts from the associated pop-up menu. Figure 10-12 shows the result. Figure 10-12: The Open/Close tab of the File Options window (File➝File Options) lets you tell FileMaker what to do when someone opens or closes a file. Most of this window is devoted to the things that happen when a file opens. Only the last checkbox (under “When closing this file”) has to do with which script should run when the file closes. Timer Scripts Finally, using the Install OnTimer Script script step, you can tell FileMaker to run a particular script periodically. For instance, you might set up a special computer that runs a script every 10 minutes to grab order information from your website. Or perhaps when someone switches to a sensitive layout, you want a timer to automati- cally switch to some place more innocuous after 5 minutes, in case he walks away from the computer. Timer scripts are notoriously tricky, so be prepared to experiment and test thor- oughly. Timer scripts are definitely not for the faint of heart. The process of setting one up, however, is simple. Just write a script that uses the “Install OnTimer Script” script step. When you configure this step, you specify a script, and then provide an interval, which is how often the script runs. You express the interval as a number of seconds using a calculation. 433 : Organizing Your Scripts Organizing Your Scripts Most mature databases end up with dozens, or even hundreds, of scripts that make life a lot easier for the folks who use it. But developers don’t usually have the luxury of creating scripts in an order that makes sense for display in the Manage Scripts window. That’s why FileMaker gives you a suite of tools you can use to organize your scripts. Creating Script Folders In Figure 10-13, you can see the Manage Scripts window from a database with a lot of scripts. The window looks a little like a window on your operating system, where documents are organized in folders. FileMaker lets you create folders for organizing your scripts that same way. Not only can you give a new folder a descriptive name, but also, like folders on your desktop, you can collapse them, so you don’t have to scan a lengthy list of scripts to find those under Reports. Figure 10-13: When your database has lots of scripts, like this one, organization becomes critical. Here, the scripts are arranged in a logical order, and grouped into folders, as described in the steps starting on the next page. Some folders are open, and others are closed. Most scripts aren’t set to show in the Scripts menu because this solution has buttons for users to run the scripts they need. Script folders make the Manage Scripts window nice and tidy, but they also organize the Scripts menu. Any scripts inside a folder appear in a hierarchical (or pop-out) menu when you click the Scripts menu. 434 FM P : T M M Organizing Your Scripts To see how all this works, you need to create a few extra scripts in your sample file. But they don’t have to be real, working scripts. Just select your Find Unpaid Invoices script, and then, in the Manage Scripts window, click the Duplicate button a few times. 1. In the Manage Scripts window, select the top script in your list, and then, at the right edge of the New button, press the little arrow that’s beside the New button (see Figure 10-14). The New item pop-up menu appears. Figure 10-14: In the Manage Scripts window, the New button makes a new script. But if you click the little arrow, then you get a pop-up menu of additional choices. You can make a new folder instead, or add a separator to the script list. New Script Tiny triangle 2. Choose New Folder. The Edit Folder dialog box appears. 3. In the Folder Name box, type Reports. Just like everything else in FileMaker, a descriptive name helps you figure out what’s what. “Reports” is a little arbitrary, since this exercise is theoretical, but it’s still a good habit to use descriptive names. 4. Click OK. The Reports folder appears as the second item in your list. A new group always appears just below any selected item in the list. If you forget to select a script and the new folder lands all the way at the bottom of the list, you can move it into place by dragging the double-headed arrow to the left of the folder’s name. 5. Drag the double arrow to the left of a script to move it into the Reports folder. The motion can be a little twitchy until you get used to it. Drag straight up or down to move a script to a new position in the list. But drag toward the right to move a script into a group. If the new folder is directly above the scripts you’re moving, then it’s easiest to move the first script under the folder right, and then move each successive one right, also. Tip: On the Mac, a blue line appears to help you see where a dragged item will land and folders highlight when you drag over them. Let go of the mouse button while the folder is highlighted and the script you’re moving will land inside the folder. 435 : Branching and Looping in Scripts Move a few scripts into the Reports group to get the hang of the technique. When you have some scripts in the new group, to the left of the group name, click the gray triangle (Mac) to collapse it. A second click opens the group again. (Windows shows a +/- icon to expand and collapse a folder.) Finally, click the Scripts menu to see how script groups work there. Creating Menu Separators Groups help you when you’re plowing through a list of scripts trying to find the one you need to tweak. But you can also help people who use your database by giving them menu separators. It’s a good idea to use them to organize sets of scripts that do different things. To create a menu separator, click to select the script that’s just above where you want the separator to appear, and then, from the New item pop-up menu, choose New Separator. If a separator isn’t where you want it, then you can drag it into place. (On the Mac, you can click anywhere on the separator, and then drag it, but on Windows, make sure to click the double-arrow, and then drag). You can also use the Duplicate button to copy a whole bunch of separators with just a few clicks. Each new separator appears just below the original; just drag them into place. A separator is really just an empty script whose name is “–”, so if you like to do things the long way, you can make one manually. In Figure 10-15, you can see how a separa- tor looks in a menu. Branching and Looping in Scripts Now that you have a basic foundation in what scripts do and how you can run them, it’s time to see some of scripting’s more powerful features. The script you created at the beginning of this chapter was the simplest kind: It goes through a series of steps from start to finish every time. Sometimes your script needs more smarts. You can add steps to the script that cause it to take different actions depending on the situ- ation (called branching), or make it do the same thing over and over again (called looping). Branching with If, Else If, Else, and End If Sometimes a script needs to take action based on certain conditions in your da- tabase. When you send an email to a customer manually, you look him up in the Customers layout, copy his email address, and then go to your mail program to cre- ate, address and send the email. You’d like to add a button to the Customers layout that creates the email directly, saving you all the trouble of copying, switching, and pasting. 436 FM P : T M M Branching and Looping in Scripts Figure 10-15: In the Manage Scripts window, separators just show up as scripts named “–” but everywhere else, they turn into proper visual separators. Here, in the Specify Script window, the separator is right under the “Buy Cappucino machine for office” script. A one-line script using the Send Mail script step (which is just like the File➝Send Mail command) will do the trick. It’s already created for you in the Invoices sample file, or see Figure 10-16 for the settings you need. If you write the script from scratch don’t forget to select the “Perform without dialog” option or to save the script. If you run the Send Email to Customer script, it creates a new email addressed to the current customer. But what happens if you don’t have an email address for this customer? If the Customers::Email Address field is empty, then your script tries to send an email without a valid address through your email program, which com- plains mightily. Fortunately, you can head off this problem at the pass. If the Email Address field is empty, the script should let you know, and then skip the Send Mail step entirely. The If step is the answer; it tests a condition and can take action based on the result of its test. Here’s how to add an If step to the Send Email to Customer script: 1. In the Edit Script dialog box, add the If step to your script. FileMaker adds this step after the Send Mail step. It also adds a third step: End If. You can’t have an If without an End If, so you get both automatically. 437 : Branching and Looping in Scripts Figure 10-16: The Send Mail Options lets you type an ordinary email address directly in the To, CC, or BCC boxes, and enter a Subject, and Message. Or, click the triangle by any box and either pick a field or enter a calculation instead. That way, you can send email to a person in the database, or fill the message with details from the current record. Turn on the “Multiple emails (one for each record in found set)” checkbox to use this script step to send email to more than one person at a time. But remember, this option must be used for good, never for evil. You’d never send spam emails, would you? See the box on page 439 to learn about the Send Via options. Note: If you accidentally delete the End If step from your script, then FileMaker shows an error message when you try to save the script. To fix the error, you need to add the End If step back to your script, and then drag it to its proper place. 2. Using the double-pointed arrow to the left of the If step, drag the step up above the Send Mail step. By rearranging the steps in your script, you’re telling FileMaker what order they should run in. (A script doesn’t work as intended if you’ve got a step above something that needs to happen first.) Your script should look like Figure 10-17. 3. Select the If step and, in the Script Step Options area, click Specify. FileMaker shows you a standard Specify Calculation window. You use a calcula- tion to define the condition of this If step. If the calculation evaluates to True, then FileMaker does the steps after the If. If the calculation evaluates to False, then FileMaker skips to the End If, and continues running the script from there. 438 FM P : T M M Branching and Looping in Scripts Figure 10-17: To prevent FileMaker from making an email message when the customer has no email address, wrap the Send Mail step inside an If block as shown. Next, you’ll put a condition in that If step to tell FileMaker when the message should be sent. Notice that the Send Mail step is now indented. Every step inside an If block gets indented this way. 4. In the calculation box, enter Not IsEmpty(Customers::Email Address), and then click OK. This calculation evaluates to True, and sends your customer an email only if the Email Address field isn’t empty. See the box on page 441 for details on how this calculation makes these decisions. Your script now checks to see if the Email Address field has something in it before running off to create the email message. But what happens when you run the script and the Email Address field is empty? Right now, nothing at all. FileMaker evalu- ates the If condition, sees that it’s False, and skips to the End If. There’s nothing after the End If, so the script just stops and your user waits in suspense, until he finally realizes that the requested email message simply isn’t coming and investigates the problem on his own—or chucks his computer out the window. In the interest of preventing property damage, your script should tell him why noth- ing’s happening. For example, you can have your script show a message box saying, “You can’t email this customer, since there’s no email address on file”, or whatever. That’s what the Else step is for: 1. In your script, select the Send Mail script step. When you add a new step, FileMaker inserts it after the selected step. You want the Else step to go right after the Send Mail step, so you select that step first. 2. Add the Else step to the script. FileMaker inserts an Else step between Send Mail and End If. 439 : Branching and Looping in Scripts POWER USERS’ CLINIC Two Ways to Send Mail The first option in the Send Mail Options window (see Figure 10-16) is “Send via”, and it gives you two options, letting you either use or bypass the email program on your computer: • Choose E-mail Client to send the message using your email program. If you turn on “Perform without dialog” for the Send Mail script step, then the mes- sage goes directly into your mail program’s outbox, and that program sends it the next time the send/ receive mail process runs (which is automatic in most mail programs). If you leave “Perform without dia- log” off, then the message goes into the Drafts folder instead. FileMaker also opens it up and switches to your mail program. This way, you get a chance to double-check the message and edit it as needed be- fore you send it. • Choose SMTP Server to send the message through your mail server. In other words, you’re asking File- Maker to bypass your email program, and go straight to the network post office. You have to put in infor- mation about your mail server, including its network address and any user name and password it requires. This configuration has no “Perform without dialog” option, so you can’t edit the message before File- Maker sends it off, and it doesn’t show up in your sent mail folder. Under most circumstances, you’re probably best off sticking with the E-mail Client option. You can use it more easily (because you don’t need to put in mail server settings). It’s also more flexible since you can edit the message. Finally, you get a record of the emails you’ve sent from your sent mail folder. But sometimes the E-mail Client option isn’t a good choice. Sometimes you have a script that runs on the FileMaker Server (you’ll learn about that in Chapter 17) or on a shared computer that has no email client. In a case like that, you’re better off going straight to the server so you don’t have to set up and manage a mail program. Also, when you use the SMTP Server option, you get to specify the From name and address. If the message comes from your email client, then it comes from you. But what if you’re sending shipment notifications for your company? You may want the messages to come from orders@my- company.com, or some other shared address instead. In a case like this, you can use the SMTP Server option, and everybody in the office can send email from the same ac- count through FileMaker. Finally, if you’re sending lots of email, it can be faster and more reliable to send messages directly to the mail server (after all, its job is to send lots of email). As a general rule, it’s fine to use the E-mail Client option unless you need for the increased power (and hassle) of the SMTP Server option. Note: Don’t click the Else If step by mistake. You want the step called just Else. If you added the wrong step, select it, click Clear, and then try, try again. 3. Right after the Else step, add the Show Custom Dialog script step to the script. This step is also under Miscellaneous. Its job is to pop up a dialog box for some- one. You get to decide what the box says, which buttons it includes, and which fields—if any—it should show. 440 FM P : T M M Branching and Looping in Scripts 4. In the Script Step Options area, click the Specify button. The “Show Custom Dialog” Options window appears (Figure 10-18). Figure 10-18: The Show Custom Dialog script step lets you provide feedback, like why an email script isn’t behaving as expected. You can give your dialog box a title, a message, and up to three buttons for people to click. You can also add input fields to the dialog box by visiting the Input Fields tab. You’ll learn about these on page 678. 5. In the Title box, type No Email Address. Whatever you type in this box appears in the title bar along the top of your cus- tom dialog box. Punctuation marks like commas and periods look odd in title bars, but you’re welcome to include them. 6. In the Message box, type You can’t email this customer because there’s no email address on file. This tells FileMaker what to put inside the dialog box. 7. From the Button 2 box, select, and then delete the word Cancel. A custom dialog box can have up to three buttons. In this case, you want only one: an OK button. (If you don’t type anything in the Button 2 and Button 3 boxes, then those buttons don’t show up.) 8. Click OK. Your script should now look like the one shown in Figure 10-19. [...]... contents”, then FileMaker inserts the result of the calculation into the current field If you select data when the script step runs, then 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 464 FileMaker Pro 11: The Missing Manual Scripting for Fields Inserting other values FileMaker. .. “Link if available” (it’s there, but greyed out on the Mac) If the data on the clipboard comes from a source that supports object linking, then FileMaker embeds the linked object Changes to the original data show up in FileMaker Turn this option off if you want the script to just paste a copy of the data 460 FileMaker Pro 11: The Missing Manual Scripting for Fields Selecting Text FileMaker has two script... into the page, and you then click away Any script step that causes an object to be exited will fire the trigger 452 FileMaker Pro 11: The Missing Manual Script Triggers Many actions can trigger an exit from a field, portal, or web viewer For instance, you might leave the layout, close the window, or quit FileMaker In every case, the trigger fires, and in every case, if the script returns False, then the. .. 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 has to 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 462 FileMaker Pro 11: The Missing Manual. .. 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 has to be text, and it should exactly match the name of one of the layouts in the database When the script runs, FileMaker evaluates the calculation, and then switches to the layout with the correct name • The. .. is to apply the proper object trigger 450 FileMaker Pro 11: The Missing Manual Script Triggers Tip: When you’re trying to tease these triggers apart, it’s helpful to write a very simple script that does nothing but the Beep script step Assign an object trigger that runs the script to a field, and then interact with the field, noticing when the beep occurs Try each trigger—you’ll soon get the picture... your task on the current record, and then moves to the next record (just as if you’d clicked the book icon’s right-hand page) If the number of the record you’re now on equals the current found count, then FileMaker exits the loop This type of logic often requires you to repeat the steps within a loop right after it in order to get the process to run on the last record in your found set But FileMaker has... name with the Inspector (Figure 11- 1) This name is in addition to any other name or identifier it has, such as a fully-qualified field name (Invoices::InvoiceID or Customer::Full Name) or any internal FileMaker IDs You can name any object on a layout, but there’s usually no reason to unless you need to use the name in a script 4 56 FileMaker Pro 11: The Missing Manual Go to Object Figure 11- 1: Name... show in the menu (you can turn off their display—see page 282), plus you can reorder them manually To find out a layout’s number reliably, you have several choices The manual method is to switch to Layout mode, and then go to the layout in question In the Status toolbar look at the Layout box It shows which layout number you’re on Better yet, use the Data Viewer in FileMaker Pro Advanced See the box... on, then FileMaker selects the entire field before acting If this option’s turned off, then FileMaker cuts/copies/clears whatever happens to be selected in the field (But see page 462 to learn why you should rarely use these commands.) 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, then it uses the current field—the . step. If the calculation evaluates to True, then FileMaker does the steps after the If. If the calculation evaluates to False, then FileMaker skips to the End If, and continues running the script. fix the error, you need to add the End If step back to your script, and then drag it to its proper place. 2. Using the double-pointed arrow to the left of the If step, drag the step up above the. in, on the other hand, then the calculation’s result is False, and FileMaker skips the Send Mail step. This behavior is exactly the opposite of what you want. To flip the result of the calculation