ACF Library

ImportVCFcard

Back to List

Description: Import a VCF card and convert to JSON

FileMaker Prototype:

Set Variable [$res; ACF_Run("ImportVCFcard"; string_FileName)]

Category: UTILITY

Dependencies

  • SelectFileOnly: Select a file and return its name. Does not open the file. NOTE: put USE bootstrap; below the package header instead of copying the code for SelectFileOnly

Function source:

function ImportVCFcard ( string FileName )

   string FirstName, LastName, formalName, Company, 
      BusinessEmail, Cell, Phone,
      WorkAddress, WorkZip, WorkCity, 
      HomeAddress, HomeZip, HomeCity, vCardVersion, vCardUID;  
   
   if (FileName == "") then
      FileName = SelectFileOnly ( "", "Velg en VCARD fil"); 
   end if
   if (FileName == "") then
      return "No file selected";
   end if
   
   
   int x = open ( FileName , "r"); 
   string vcf = read ( x ); 
   close ( x ); 
   print vcf;
// Clean the file: 
   vcf = substitute ( vcf, "\r\n","\n");
   vcf = substitute ( vcf, "\\n","##1");
   vcf = substitute ( vcf, "\\,","##2");
   // vcf = substitute ( vcf, "\\:","##3");
    
   array string lines = explode ( "\n", vcf ); 
   int no = sizeof ( lines ); 
   array string segments; 
   array string subsegments, parts; 
   int i, n; 
   string LineID, Content; 
   JSON result; 
   // Start parsing the Content of the VCF file. 
   
   for (i = 1, no)
      if (lines [i] != "") then
         segments = explode ( ":", lines [i]); 
         n = sizeof (segments); 
         if (n>=2) then  // ignore lines without ":";
            LineID = segments [1]; 
            Content = segments [2]; 
      
            if (LineID == "BEGIN") then 
               if (Content != "VCARD") then
                  throw "Not a VCARD file:" + Content+":"; 
               end if
            elseif (LineID == "VERSION") then
               vCardVersion = Content; 
            elseif (LineID == "PRODID") then
            elseif (LineID == "N") then
               subsegments = explode (";", Content); 
               LastName = subsegments[1]; 
               FirstName = subsegments[2]; 
            elseif (LineID == "FN") then
               formalName = Content; 
            elseif (LineID == "ORG") then
               Company = Content; 
            elseif (LineID == "UID") then
               vCardUID = Content; 
            elseif (LineID == "X-ABUID") then
            elseif (LineID == "END") then
            else
               if (left(LineID,3) == "TEL" ) then
                  if (Pos ( "=CELL", LineID)>0) then
                     Cell = Content; 
                  else
                     Phone = Content; 
                  end if
               elseif (left (LineID, 4) == "item" && substring (LineID, 5,4) == ".ADR") then
                  parts = explode ( ";", Content); 
                  if (Pos ( "HOME", LineID)>0) then
                     HomeAddress = parts[2]; 
                     HomeCity = parts[3];
                     HomeZip = parts[5];
                      
                  else
                     WorkAddress = parts[2]; 
                     WorkCity = parts[3];
                     WorkZip = parts[5];
                  end if
               elseif (left (LineID, 5) == "EMAIL") then
                  BusinessEmail = Content; 
               else
                  // Ignore the item. 
               end if
            end if
         end if
      end if
   end for


   result = JSON ( "FirstName",FirstName, "LastName", LastName, "formalName", formalName, "Company", Company, 
      "BusinessEmail", BusinessEmail,  "Cell", Cell, "Phone",Phone,
      "Work", JSON("Address", WorkAddress, "Zip", WorkZip, "City", WorkCity),
      "Home", JSON("Address", HomeAddress, "Zip", HomeZip, "City", HomeCity),
      "vCardVersion",vCardVersion, "vCardUID", vCardUID);


// Let us also set some FileMaker variables to help query if we allready have the info. 
   $$vcardName = formalName; 
   $$vcardEmail = BusinessEmail; 
      
   return string(result); // 

end

The ImportVCFcard function opens a VCF (vCard) file, parses it, and returns the data as a JSON object. If no filename is provided, the function prompts the user to select a VCF file.

About VCF Cards

VCF (vCard) is a widely used standard for digital contact information, often used in address books and contact-sharing features across applications. VCF cards are also commonly shared through SMS and other messaging platforms.

Use Case

In a contact form, you could implement a button that allows users to import a VCF card, automatically filling in the contact data, making it easy to add new contacts.

Example

Set Variable [$vcfjson; ACF_Run("ImportVCFcard"; "")]

After choosing a VCF file exported from the Apple Address Book, the function returns a JSON object, which FileMaker can easily parse.

Sample output:

{
    "BusinessEmail": "demo@example.com",
    "Cell": "(+47)-12345678",
    "Company": "EXAMPLE COMPANY LTD",
    "FirstName": "John",
    "Home": {
        "Address": "Knakkebakkeskogen 12",
        "City": "KRISTIANSAND",
        "Zip": "4120"
    },
    "LastName": "Doe",
    "Phone": "(+47)12345678",
    "Work": {
        "Address": "Huthegata 12",
        "City": "KRISTIANSAND",
        "Zip": "4120"
    },
    "formalName": "John Doe",
    "vCardUID": "",
    "vCardVersion": "3.0"
}

To update the contacts table, a series of script steps can do the trick.

# Parse JSON data into fields in the Contacts table
if [left(vcfjson;1)="{"] // We have a JSON file
# Set each field in Contacts from the JSON data in $vcfjson
Set Field [Contacts::FirstName; JSONGetElement ( $vcfjson; "FirstName" )]
Set Field [Contacts::LastName; JSONGetElement ( $vcfjson; "LastName" )]
Set Field [Contacts::formalName; JSONGetElement ( $vcfjson; "formalName" )]
Set Field [Contacts::Company; JSONGetElement ( $vcfjson; "Company" )]
Set Field [Contacts::BusinessEmail; JSONGetElement ( $vcfjson; "BusinessEmail" )]
Set Field [Contacts::CellPhone; JSONGetElement ( $vcfjson; "Cell" )]
Set Field [Contacts::Phone; JSONGetElement ( $vcfjson; "Phone" )]

# Set Home Address fields
Set Field [Contacts::HomeAddress; JSONGetElement ( $vcfjson; "Home.Address" )]
Set Field [Contacts::HomeCity; JSONGetElement ( $vcfjson; "Home.City" )]
Set Field [Contacts::HomeZip; JSONGetElement ( $vcfjson; "Home.Zip" )]

# Set Work Address fields
Set Field [Contacts::WorkAddress; JSONGetElement ( $vcfjson; "Work.Address" )]
Set Field [Contacts::WorkCity; JSONGetElement ( $vcfjson; "Work.City" )]
Set Field [Contacts::WorkZip; JSONGetElement ( $vcfjson; "Work.Zip" )]

# Optional step: Capture vCardVersion without setting it to a field in Contacts
Set Variable [$vCardVersion; JSONGetElement ( $vcfjson; "vCardVersion" )]
end if

Explanation

  • FirstName, LastName, and Company are mapped directly based on the JSON keys.
  • formalName, BusinessEmail, CellPhone, and Phone are additional standard fields for contact information.
  • Home and Work Address fields are separated into their individual components: Address, City, and Zip.
  • vCardVersion is stored in a variable but not assigned to a field in the Contacts table.
Back to List