KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Programmatically creating 4DLink files
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: June 16, 2010

You may need a custom 4DLink file but constructing one by hand can be prone to user error. The method below makes creating a 4DLink file a matter of point and click. You pick the 4D structure file (.4DB or .4DC), the 4D data file (.4DD), enter the name for the file (without the .4DLink extension) and the file is created for you.

Once you have created it place the file where you need it, establish the 4D application as the default launch application as per the Tech Tip "What application do my .4DLINK files open? Setting file type association", and you are in business.

  `*****************************************************************************
  `//
  `// Create4DLinkFile
  `//
  `// Purpose: To create a 4D .4DLink file
  `//
  `*****************************************************************************
C_TEXT($MethodName_T)
$MethodName_T:=Current method name
  `===================== Declare Variables ==================================
  `local_variable_declarations
C_TEXT($XML_T;$Path_Struct_T;$Path_Data_T;$Doc_T;$Folder_T;$Delim_T)
C_TIME($DocRef_T)
C_LONGINT($Platform_L)
  `====================== Initialize and Setup ================================

PLATFORM PROPERTIES($Platform_L)
If ($Platform_L=Windows )
   $Delim_T:="\\"
Else
   $Delim_T:=":"
End if

$XML_T:="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r"
$XML_T:=$XML_T+"<database_shortcut structure_opening_mode=\"0\" "
$XML_T:=$XML_T+"structure_file=\"file:///Volumes/"

  `======================== Method Actions ==================================

  ` Get the path to the 4D Structure file
  `
$Path_Struct_T:=Select document("";".4DB, .4DC";"Select structure";Package open )
If (OK=1)
   If (($Path_Struct_T="@.4DB") | ($Path_Struct_T="@.4DC"))
        `
        ` Use the 4D variable "document" to get the full document path
        `
      $Path_Struct_T:=Replace string(document;$Delim_T;"/")
      $XML_T:=$XML_T+$Path_Struct_T+"\" "
      $XML_T:=$XML_T+"data_file=\"file:///Volumes/"
      
        ` Get the path to the 4D data file
        `
      $Path_Data_T:=Select document("";".4DD";"Select the data file";Package open )
      If (OK=1)
         If ($Path_Data_T="@.4DD")
            $Path_Data_T:=Replace string(document;$Delim_T;"/")
            $XML_T:=$XML_T+$Path_Data_T+"\"/>"
            
              ` Get the name for the 4DLink file
              `
            $Doc_T:=Request("4DLink file name")+".4DLink"
            If (OK=1)
               $Folder_T:=Select folder("Select location to save the file")
               $Folder_T:=$Folder_T+$Doc_T
               
                 ` Create and write the file
                 `
               $DocRef_T:=Create document($Folder_T;"utf8")
               If (OK=1)
                  SEND PACKET($DocRef_T;$XML_T) ` Write one word in the document
                  CLOSE DOCUMENT($DocRef_T) ` Close the document
               End if
            End if
         End if
      End if
   End if
End if