Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 3857 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

Or how do I make my scripts more reusable.

 

Perfect example; Phone number formating.

 

Here is the essence of the script:

Set Variable [$AreaCode; Value:Left ( Get ( ActiveFieldContents ); 3)]

Set Variable [$digits3; Value:Middle ( Get ( ActiveFieldContents ); 4; 3)]

Set Variable [$digits4; Value:Middle ( Get ( ActiveFieldContents ); 7; 4)]

Set Field [Tutor::HomePhone; "(" & $AreaCode & ") " & $digits3 & "-" & $digits4 ]

 

Rather than hard coding this script to every field; creating a new script for each instance; how can I make a general reference to the active field?  Thus alowing me to have ONE script that can be reused for many similar fields.

 

Jason

Posted

Perfect example; Phone number formating.

 

This is not a perfect example – not even a good one; phone number formatting can be offloaded to an auto-enter calculation for the field, e.g.

 

Case ( 
  Length ( Trim ( self ) ) = 10 ;
  Let (
    s = Trim ( self ) ;
    area = Left ( s ; 3 ) ;
    digits3 = Middle ( s ; 4 ; 3 ) ;
    digits4 ; Right ( s ; 4 )
    ] ;
    "(" & area & ") " & digits3 & "-" & digits4
    ) ;
  self // or use different formats for different lengths
)
 
Note the use of self to refer to the field whose auto-enter calculation you're editing.
 
And then of course you can put this logic into a Custom Function and simply call, say, FormatAsPhone ( self ), where the function could decide on the appropriate format based on the length.
Posted

I presume that for this case, the script is assuming that the currently active field is the target of the formatting. In that case, you case simply not specify a target field for the Set Field script step, and it will target the active field instead.

 

There isn't a single best way to make scripts reusable in general. What techniques we use to make a script more reusable depends on what exactly the script does, and how exactly we want to reuse it. One general guideline I can suggest is that it's usually not a good idea to try to make a script or calculation reusable until after the need for reuse actually comes up; otherwise, you can easily waste effort making something more general in a way that it may never actually need to be. Comics have been written about the urge for premature generalization. If you're interested in more specific techniques, check out some of the modules at ModularFileMaker.org, which have been written specifically to be reusable.

 

In your case, if you want to use the snippet above as the target of a script trigger you can apply to different fields, it would be fine with just not specifying the target of the Set Field script step. (If you're using this to clean-up data input, it might be better to use an auto-enter calculation on those fields, as eos says, that call a re-usable custom function like this one (which uses this one)).

This topic is 3857 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.