KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to programmatically return 4D's build number
PRODUCT: 4D | VERSION: 11.4 | PLATFORM: Mac & Win
Published On: August 20, 2009
Your current browser may not allow you to download the asset. We recommend Mozilla Firefox or Google Chrome.
Log In

The following code can be used to programmatically return the build number of 4D currently being used. It is cross platform and accesses the 4D Application differently on Mac and Windows.

On Mac, the Info.plist file is parsed. This is an XML file that containes information about the application. The file is transformed with XSLT and then the Match regex command is used to test the result. If it matches correctly, then the XML result is parsed to get the build number.

On Windows, LAUNCH EXTERNAL PROCESS is used to run a VB Script that is written to get the 4D Application's file version. The return value is then tested with Match regex to make sure that the build is correctly included, and then the result is parsed to get the build number.

Here is the method:

C_TEXT($0)

C_LONGINT($platform_l)
PLATFORM PROPERTIES($platform_l)

If ($platform_l=Mac OS )
  $xsl_source_t:=""
  $xsl_source_t:=$xsl_source_t+"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
  $xsl_source_t:=$xsl_source_t+"<xsl:stylesheet version=\"1.0\" xmlns:xsl="
  $xsl_source_t:=$xsl_source_t+"\"https://www.w3.org/1999/XSL/Transform\">\n"
  $xsl_source_t:=$xsl_source_t+"<xsl:output method=\"text\" />\n"
  $xsl_source_t:=$xsl_source_t+"<xsl:template match=\"/\">\n"
  $xsl_source_t:=$xsl_source_t+"<xsl:for-each select=\"/plist/dict/key[ string() = "
  $xsl_source_t:=$xsl_source_t+"'CFBundleShortVersionString' ]\">\n"
  $xsl_source_t:=$xsl_source_t+"<xsl:value-of select=\"./following-sibling::*\" />\n"
  $xsl_source_t:=$xsl_source_t+"</xsl:for-each>\n"
  $xsl_source_t:=$xsl_source_t+"</xsl:template>\n"
  $xsl_source_t:=$xsl_source_t+"</xsl:stylesheet>\n"

  C_BLOB($xml_result_x;$xsl_source_x)
  CONVERT FROM TEXT($xsl_source_t;"utf-8";$xsl_source_x)
  DOCUMENT TO BLOB(Application file+":Contents:Info.plist";$xml_source_x)
  APPLY XSLT TRANSFORMATION($xml_source_x;$xsl_source_x;$xml_result_x)
  $xml_result_t:=Convert to text($xml_result_x;"utf-8")

  ARRAY LONGINT($match_pos_al;0)
  ARRAY LONGINT($match_length_al;0)

  If (Match regex("(.*)\\((.*)\\)";$xml_result_t;1;$match_pos_al;$match_length_al))
    $0:=Substring($xml_result_t;$match_pos_al{2};$match_length_al{2})
  End if

Else
  $vbs_in_t:=""

  $vbs_in_t:=$vbs_in_t+"Set fso = CreateObject(\"Scripting.FileSystemObject\")\r\n"
  $vbs_in_t:=$vbs_in_t+"WScript.StdOut.Write fso.GetFileVersion(\""
  $vbs_in_t:=$vbs_in_t+Application file+"\")\r\n"

  $script_path_t:=Temporary folder+"$"+String(Milliseconds)+".vbs"

  CONVERT FROM TEXT($vbs_in_t;"UTF-16LE";$script_x)
  BLOB TO DOCUMENT($script_path_t;$script_x)
  SET ENVIRONMENT VARIABLE("_4D_OPTION_HIDE_CONSOLE";"true")
  C_BLOB($stdin_x;$stdout_x;$stderr_x)
  $cscript_t:="cscript //Nologo //U \""+$script_path_t+"\""
  LAUNCH EXTERNAL PROCESS($cscript_t;$stdin_x;$stdout_x;$stderr_x)

  $vbs_out_t:=Convert to text($stdout_x;"UTF-16LE")

  ARRAY LONGINT($match_pos_al;0)
  ARRAY LONGINT($match_length_al;0)

  If (Match regex("(.*)\\.(.*)";$vbs_out_t;1;$match_pos_al;$match_length_al))
    $0:=Substring($vbs_out_t;$match_pos_al{2};$match_length_al{2})
  End if

  DELETE DOCUMENT($script_path_t)
End if

Commented by Jesse Pina on August 21, 2009 at 10:25 AM
In a Client/Server setup, this method can be very useful for confirming that all clients have been updated to a very specific version of 4D.