Automating tasks with scripts > Script examples
 
Script examples
You can use script examples provided in Help as starting points for your own scripts. Several scripts are included in the FileMaker Pro Starter Solutions as well.
To view a script in FileMaker Pro:
1. Choose Scripts menu > Script Workspace. Or, choose File menu > Manage > Scripts.
2. In the scripts pane, double-click the script you want to view.
The script opens in a new tab in the script editing pane.
If structure examples
If, Else If, Else, and End If script steps define a structure that controls whether or not script steps are performed. This control depends upon the result of a testable condition or Boolean calculation.
When the calculation result is any number except zero, the condition evaluates to True, and subsequent script steps are performed.
When the calculation result is zero, blank, or content that does not resolve into a number, then the condition evaluates to False and the subsequent script steps are not performed.
Else If steps provide additional Boolean tests. Else steps provide alternative steps to perform if all conditions evaluate to False.
Example 1
Performs a find. If no records are found, displays a custom dialog. If records are found, sorts the found set.
Perform Find [Restore]
If [Get ( FoundCount ) = 0]
Show Custom Dialog ["Find Records"; "No records were found."]
Else
Sort Records [Restore; With dialog: Off]
End If
Example 2
Performs a find. If no records are found, displays a custom dialog. If one record is found, goes to the Invoice Details layout. If more than one record is found, goes to the Invoices layout.
Perform Find [Restore]
If [Get (FoundCount) = 0]
Show Custom Dialog ["Find Records"; "No record was found."]
Else If [Get (FoundCount) = 1]
Go to Layout ["Invoice Details"]
Else
Go to Layout ["Invoices"]
End If
Loop structure examples
Loop, Exit Loop If, and End Loop script steps define a structure that enables script steps to be repeated. Script steps between a Loop and an End Loop are executed continuously, until an Exit Loop If condition or an Exit After Last condition is reached for a Go to Record/Request/Page or Go to Portal step.
Use the Exit Loop If script step to specify a calculation to be evaluated. When the calculation result is not zero, it evaluates to True and the loop ends. When the calculation result is zero, it evaluates to False and the loop continues.
Example 1
Copies the contents of the Customers::Work Phone to Customer::Day Contact in all records.
Go to Record/Request/Page [First]
Loop
Set Field [Customers::Day Contact; Customers::Work Phone]
Go to Record/Request/Page [Next; Exit after last]
End Loop
Example 2
Loops through records to export files that are in container fields. Exits the loop if a record has an empty Container field.
Set Variable [$PATH; Value: Get ( DocumentsPath ) & Products::Container]
Go to Record/Request/Page [First]
Loop
Exit Loop If [IsEmpty ( Products::Container )]
Export Field Contents [Products::Container; "$PATH"]
Go to Record/Request/Page [Next; Exit after last]
End Loop
Startup script examples
A startup script can customize a user's view of a database or perform other actions when a database opens. Startup scripts are triggered by the OnFirstWindowOpen script trigger. For information on setting up a startup script, see Setting file options.
Example 1
Goes to the Administration layout if the account is assigned the Full Access privilege set. Otherwise, goes to the Data Entry layout.
If [Get ( AccountPrivilegeSetName ) = "[Full Access]"]
Go to Layout ["Administration"]
Else
Go to Layout ["Data Entry"]
End If
Example 2
Checks which version of FileMaker Pro or FileMaker Go opened the database and goes to the appropriate Customers layout.
If [Get ( Device ) = 3]
Go to Layout ["Customers iPad"]
Else If [Get ( Device ) = 4]
Go to Layout ["Customers iPhone"]
Else
Go to Layout ["Customers"]
End If
Perform Script and script parameter examples
Perform Script performs a script that is defined in the current file or in another FileMaker Pro file.
Optional script parameters can pass text into a script. For example, you can use a script parameter to store the active record number when a script is initiated, making it easy to return to that record at the end of the script. Or you can call the same script from different buttons on the same layout, and easily determine which button called the script by using a different script parameter for each button.
When you specify a parameter, you can access it within a script or pass it to other scripts using the Get(ScriptParameter) function.
Complex parameters, such as a list of names or other values, can also be used. Complex parameters that are separated by carriage returns can be parsed using the LeftValues function, MiddleValues function, and RightValues function. These functions return the beginning, middle, and ending values from lists that are separated by carriage returns. Complex parameters separated by other characters can be parsed as text using functions such as Left function, Middle function, and Right function.
Example 1
Runs the "Print Invoice Report" script with no parameters.
Go to Layout ["Invoice Report"]
Perform Script ["Print Invoice Report"]
Example 2
Uses a field, Customer Name, as the parameter. Invoices for the current customer are returned in a new window with the Invoice Report layout.
Main script: Current Customer Invoices
Find Matching Records [Replace; Invoices::Customer ID]
#Calls the "View Customer Invoices" sub-script defined below
Perform Script ["View Customer Invoices"; Parameter: Invoices::Customer Name]
Sub-script: View Customer Invoices
New Window [Name: "Customer: " & Get ( ScriptParameter ); Style: Document]
Go to Layout ["Invoice Report"]
Sort Records [Restore; With dialog: Off]
Notes
A script parameter exists only for the duration of the script. Script parameters are reset each time a script is performed. If you want a script parameter to persist while a file is open, you can use a global variable as the script parameter.
A script parameter exists within the parent script only, unless it is explicitly passed to another script using the Get(ScriptParameter) function.
A script parameter can be used (but not modified) within a script and can be passed along to sub-scripts by using the Get(ScriptParameter) function as the parameter for the sub-script. You can also specify different parameters each time the sub-script is called using Perform Script. Changing the parameters passed to a sub-script does not modify the value of the parameters returned from Get(ScriptParameter) in the parent script.