4.1 Basics of scripting in FileMaker
Scripting is one of FileMaker's most powerful features, allowing you to automate repetitive tasks, create complex workflows, and improve the user experience in your database application. In this tutorial, we'll look at the basics of scripting in FileMaker, including loops, conditions and variables, and learn how to create a simple script.
Table of contents
- 4.1 Basics of scripting in FileMaker
- Introduction to FileMaker Scripts
- gFM-Business Open Source FileMaker Basis ERP The software for a crash course
- Scripting basics: loops, conditions, variables
- Creating a simple script
- Script structure in gFM-Business
- FileMaker ERP with clear and fully documented script structure
- Frequently asked questions about FileMaker scripts
Introduction to FileMaker Scripts
What are FileMaker Scripts?
FileMaker Scripts are sequences of instructions that are executed automatically to complete certain tasks in your database. They allow you to automate manual processes, perform complex calculations or optimize the user experience. With scripts, you can automate almost any task in FileMaker, from simply saving a record to executing complex, multi-step workflows.
Why scripting?
Scripting allows you to:
- Save time: Automate recurring tasks.
- Reduce errors: Scripts ensure consistent and error-free processes.
- Increase efficiency: Create workflows that guide users through your application faster and more effectively.
Where and how do I write scripts?
To open the Script Editor in FileMaker, select [Scripts > Script Workspace] in the FileMaker menu bar, whereupon the corresponding window opens. In the script workspace, you will find all the scripts available in your solution in the left-hand column. The right-hand column shows a list of all available commands, which you can filter using categories and search terms. Your script is located in the central main area.
gFM-Business Open Source FileMaker Basis-ERP
The software for the crash course
Download for free
Scripting basics: loops, conditions, variables
Loops in scripts
Loops allow you to execute a group of statements repeatedly. In FileMaker, you can use a loop to navigate through records, for example, to apply a specific calculation to each record or to check all records in sequence.
- Example: With a
Loopyou can scroll through all data records in a layout and update a specific field in each data record.
Loop
Set field value [Table::Field; Value]Go to query/record [Next; Exit after last]
End of loop
Conditions
Conditions allow you to execute statements only when certain criteria are met. FileMaker uses If-then-else-conditions to make decisions based on the data.
- Example: A
If-command can be used to only process data records that meet certain criteria, such as checking whether a field is empty.
If [Table::Field = ""] Set field value [Table::Field; "Default value"]Otherwise Show own dialog box ["The field is already filled in"]End IfVariables
Variables are storage locations for data that can be used during the execution of a script. You can use global or local variables to save and reuse values between the steps of a script. A script variable is identified with a dollar sign as a prefix and is only valid in the respective script. If the variable is prefixed with two dollar signs ($$Variable), the value is global and can be used in the entire FileMaker file.
- Example: With
Set variableyou can save a value and use it again later in the script.
Set variable [$Value; Value: Table::Field]Creating a simple script
Step 1: Open script workspace
To create a script, open the script workspace via Scripts > Script Workspace..
Step 2: Create new script
Click on "New script" and give the script a meaningful name, e.g. "Save and confirm record".
Step 3: Add script steps
Add the desired script steps. For example, you could create a script that saves the current data record and displays a confirmation.
Set field value [Table::Field; "New value"]Save data record/queryDisplay own dialog box ["The data record was saved successfully."].
Step 4: Test script
After you have created the script, test it to ensure that it works as desired. To do this, go to Browse mode and execute the script.
Step 5: Customize and extend the script
If the script works as expected, you can extend or customize it as needed to perform more complex tasks. For example, you could add conditions to execute the script only under certain circumstances.
Script structure in gFM-Business
gFM-Business contains a number of scripts to ensure the functionality of the ERP software. All scripts in gFM-Business are structured as follows:
- Layout scripts (lay.)Each screen layout in gFM-Business has exactly one layout script in which all functions of the respective layout are queried. For example, the layout "scr.Faktura_Rechnungen" is controlled via the layout script "lay.Faktura_Rechnungen". All functions, such as "New invoice" or "Duplicate invoice", are transferred to the script with the respective function as a script parameter, where they are queried and executed.
- Partial scripts (ts.)Partial scripts are, as the name suggests, not complete scripts, but scripts that only execute certain parts or can be called generically via parameters. Partial scripts are either assigned to a module (e.g. "ts.fak_invoice_duplicate") or can be called generically without a module abbreviation as a prefix.
- REST API scripts (REST.)These scripts contain an interface script for a third-party system that contains all the functions of the interface for this system and can be controlled via parameters.
- System scripts (sys.)System scripts are executed context-independently on the system, such as the start script "sys.Start"
FileMaker ERP with clear and fully documented script structure
More information
Frequently asked questions about FileMaker scripts
- What are FileMaker scripts and how do they help with automation?
- FileMaker scripts are a series of commands that automate tasks. They help to make recurring processes such as creating records, performing calculations or navigating between layouts more efficient. Scripts allow you to control workflows so that users have to perform fewer manual steps and errors are reduced.
- How do I create a simple script in FileMaker?
- To create a script, open the script editor via "Script" > "Script workspace". Click on "New script" and give it a name. You can now add commands such as "New record", "Go to layout" or "Sort records". Once the script is ready, you can save and execute it. Scripts can be easily customized to automate various tasks within your FileMaker solution.
- What are loops in FileMaker scripts and how are they used?
- A loop is a script command that repeats a series of actions until a certain condition is met. You add "Loop" and "Exit loop" to the script, and between these commands you specify the actions to be repeated. For example, a loop could be used to navigate through a list of records and update them until all records have been processed.
- How do I use conditions in FileMaker scripts?
- You can use conditions to control the flow of a script by only executing certain actions if certain criteria are met. The "If/else" command allows you to check whether a condition is true and execute different actions based on this. For example, you can create a script that only creates a new data record if a certain field is empty.
- How do I use variables in FileMaker scripts?
- Variables are temporary storage locations in which you can save values such as text, numbers or the results of calculations. They can be defined in the script with the "Set variable" command. There are two types of variables: Local variables (start with $) and global variables (start with $$). Local variables are only available while the script is running, while global variables are retained even after the script has been completed.
- How can I create a script that processes multiple data sets?
- You can create a script that edits multiple data records by using a loop. In the loop, you can insert the "Go to next record" function and then make the desired changes for each record. The script then runs through each data record until all have been processed. Use the "End loop" function to ensure that the loop ends when the last data record is reached.
- How can I integrate user interactions into a script, e.g. through dialogs?
- You can build user interactions into scripts by using custom dialog boxes. The "Show custom dialog box" command opens a window in which the user can enter information or make a selection. This is useful if the script requires input before it continues, for example, if a user needs to enter the name of a new customer before the record is created.
- If you want to create more complex dialogs, you can do this on the basis of your own layout for the dialog, which you can display in the script with the "New window" command. In this case, you must place fields and buttons on the dialog layout yourself and assign the corresponding button functions.
- How can I catch and handle errors in FileMaker scripts?
- You can catch errors in FileMaker scripts by using the Enable Error Logging command. This command prevents FileMaker from displaying an error message and allows you to continue the script. You can then use the Get(LastErrorNo) command to check the error and adjust the script accordingly, such as performing an alternative action or informing the user.
- How can I use scripts to automate layout changes and navigation?
- Scripts are ideal for automating layout changes and navigating users through the application. You can use the "Go to layout" command to switch between layouts, setting conditions to customize the layout depending on the user role or data situation. This is useful to simplify user guidance in complex applications and ensure that the user always gets to the right place.
- Can I run scripts automatically when starting a database?
- Yes, you can run scripts automatically when starting a database by defining a start script. Go to "File" > "File options" and select the desired script under "Open". This script is executed automatically as soon as the database is opened. You can use it, for example, to customize the user interface, perform calculations or load initial data records.
- How do I test and debug a script in FileMaker?
- To test a script, you can activate the script debugger (step-by-step mode) in the script workspace, which allows you to execute the script step by step and monitor how it works. You can also set breakpoints at which the script is interrupted to check the values of variables or the status of the script. The "Data display" function allows you to monitor the current status of fields and variables in real time.
