Modular script libraries in FileMaker
ExpertDesign reusable, table-agnostic utility scripts and organize them into a logical library that any part of your solution can call.
What you'll learn
- How to design a script that is table-agnostic and reusable
- How to document a library script with a contract comment block
- How to handle versioning as library scripts evolve
- How to organize library scripts in the Script Workspace
- How to import library scripts into a new solution file
A script library is a collection of utility scripts that perform generic operations -- formatting a number as currency, sending an email, building a JSON envelope, logging an event -- without being tied to a specific table or layout. Library scripts accept all context via their parameter and return results via Exit Script. They are the FileMaker equivalent of shared functions in a programming language.
Design a table-agnostic utility script
A library script never references a specific table's fields directly. All input arrives via Get(ScriptParameter) as a JSON object. All output goes out via Exit Script [ Text Result: JSON ]. The script knows nothing about the calling context.
# Script: Util - Format Phone Number
# Parameter: JSON { "phone": "5551234567", "format": "us" }
# Returns: JSON { "success": true, "formatted": "(555) 123-4567" }
Set Variable [ $p ; Value: Get ( ScriptParameter ) ]
Set Variable [ $raw ; Value: Filter ( JSONGetElement ( $p ; "phone" ) ; "0123456789" ) ]
Set Variable [ $format ; Value: JSONGetElement ( $p ; "format" ) ]
If [ IsEmpty ( $raw ) ]
Exit Script [ Text Result:
JSONSetElement ( "{}" ;
[ "success" ; False ; JSONBoolean ] ;
[ "error" ; "Phone number is empty" ; JSONString ]
)
]
End If
If [ $format = "us" and Length ( $raw ) = 10 ]
Set Variable [ $formatted ; Value:
"(" & Left ( $raw ; 3 ) & ") " &
Middle ( $raw ; 4 ; 3 ) & "-" &
Right ( $raw ; 4 )
]
Else
Set Variable [ $formatted ; Value: $raw ]
End If
Exit Script [ Text Result:
JSONSetElement ( "{}" ;
[ "success" ; True ; JSONBoolean ] ;
[ "formatted" ; $formatted ; JSONString ]
)
]Sign in to track your progress and pick up where you left off.
Sign in to FM Dojo