KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Find Form Objects Based on their Properties
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: March 21, 2022

When wanting to find or edit all form objects that have a specific property, this utility method can be used. This utility method can help with finding all form form objects that have a specific property in their object definition. For example, it can be used to find all buttons with the focusable property set to False.

The method requires 2 parameters and the 3rd is optional.
- $1 (Text) - Property name to search
- $2 (Variant) - Value of property to search
- $3 (Text) Optional - Type of form object
- $0 (CollectIon) - Returns collection containing the form objects that matched the search criteria. Includes the table, form, and name of object.

Note: This method searches through the form.4DForm, so it must only be used in project mode. This method will only find the form objects on a property that is defined at the object level (ie. the property is bolded in the properties list.). If the property name in the properties list differs with the name saved in form.4DForm, use the name saved in form.4DForm.

//METHOD: util_FindFormObjects
//DESCRIPTION: Finds form objects based on a specific property
//PARAMETERS :
//$1 (Text) - Property name to search
//$2 (Variant) - Value of property to search
//$3 (Text) Optional - Type of form object
//$0 (CollectIon) - Returns collection containing the form objects that matched the search criteria. Includes the table, form, and object name.

If (Count parameters>=2)
  C_OBJECT($toFind; $forms_folder_o; $tableForms_folder_o; $form_o; $page; $o)
  C_COLLECTION($0; $return_c)
  C_LONGINT($i; $x; $y; $z; $page_num; $type)
  C_TEXT($1; $3; $jsonString_t; $object)
  C_VARIANT($2)
  C_BOOLEAN($addObject_b)
  
  ARRAY TEXT($tableName_a; 0)
  ARRAY LONGINT($tableNum_a; 0)
  ARRAY TEXT($formName_a; 0)
  ARRAY TEXT($tableFormName_a; 0)
  Case of
    : (Count parameters=2)
      $toFind:=New object("prop"; $1; "value"; $2)
  
    : (Count parameters=3)
      $toFind:=New object("prop"; $1; "value"; $2; "type"; $3)
  
  End case
  $return_c:=New collection
  
  GET TABLE TITLES($tableName_a; $tableNum_a)
  For ($i; 1; Size of array($tableName_a))
    FORM GET NAMES(Table($tableNum_a{$i})->; $tableFormName_a)
  End for
  FORM GET NAMES($formName_a)
  
  $forms_folder_o:=Folder("/SOURCES/Forms")
  $tableForms_folder_o:=Folder("/SOURCES/TableForms")
  
  //get form objects from project form JSON
  If ($forms_folder_o.exists=True)
    // for each form
    For ($x; 1; Size of array($formName_a))
      //if form file exists
      If (Test path name($forms_folder_o.platformPath+$formName_a{$x}+Folder separator+"form.4DForm")=1)
        $jsonString_t:=Document to text($forms_folder_o.platformPath+$formName_a{$x}+Folder separator+"form.4DForm")
        $form_o:=JSON Parse($jsonString_t)
        $page_num:=0
        For each ($page; $form_o.pages)
          For each ($object; $page.objects)
            Case of
              : ($toFind.type#Null)
                If ($page.objects[$object].type=$toFind.type)
                  If (Undefined($page.objects[$object][$toFind.prop])=False)
                    $type:=Value type($page.objects[$object][$toFind.prop])
                    Case of
                      : ($type=42)
                        If ($page.objects[$object][$toFind.prop].indexOf($toFind.value)#-1)
                          $addObject_b:=True
                        End if
                      : ($type#42)
                        If ($page.objects[$object][$toFind.prop]=$toFind.value)
                          $addObject_b:=True
                        End if
                    End case
                  End if
                End if
              : ($toFind.type=Null)
                If (Undefined($page.objects[$object][$toFind.prop])=False)
                  $type:=Value type($page.objects[$object][$toFind.prop])
                  Case of
                    : ($type=42)
                      If ($page.objects[$object][$toFind.prop].indexOf($toFind.value)#-1)
                        $addObject_b:=True
                      End if
                    : ($type#42)
                      If ($page.objects[$object][$toFind.prop]=$toFind.value)
                        $addObject_b:=True
                      End if
                  End case
                End if
            End case
            If ($addObject_b)
              $addObject_b:=False
              $o:=New object
              $o.form:=$formName_a{$x}
              $o.page:=$page_num
              $o.object:=$object
              $o.searched:=New object($toFind.prop; $toFind.value)
              $return_c.push($o)
            End if
          End for each
          $page_num:=$page_num+1
        End for each
      End if
    End for
  End if
  
  //get form objects from table form JSON
  If ($tableForms_folder_o.exists=True)
    //for each table
    For ($y; 1; Size of array($tableNum_a))
      If (Test path name($tableForms_folder_o.platformPath+String($tableNum_a{$y}))=0)
        //for each table form
        For ($z; 1; Size of array($tableFormName_a))
          If (Test path name($tableForms_folder_o.platformPath+String($tableNum_a{$y})+Folder separator+$tableFormName_a{$z}+Folder separator+"form.4DForm")=1)
            $jsonString_t:=Document to text($tableForms_folder_o.platformPath+String($tableNum_a{$y})+Folder separator+$tableFormName_a{$z}+Folder separator+"form.4DForm")
            $tableFormJson_o:=JSON Parse($jsonString_t)
            $page_num:=0
            For each ($page; $tableFormJson_o.pages)
              For each ($object; $page.objects)
                Case of
                  : ($toFind.type#Null)
                    If ($page.objects[$object].type=$toFind.type)
                      If (Undefined($page.objects[$object][$toFind.prop])=False)
                        $type:=Value type($page.objects[$object][$toFind.prop])
                        Case of
                          : ($type=42)
                            If ($page.objects[$object][$toFind.prop].indexOf($toFind.value)#-1)
                              $addObject_b:=True
                            End if
                          : ($type#42)
                            If ($page.objects[$object][$toFind.prop]=$toFind.value)
                              $addObject_b:=True
                            End if
                        End case
                      End if
                    End if
                : ($toFind.type=Null)
                  If (Undefined($page.objects[$object][$toFind.prop])=False)
                    $type:=Value type($page.objects[$object][$toFind.prop])
                    Case of
                      : ($type=42)
                        If ($page.objects[$object][$toFind.prop].indexOf($toFind.value)#-1)
                          $addObject_b:=True
                        End if
                      : ($type#42)
                        If ($page.objects[$object][$toFind.prop]=$toFind.value)
                          $addObject_b:=True
                        End if
                    End case
                  End if
                End case
                If ($addObject_b)
                  $addObject_b:=False
                  $o:=New object
                  $o.form:=$formName_a{$z}
                  $o.table:=$tableNum_a{$y}
                  $o.page:=$page_num
                  $o.object:=$object
                  $o.searched:=New object($toFind.prop; $toFind.value)
                  $return_c.push($o)
                End if
              End for each
              $page_num:=$page_num+1
            End for each
          End if
        End for
      End if
    End for
  End if
  
  $0:=$return_c
  
End if


Example of finding all buttons with focusable property set to false in object definition

C_COLLECTION($return)
$return:=util_FindFormObjects("focusable"; False; "button")

Commented by Jody Bevan on May 14, 2022 at 12:22 PM
Thank you for this example. It is a great foundation for what I want to do.