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

how to use the new data file script steps in FileMaker 18

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.

Script steps are the set of commands, which are in the ScriptMaker feature, to be added to a script, in order to automate tasks or operations inside of your FileMaker app.

Claris International (source)

What does script steps include?

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 FileGet 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 ] 


  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:


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? Feel free to contact us if you have a project in mind!

*This article was originally written for AppWorks, which has since joined Direct Impact Solutions. This article is intended for informative purposes only. To the best of our knowledge, this information is accurate as of the date of publication.

Leave a Comment

Your email address will not be published. Required fields are marked *