We've shown you how to add custom headers to a CSV file. Custom headers are useful, particularly for end users, because they provide intelligible header names when downloaded FileMaker files are opened in other applications. We developers don't always use intuitive field names when we're in the heat of custom app development. It's usually a bunch of geeky gibberish only the coder can understand. FileMaker 18 actually simplifies our custom header export process a bit, thanks to a few of the new script steps that this version offers.
Before we begin, it might be helpful to review our FileMaker 18 file-based script steps blog post, particularly the section that discusses how to read, write and manage external files. This will give you a good foundation before taking a look at this more advanced topic.
There are three basic steps to creating custom headers in FileMaker 18:
- Export a set of found records to a CSV file and open the newly created file in FileMaker's file memory
- Attach custom headers to the CSV file
- Create a new file with the custom headers
The first thing we'll do is export the data to a file and then open it in FileMaker’s memory file space. We use the Open Data File script step, which is part of the new suite of file-based scripts steps available in FileMaker 18 that allow us to manipulate text files on our local computers. This specific script step takes a file on your PC and loads it into FileMaker’s memory so that you can access it via the other steps in this script. The important thing to note here is that the new Data File script steps use the ID of the opened file and that ID is a serial number that is reset for each session of the FileMaker application. We use this ID to work with the file as long it resides in FileMaker’s memory.
Next, we set our custom headers to a variable. Since we're creating a comma-separated value (CSV) file, we need to format the headers properly using quotes, separated by commas (of course) and don't forget to include a return character at the end. Then, we need to combine the custom headers and the opened file. The last step in this part of the process sets the position where we will start the write function. Since we're going to rewrite the entire file, we'll set it at 0, the start of the file.
Now that we've prepped everything in FileMaker’s memory, it's time to send it all to a disk so that other programs can open it. We accomplish that by simply using the Write to Data File step. The final thing we do here is important because, as far as your computer’s OS is concerned, FileMaker still has this file locked until we send the Close Data File command. You have to specify this step so that other apps can open the file.
This example has been specifically designed for the output to be read on a computer running Apple’s MacOS, but you can use the many options included in the Data File script steps to customize it for any number of other platforms. Download the sample file to see this script in action.
There's a lot you can to do with the new FileMaker 18 script steps, like combining the Data File and Error Logging script steps to automatically create error logs and troubleshoot development problems. Learn more about some of the new features in FileMaker 18.
This article is also posted on FileMakerProGurus.com.