The next step in creating a dynamically generated organization chart is to transfer the generated JSON from the FileMaker database to the OmniGraffle document. This is accomplished by creating and running another FileMaker script that encodes and inserts the JSON into an Omni Automation script that writes the data into the OmniGraffle document.
Here is the script:
1 Perform the FileMaker script for generating JSON for the iterated set of database records. If you remember from the previous page, the resulting JSON is returned by the script when it has completed.
2 Instantiate a new variable named $encodedOrgData that uses the FileMaker GetAsURLEncoded function to percent encode the JSON and store the result in the created variable.
3 Instantiate a new variable named $dbName that uses the FileMaker GetAsURLEncoded function to percent encode the database file name and store the result in the created variable.
4 Instantiate a new variable named $omniscript that contains a percent encoded version of the Omni Automation script shown later on this page.
4 Execute the Omni Automation script by opening the script URL.
As shown by the illustration below, in the FileMaker script statement that creates the $omniscript variable that contains the encoded Omni Automation script (FileMaker script line 4 above), the placeholders DBTRANSFERDATA (below: script line 1) and DBFILENAME (below: script line 2) are replaced with the created FileMaker variables $encodedOrgData and $dbName respectively.
When the encoded Omni Automation script URL is executed, it will include the JSON data extracted from the database.
The Tagging Script
The executed Omni Automation script performs two tasks with the current OmniGraffle document:
Writes the decoded JSON data into the notes field for the currently selected canvas.
Assigns metadata keys and values for the top-level person (database record 1) to the currently selected solid graphic.
The following is a line-by-line explanation of the Omni Automation script:
1 In the encoded version of this script, the placeholder DBTRANSFERDATA is replaced with the percent encoded JSON database data. When the script URL is processed by OmniGraffle, it is decoded and the script variable JSONstr will be assigned the transfered JSON.
2 The script variable databaseFileName wil be assigned the name of the source FileMaker database.
3 A conditional statement that checks for a single selected solid to be assigned the values of the first database record. A solid is a graphic that can contain text or an image. If a single solid is not selected, then alert the script user.
9-28 The script statements for tagging document objects with the transferred data.
JSONstr = 'DBTRANSFERDATA'
databaseFileName = 'DBFILENAME'
if (document.windows[0].selection.solids.length != 1){
title = "SELECTION ERROR"
message = "Please select a single solid graphic."
new Alert(title, message).show(function(result){})
throw new Error(title)
} else {
cnvs = document.windows[0].selection.canvas
graphic = document.windows[0].selection.solids[0]
// retrieve data for top-level item
data = JSON.parse(JSONstr)
topLevelName = data[0]['Name']
topLevelID = data[0]['Employee ID']
// write name and ID to graphic
graphic.name = topLevelName + ' ' + topLevelID
// write name and ID to graphic text
graphic.text = topLevelName + '\n' + topLevelID
// write metadata to graphic user data
graphic.setUserData('Employee Name',topLevelName)
graphic.setUserData('Employee ID',topLevelID)
graphic.setUserData('Database Name',databaseFileName)
// write JSON string to canvas notes field
cnvs.background.notes = JSONstr
// confirmation alert
title = "DATA TRANSFERED"
message = "The organization data has been successfully transfered."
new Alert(title, message).show(function(result){})
}