KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to parse forms for active object event
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: March 20, 2023

Below is a utility method that parses all forms in a project to find any form objects that have a particular event active:

/*
METHOD: findFormObjectsWithActiveEvent
INPUT: event name (e.g. "On Double Clicked") to find activated for any form object
OUTPUT: JSON object with collections of names of form objects that have the input event active.
Organized by Project/Table Forms > Table # (if Table Form) > Form Name > Page #
Example structure:
{
 "Project Forms":{
  "formName":{
   "Page #": [ "obj1", "obj2", ...],
   ...
  },
  ...
 },
 "Table Forms":{
  "Table #": {
   "formName":{
    "Page #": [ "obj2”, "obj3”, ...],
    ...
   },
   ...
  },
  ...
 }
}
NOTE: An empty collection indicates that there are no form objects on the page with the event activated.
*/
#DECLARE($event : Text)->$results : Object
var $eventsColl; $formsColl; $pagesColl; $objectsColl; $eventsColl : Collection
var $tableFoldersColl; $tableFormsColl; $tpagesColl : Collection
var $eventProp; $projectFolder; $projectForms; $projFormsObj; $formFolder; $formFile : Object
var $page; $object; $tableForms; $tableFoldersObj; $tableFolder; $tableForm; $tableFormFile : Object
var $formName; $formText; $pageStr; $objectName; $tableFolderName; $tableFormName; $tableFormText : Text
var $pageNum : Integer

$results:=New object
// map event name to event property
$eventProp:=New object
$eventProp["On Load"]:="onLoad"
$eventProp["On Validate"]:="onValidate"
$eventProp["On Clicked"]:="onClick"
$eventProp["On Double Clicked"]:="onDoubleClick"
$eventProp["On Header"]:="onHeader"
$eventProp["On Display Detail"]:="onDisplayDetail"
$eventProp["On Getting Focus"]:="onGettingFocus"
$eventProp["On Losing Focus"]:="onLosingFocus"
$eventProp["On Begin Drag Over"]:="onBeginDragOver"
$eventProp["On Drag Over"]:="onDragOver"
$eventProp["On Drop"]:="onDrop"
$eventProp["On Before Keystroke"]:="onBeforeKeystroke"
$eventProp["On After Keystroke"]:="onAfterKeystroke"
$eventProp["On Data Change"]:="onDataChange"
$eventProp["On After Edit"]:="onAfterEdit"
$eventProp["On Printing Detail"]:="onPrintingDetail"
$eventProp["On Printing Break"]:="onPrintingBreak"
$eventProp["On Printing Footer"]:="onPrintingFooter"
$eventProp["On Unload"]:="onUnload"
$eventProp["On Selection Change"]:="onSelectionChange"
$eventProp["On Mouse Enter"]:="onMouseEnter"
$eventProp["On Mouse Move"]:="onMouseMove"
$eventProp["On Mouse Leave"]:="onMouseLeave"
$eventProp["On Expand"]:="onExpand"
$eventProp["On Collapse"]:="onCollapse"
$eventProp["On Delete Action"]:="onDeleteAction"
$eventProp["On Alternative Click"]:="onAlternateClick"
$eventProp["On Header Click"]:="onHeaderClick"
$eventProp["On Footer Click"]:="onFooterClick"
$eventProp["On Open Detail"]:="onOpenDetail"
$eventProp["On Close Detail"]:="onCloseDetail"
$eventProp["On After Sort"]:="onAfterSort"
$eventProp["On Column Moved"]:="onColumnMove"
$eventProp["On Column Resize"]:="onColumnResize"
$eventProp["On Row Moved"]:="onRowMove"
$eventProp["On Before Data Entry"]:="onBeforeDataEntry"
$eventProp["On Scroll"]:="onScroll"
$eventProp["On Long Click"]:="onLongClick"
$eventProp["On Plug in Area"]:="onPluginArea"
$eventProp["On Begin URL Loading"]:="onBeginURLLoading"
$eventProp["On URL Resource Loading"]:="onURLResourceLoading"
$eventProp["On End URL Loading"]:="onEndURLLoading"
$eventProp["On URL Loading Error"]:="onURLLoadingError"
$eventProp["On URL Filtering"]:="onURLFiltering"
$eventProp["On Open External Link"]:="onOpenExternalLink"
$eventProp["On Window Opening Denied"]:="onWindowOpeningDenied"
$eventProp["On VP Ready"]:="onVPReady"
$eventProp["On Row Resize"]:="onRowResize"
$eventProp["On VP Range Changed"]:="onVPRangeChanged"

$projectFolder:=Folder("/PACKAGE/Project")

If ($projectFolder.exists)
   // Project Forms
   $projectForms:=Folder("/PACKAGE/Project/Sources/Forms")
   $formsColl:=$projectForms.folders()

   $projFormsObj:=New object
   $results["Project Forms"]:=$projFormsObj

   For each ($formFolder; $formsColl)
     $formName:=$formFolder.name
     $results["Project Forms"][$formName]:=New object
     $formFile:=File($formFolder.path+"form.4DForm")
     $formText:=$formFile.getText()
     $pagesColl:=JSON Parse($formText).pages

     For ($pageNum; 0; $pagesColl.length-1)
       $page:=$pagesColl[$pageNum]

       If ($page#Null)
         $pageStr:="Page "+String($pageNum)
         $objectsColl:=New collection
         $results["Project Forms"][$formName][$pageStr]:=$objectsColl

         If (Not(OB Is empty($page.objects)))
           For each ($objectName; $page.objects)
             $object:=$page.objects[$objectName]
             $eventsColl:=$object.events

             If ($eventsColl.length>0)
               // search for desired event
               If ($eventsColl.indexOf($eventProp[$event])#-1)
                 $objectsColl.push($objectName)

               End if
             End if
           End for each
         End if
       End if
     End for
   End for each

   // Table Forms
   $tableForms:=Folder("/PACKAGE/Project/Sources/TableForms")
   $tableFoldersColl:=$tableForms.folders()

   $tableFoldersObj:=New object
   $results["Table Forms"]:=$tableFoldersObj

   For each ($tableFolder; $tableFoldersColl)
     $tableFolderName:="Table "+$tableFolder.name
     $tableFoldersObj[$tableFolderName]:=New object

     $tableFormsColl:=$tableFolder.folders()
     For each ($tableForm; $tableFormsColl)
       $tableFormName:=$tableForm.name
       $tableFoldersObj[$tableFolderName][$tableFormName]:=New object
       $tableFormFile:=File($tableForm.path+"form.4DForm")
       $tableFormText:=$tableFormFile.getText()
       $tpagesColl:=JSON Parse($tableFormText).pages

       For ($pageNum; 0; $tpagesColl.length-1)
         $page:=$tpagesColl[$pageNum]

         If ($page#Null)
           $pageStr:="Page "+String($pageNum)
           $objectsColl:=New collection
           $tableFoldersObj[$tableFolderName][$tableFormName][$pageStr]:=$objectsColl

           If (Not(OB Is empty($page.objects)))
             For each ($objectName; $page.objects)
               $object:=$page.objects[$objectName]
               $eventsColl:=$object.events

               If ($eventsColl.length>0)
                 // search for desired event
                 If ($eventsColl.indexOf($eventProp[$event])#-1)
                   $objectsColl.push($objectName)

                 End if
               End if
             End for each
           End if
         End if
       End for
     End for each
   End for each

Else
   ALERT("Project does not exist.")
End if

Example - find all form objects that have “On Display Detail” event turned ON:

$results:=findFormObjectsWithActiveEvent("On Display Detail”)

results: