ACF Library

ACF_Calculator

Back to List

Description: Open Ad-hoc calculator for field entry of numeric fields.

Call options

FileMaker calculation

Set Variable [$res; ACF_Run("ACF_Calculator"; string_previousValue;  string_fieldName;  string_tableOccurrence)]

QuickJS script executed inside ACF

const previousValue = "";
const fieldName = "";
const tableOccurrence = "";

const result = ACF_Run("ACF_Calculator", previousValue, fieldName, tableOccurrence);

Category: UTILITY

Function source:

function ACF_CalculatorVariables (string previousValue, string fieldName, string tableOccurrence)
    // This is the context-selection boundary. Add BestSalg variable mappings here,
    // using BOTH tableOccurrence and fieldName. Do not infer a related record
    // from a table name: portal rows can refer to different records.
    // Starter variables work for every numeric field; no business field mapping
    // has been assumed. Additional values must be explicitly collected here
    // or supplied by the calling script before opening the calculator.
    string expr = substitute(previousValue, ",", ".");
    if (expr == "") then
        expr = "0";
    end if
    return JSON("value", ACF_User_calculation(expr));
end

function ACF_Calculator (string previousValue, string fieldName, string tableOccurrence)
   if (! VersionAtLeast(pluginversion, "1.8.0.11")) then
      return string(JSON("status", "cancelled")); 
   end if
    string expr = substitute(previousValue, ",", ".");
    if (expr == "") then
        expr = "0";
    end if
    json variables = ACF_CalculatorVariables(previousValue, fieldName, tableOccurrence);
    json result = ACF_calculatorDialog(expr, variables, "Simple");
    return string(result);
end

The ACF_Calculator function, using a small helper function to craft a JSON for the local variables available in the calculator.

NOTE: Require ACF-Plugin version 1.8.0.12 to work.

We usually put this script into the FileMaker solution:

# Script: CalculatorActiveField
# Menu item title calculation: _("Calculator…"); no script parameter.
# Run with the user's normal privileges, not full access.

Set Error Capture [ On ]
Set Variable [ $field; Value: Get ( ActiveFieldName ) ]
Set Variable [ $tableOccurrence; Value: Get ( ActiveFieldTableName ) ]
Set Variable [ $previousValue; Value: GetAsText ( Get ( ActiveFieldContents ) ) ]
Set Variable [ $target; Value: $tableOccurrence & "::" & $field ]
Set Variable [ $portalRow; Value: Get ( ActivePortalRowNumber ) ]
Set Variable [ $repetition; Value: Get ( ActiveRepetitionNumber ) ]
Set Variable [ $recordID; Value: Get ( RecordID ) ]
Set Variable [ $layout; Value: Get ( LayoutName ) ]
Set Variable [ $window; Value: Get ( WindowName ) ]

If [ Get ( WindowMode ) <> 0 or IsEmpty ( $field ) or IsEmpty ( $tableOccurrence ) ]
    Show Custom Dialog [ _("Calculator"); _("Click in a numeric field before opening the calculator.") ]
    Exit Script [ Text Result: "cancelled" ]
End If
Set Variable [ $type; Value: FieldType ( Get ( FileName ) ; $target ) ]
If [ MiddleWords ( $type ; 2 ; 1 ) <> "Number" or ( LeftWords ( $type ; 1 ) <> "Standard" and LeftWords ( $type ; 1 ) <> "Global" ) ]
    Show Custom Dialog [ _("Calculator"); _("The calculator can be used in editable numeric fields.") ]
    Exit Script [ Text Result: "cancelled" ]
End If

Set Variable [ $result; Value: ACF_run ( "ACF_Calculator" ; $previousValue ; $field ; $tableOccurrence ) ]
Set Variable [ $status; Value: JSONGetElement ( $result ; "status" ) ]
If [ $status = "cancelled" ]
    Exit Script [ Text Result: $result ]
End If
If [ $status <> "ok" or IsEmpty ( JSONGetElement ( $result ; "value" ) ) or Left ( JSONFormatElements ( $result ) ; 1 ) = "?" ]
    Show Custom Dialog [ _("Calculator"); _("The calculation could not be completed. The field has not been changed.") & "¶" & $result ]
    Exit Script [ Text Result: "error" ]
End If

# The dialog must return to the same active field and record/portal context.
If [ Get ( WindowName ) <> $window or Get ( LayoutName ) <> $layout or Get ( RecordID ) <> $recordID or Get ( ActivePortalRowNumber ) <> $portalRow or Get ( ActiveRepetitionNumber ) <> $repetition or Get ( ActiveFieldTableName ) <> $tableOccurrence or Get ( ActiveFieldName ) <> $field or GetAsText ( Get ( ActiveFieldContents ) ) <> $previousValue ]
    Show Custom Dialog [ _("Calculator"); _("The active field or its contents have changed. The result has not been inserted.") ]
    Exit Script [ Text Result: "error" ]
End If
# Target NOT specified; Select entire contents ON. Keep the active portal row/repetition.
# Use an input step so field-edit triggers can run. Do not commit or leave the field here.
Insert Calculated Result [ Select; GetAsNumber ( JSONGetElement ( $result ; "value" ) ) ]
Set Variable [ $error; Value: Get ( LastError ) ]
If [ $error <> 0 ]
    Show Custom Dialog [ _("Calculator"); _("The result could not be inserted. FileMaker error: ") & $error ]
    Exit Script [ Text Result: "error" ]
End If
Exit Script [ Text Result: $result ]

Copy the text above, use DDRparser -p in terminal, and paste into the new script.

Then add a custom menu item, available in all layout where this should be available:

name: _("Calculator…")
Action: Perform script ("CalculatorActiveField")
Keys: Command-Option+R (if available) - For norwegian "Regn", meaning calculate. 

When the user stays in a numeric field on your layout, simply press Command-option-R to open the calculator:

Simple-calc

When user press "Use result", the calc closes, and the result is shown in the field.

Back to List