KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Get the current users Short Name on Mac OS X
PRODUCT: 4D | VERSION: 12.1 | PLATFORM: Mac OS X
Published On: December 21, 2010

In versions prior to 4D v11 SQL the command Current machine owner returned the current owners short user name on Mac OS X. On Mac OS X that command now returns the current owners long name, as set in the current user account on the machine.

To once again acquire the short name for the current user, the method below accomplishes that task.

The method below uses the 4D function Match regex and the RegEx (Regular Expression) feature of "Lookarounds" to isolate the username. It then uses the command Substring to acquire the username from the directory path to the active 4D folder.

If (True)
    If (False)
    //*************************************************************************
    //
    // Current_Machine_User
    //
    // Purpose: To return the Mac OS X current users name.
    // Acquire the current users name from the directory path to the
    // Active 4D folder which is created by default on Mac OS at:
    // {Disk}:Users:Current user:Library:Preferences:4D
    //
    // $0 - TEXT - current users name
    //
    //*************************************************************************

   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations

   C_TEXT($0)
    //------------------------------------------------------------------------
    //method_wide_constants_declarations
    //------------------------------------------------------------------------
    //local_variable_declarations

   C_LONGINT($From_L;$Len_L)
   C_TEXT($LookBk_T;$LookFwd_T;$RegExPat_T;$UserName_T)
End if
  //====================== Initialize and Setup ================================

  // The "Lookback" pattern
  //

$LookBk_T:="(?<=Users:)"

  // The "Lookahead" pattern
  //

$LookFwd_T:="(?=:Library)"

  // The Regex "Lookaround" pattern
  // \w matches any word character (alphanumeric & underscore).
  // \ escape char has to be escaped in 4D, hence \\.
  //

$RegExPat_T:=$LookBk_T+"\\w+"+$LookFwd_T

  // Get the Active 4D Folder
  //

$Path_T:=Get 4D folder(Active 4D Folder)

  //======================== Method Actions ==================================

If (Match regex($RegExPat_T;$Path_T;1;$From_L;$Len_L))
   $UserName_T:=Substring($Path_T;$From_L;$Len_L-1)
End if

  //======================== Clean up and Exit =================================

$0:=$UserName_T