get_params
automatically turns named script parameters into local variables
Average rating: 3.7 (48 votes) Log in to vote
Steve Harley - Show more from this author |
(operates via the side effect of creating local variables for each script parameter)
Function definition: (Copy & paste into FileMaker's Edit Custom Function window)
this function supports a powerful technique for passing multiple/optional named parameters to scripts; it is specific to FM8+, and relies on the feature that Let() can create local (and global) script variables, even from a within custom function
the principle is that a script is called with a string parameter of the form
"$param_name1 = value1 ; $param_name2 = value2"
the parameter string will be wrapped with Let() and Evaluate(), using the well-known technique of passing multiple named parameters; however since the names are prefixed with "$", the ordinarily "temporary" variables created will instead persist outside the Let() to become local script variables
if the evaluation succeeds, get_params will return 1; if there is an evaluation error (usually because of a malformed parameter string), Let() returns an undefined value, hence the test for equality to 1 (it's not as redundant as it seems), so my pattern is to call get_params() within an If statement at the top of a script both to automatically set local variables and to catch gross errors in the parameter string
the script that receives the parameters may preset a default value of a local variable prior to calling get_params, and if the variable is defined in the parameter, it will override the default; if the value is not defined, the default will prevail, thus one can intuitively specify optional parameters to a script
the main limitations have to do with the fact the overall script parameter must be a string; within the string, escaping of substrings is necessary; the general case is illustrated by this parameter, from which get_params will assign to the script variable $num_files a numeric value of 3 and to $filename_prefix a string value of "foo"
"$num_files=3;$filename_prefix=\"foo\""
another issue is that, due to an Evaluate feature, line ends within parameter substrings are converted to spaces; substitution of a proxy character (such as a vertical bar) is the recommended work around
i use this technique along with a naming scheme for scripts which includes the parameter names and whether they are optional; for example a script might be named
create_files($filename_prefix[; $num_files=5])
which indicates the two parameter names, and that if $num_files is omitted it will take a default value of 5; i find this naming scheme, in addition to being self-documenting, makes it much easier to remember the parameter names while setting up the parameter in a Perform Script step
in this case the script might read:
Set Variable [$num_files; 5 // the default value]
If [not get_params()]
// handle an error with the parameter string (i usually log it and return a fail value from the script)
Else
// normal processing; param values are now defined; the rest is just example and is not relevant to the concept
Loop
Exit Loop If [$num_files < 1 // default value or passed value]
// other actions here
Set Variable [$num_files; $num_files - 1 // decrement loop counter]
End Loop
End If
(i would ordinarily avoid changing the value of script parameters, but in this case it makes the example simpler)
Comments
Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.