FileMaker 18: How to Use the New Data File Script Steps

FileMaker 18 offers a series of new script steps that add native file creation and manipulation to the developer’s toolkit. The utility of these new script steps will be fully realized once people start using them, but right away I can foresee some great use cases. Write records to a text file for import into another system? Generate an HTML document? Custom local log files? All are possible with these new script steps.

The script steps include:

  • Create Data File
  • Open Data File
  • Get Data File Position
  • Set Data File Position
  • Get File Exists
  • Get File Size
  • Write to Data File
  • Read from Data File
  • Rename File
  • Close Data File
  • Delete File

Plus the function

  • Get ( OpenDataFileInfo )

Some of these script steps are simple file system functions, enabling FileMaker to interact more easily with the Mac’s Finder or Windows Explorer. Delete File does just that – it just deletes a named file from disk. Likewise, Rename File, Get File Size and Get File Exists do more or less what you would expect. Interestingly, Delete File and Rename File give no confirmation dialog or warning. It seems you can do just about whatever you like with them, including appending a completely different file extension or wipe out a file completely (Delete File doesn’t just stick it in the recycle bin or trash; it fully deletes it). Definitely use these with caution!

Things get a bit more interesting with the rest of the script steps. With these, you can actually create a file on disk, with any file extension you want, and as far as I can tell, any format you can write with text. This includes an HTML file, CSV, Tab-delimited text, plain text, JSON, a log file, a Base64 encoded image, a CSS library, whatever! As an example, I’ll show you how to export records to a basic text file on the desktop.

Create a text file

The first script step to use will be a “Set Variable”, in which you’ll want to set up a filename and file path:

Set Variable [ $fileName; Value:Get ( DesktopPath ) & "18_dataFile.txt" ] 

Next you’ll want to check to see if the file exists already on disk (not strictly necessary, but you may want to handle it if the file does already exist). If the file doesn’t exist you can create it using the “Create Data File” step:

Get File Exists [ “$fileName” ; Target: $exists ] 
#results in boolean 1 if $fileName exists
If [ not $exists ]
#create an empty file
Create Data File [ “$fileName” ; Create folders: Off ]
End If

Now that we’ve created a blank data file, we need to open it:

#open the empty file 
Open Data File [ “$fileName” ; Target: $dataFile ]

Once we have the file in memory, we need to get an ID from it using the new Get (OpenDataFileInfo) function. This step is crucial, because the ID of this file is required when referencing it in other script steps. When using this function, the file info is returned in the format:

1    file:/Macintosh HD/Users/davidweiner/Desktop/18_dataFile.txt

The first number is the actual file ID, which is what we need. The space between the ID and the file name is actually a Tab character, and there are a number of ways you could extract the ID from this. I used a Substitute function to change the Tab to a Pilcrow, and then extracted the first value in the list to get our ID:

#get the file ID using the Get ( OpenDataFileInfo ) function 
Set Variable [ $fileID; Value:
GetValue (Substitute (Get (OpenDataFileInfo); " " ; ¶ ); 1 )]

Now that we have a file open, and an ID to reference it, we can begin to write data into it. For this demo I set up a simple looping script, gathered some Contact data from each record into a JSON blob, and inserted the lines into the open data file one at a time:

#insert JSON records into the file 
Go to Record/Request/Page
[ First ]

Loop

Set Variable [ $json; Value:JSONSetElement ( "{}" ;
[ "id" ; Contacts::PrimaryKey ; JSONString ];
[ "first" ; Contacts::First Name ; JSONString ];
[ "last" ; Contacts::Last Name ; JSONString ] )]

Write to Data File [ File ID: $fileID ;
Data source: $json ; Write as: UTF-16 ; Append line feed: On ]

Go to Record/Request/Page
[ Next; Exit after last ]

End Loop

Once the loop is complete, we want to make sure to close the data file, otherwise we’ll be left with copies open in memory:

Close Data File [ File ID: $fileID ] 

After the script has completed running, we have a text file on the desktop, named 18_dataFile.txt with the following contents:

{"first":"David","id":"6210418C","last":"Weiner"}
{"first":"Eleanor","id":"BCDDC24E","last":"Fulton"}
{"first":"Karl","id":"DFA3F16D","last":"Jreijiri"}
{"first":"Kimberly","id":"E5032653","last":"Carlson"}
{"first":"Matt","id":"1B412835","last":"Navarre"}
{"first":"Michelle","id":"B5B19E5D","last":"Davison"}
{"first":"Paul","id":"DF8DAE5C","last":"Beyrouty"}
{"first":"Shawn","id":"302FD01D","last":"Prado"}

That’s it! Once you’ve figured out the basics of inserting text into a file from a FileMaker script, the possibilities are endless. What use cases can you think of? Leave a comment below!