Log In    |    Knowledge Base    |    4D Home
Tech Tip: Installing custom fonts with your 4D application (MacOS)
Published On: July 27, 2023

One way to make your 4D application unique is by using a custom font. Custom fonts can be downloaded from the internet and would be saved as font files on the hard drive. There are many font file types for different use cases; for this example, we will use the commonly used .otf file. Luckily, the process of installing in MacOS is very simple as it only requires saving the file into the system's Fonts folder and letting OS take care of the installation process. Knowing this, you can simply use the .copyTo() function of the Files class to programmatically install a custom font with 4D.

You can use the following method to install a font file:

// declare variables
var $fontPath; $fontFolderPath : Text
var $fontObj; $fontFolderObj : Object

// change this value!!
$fontPath:= // insert full path to where font was downloaded to

// create File object for font file
$fontObj:=File($fontPath; fk platform path)

// get path + Folder object for Fonts folder
$fontFolderPath:=System folder(Fonts) // affects all users on computer
//$fontFolderPath:="/Users/[username]/Library/Fonts" // for specific user, change [username] to your own
$fontFolderObj:=Folder($fontFolderPath; fk platform path)

// check if font file is already installed
If (Test path name($fontPath)#Is a document)
   // if not, confirm with user to install the font and restart 4D
     CONFIRM("Would you like to install the font and restart the application?"; "OK"; "Cancel")
     If (OK=1)
       // move file to Fonts folder
       $fontObj.copyTo($fontFolderObj; fk overwrite)
       // restart 4D
       RESTART 4D
     End if
End if

When using this method, make sure to insert the full path of where the font file was saved in the $fontPath variable.

Essentially, the method will first check if the font file already exists in the Fonts folder (the system Fonts folder is used in this example, but you can also set to the path to the user's Fonts folder if wanted). If it already does, no changes are made to the disk, and the application will open as normal. If the file is not detected, 4D will copy over the font file for installation.

The font does not automatically appear in the fonts library in 4D, so the application must be restarted in order to see the changes take place. A CONFIRM dialog window will pop up for the user to confirm installing the font and restarting 4D; this way, the user has a chance to save any progress before restarting the program.