Thursday, September 29, 2016

Exporting OLE Objects as PDFs from FileMaker Container Fields using a Macro Program

This post discusses an automated method for exporting OLE objects embedded as PDFs in FileMaker Pro container fields using a Windows macro program, Auto Hot Key and script (FMP_OLE_PDF_export.ahk). This method is useful when converting files from .fp7 to .fmp12 file format.

FileMaker Container Fields and Microsoft's OLE 


FileMaker Pro .fp7 file formats on the MS Windows platform, allow the user to "insert object" when saving PDFs into container fields. To view the embedded PDFs, double clicking in the container field will open the PDF in Adobe Acrobat Reader.  One problem users and developers face when converting from the .fp7 to .fmp12 file format is that FileMaker has deprecated the ability to embed objects into container fields as an OLE object types.

A bigger problem exists when converting files to the new file format, the OLE objects seem to appear nonexistent within the container fields and users cannot export or interact with the contents of the container fields; a disconcerting fact if you have thousands of PDFs. The only way to export the contents of the container field is by manual repetitive process or thru automation.

When I posed the OLE PDF container field problem to several developers, they recommended learning MS Visual Basic, Autoit and Auto Hot Key for automating repetitive tasks. These software programs, have scripting languages which allow you to write and execute scripts that automate tasks amongst other things. When I first looked into these, I have to admit that I was lazy because I didn't want to learn another language just to export PDFs out of a FileMaker container field. So I tried using several Macro Recording programs. These, often free, programs allow you to press a record button to capture your keyboard and mouse clicks. You may then save your recording and press a playback button to repeat the task that has been recorded. Long story short, while they held promise for their ease of use they lacked what I required most which was finesse and control.



What is Auto Hot Key?



To solve the problem of exporting a large volume of PDFs using repetitive keyboard commands, I decided to code an Auto Hot Key script to automate the process.

What is Auto Hot Key? From the web site:

"AutoHotkey (AHK) is a free, open-source macro-creation and automation software for Windows that allows users to automate repetitive tasks."

AHK has an excellent tutorial that I worked through in an afternoon to understand how it could help me automate keyboard and mouse clicks in context to my problem.

Preliminary Steps for Success 


Install the AHK Software Program


Install the AHK macro software program on your computer per instructions found on their website. I recommend that you install the 64 bit version as it will run scripts much faster than the 32 bit version.

Use a Text Editor


I use Notepad++ , a free text editor to read/write/edit my .ahk scripts.

Create a Folder for Saving PDFs


Create a folder named "OLE PDFs your file name" and save it to your desktop or documents folder. This folder will contain the exported PDFs.

Preparing the FileMaker Pro File

  1. On your local computer, save a copy of your FileMaker Pro file with the OLE PDF objects. Name it to include the word "Master". E.g., "YourFileName Master.fp7"
  2. Open the master copy and set auto-login using [Full Access] privileges.
  3. Turn off or remove any scripts that are triggered to run on opening the file.
  4. Remove any file references in External Data Sources.
  5. Create a new calculation field named something like "container_file_name" that uses the "GetAsText(  )" function. The calculation, GetAsText( your_container_field_name  ) should return the file name in the container field, and for those container fields that have an OLE object embedded in it it returns a "?" ( empty container fields should return a Null or blank value).
  6. Perform a find using the wildcard character  "*" (asterisk) in the container_file_name field you defined above. This will return all the records that have an object embedded in the container field.
  7. Optional - Delete the omitted records (they won't have object in their container field) in the find above. This can be handy when you're working with thousands of records.
  8. Create a new layout named OLE PDF Export based on the table occurrence with the container field in it. ***IMPORTANT: This is the layout the AHK script will be using to export the PDF files.
  9. On the OLE PDF Export layout add only these 2 fields: 
    1. a unique record Id field which has a value that was generated as an auto-enter, serially incrementing number ( i.e., something that can uniquely identify the record with the container field in it).
    2. the container field
  10. Set the tab order so that the unique Id field is 1 and the container field is 2.
  11. Save a copy of the Master file. Name it something like "YourFileName Working.fp7". This is the file you will be performing the automated tasks using the AHK script. If something happens to this file, e.g., crashes and needs to be recovered, or container contents is removed or a record gets accidentally deleted you'll have a prepped file ready to go again!

Preference Settings for Adobe Acrobat Reader


Adobe Acrobat Reader will be used to open and save the embedded PDFs. It too will be called from the AHK script. Make sure to include these settings in Adobe Acrobat Reader preferences section for optimal results. (I made some changes from the default settings, but couldn't remember what they were during testing, so I've included screen shots of my current settings.)









.ahk - Anatomy of a Automated Repetitive Task Script


The purpose of the AHK script (FMP_OLE_PDF_export.ahk) is to Loop through the found set of records in the FileMaker file, copying the Unique Id value, opening the OLE PDF objects (saved in FMP container fields) in Acrobat Reader and saving the PDFs to a folder. It's that simple.

The AHK script that I've included in the post has been commented fairly well. Below is a high level description of what it does.


  1.  line 15: "Esc" key is set as the abort key. Pressing this key stops the script.
  2.  line 17: "Windows" + "j" key executes the script.
  3.  line 19:  The Clipboard variable is cleared of its contents.
  4.  line 20:  WinActivate, FileMaker Pro Advanced  FileMaker Pro Advanced is activated. The program has focus now.  (***Important*** you should change this to FileMaker Pro if you have this version)
  5.  line 28:  The number of times the Loop script step is set to loop and it begins the process of looping through the FileMaker pro records.
  6.  line 33: Tab to the Unique Id field.
  7.  line 36-41: Copy the Unique Id field value.
  8.  line 45: sets the PDFfileName variable to what was copied in the clipboard in lines 36-41.
  9.  line 54: sends a second tab to go to the container field.
  10.  line 60: sends a right click and selects Open Acrobat Document
  11.  line 68: Adobe Reader has focus now and the PDF file that has been opened in FileMaker is in displayed.
  12.  lines 91-98: interacts with the save window in Acrobat Reader sends a paste command and saves the PDF document.
  13.  line 105:  closes the Acrobat Reader program.
  14.  line 113:  FileMaker Pro (Advanced) is activated and now has focus
  15.  line 118: send go to next record keys
  16.  line 120: ends the loop

Anything in between "/*" and "*/" or following ";" are comments.

Running Auto Hot Key Script with FileMaker



  1. In your FileMaker database, perform a find to return a found set of records that contain PDFs. Use the File Name field to perform your search, or another method, to retrieve the found set. 
  2. Go to the first record, copy the name of the Unique Id field.
  3. Right click on the container field and select Open Acrobat Document.




  1. In Acrobat Reader, save the PDF file to the folder "OLE PDFs your file name" you created above. This will set which folder Acrobat Reader will save the PDFs to when running the AHK script.
  2. ***Important*** Switch back to FileMaker and make sure to Omit the record you just manually processed. 
  3. Note the total number of records in the left side of the status tool bar area. Use this value to set the Number of FMP records to loop thru line of code in the Export OLE PDF from FMP.ahk script.




  1. Open the Export OLE PDF from FMP.ahk script using a text editor.
  2. Edit the found set of records on line 28 Loop n, where n is the found set of records to loop thru.


  1. Save the AHK script.
  2. Double click the AHK script to start it. It should now be running. You may confirm this by opening Windows Task Manager to see if the process is running.
  3. Switch to FileMaker and go to the first record in the found set. The AHK script works on the records in Form View.
  4. Press the "Windows" + "j" keys. This auto-hot key combination will start the process of exporting, naming and saving your PDFs.
If everything goes well, you should be able to walk away while your script does all the repetitive keyboard commands. If you notice a problem, you may abort the process by clicking the "Esc" key. This will stop the script.

Importing PDF Files into the Converted FileMaker File

Once all of the PDFs have been exported and saved to the PDF folder, OLE PDFs your file name, the files in the folder can be imported into a new FileMaker Pro file. The new FileMaker Pro file needs to be in the .fmp12 file format and at the time of this writing you may use FileMaker Pro versions 12, 13, 14 and 15.

  1. Open FileMaker Pro in one of the versions mentioned above. 
  2. Create a new file/database and name it to something like OLE PDFs
  3. Define 4 fields:
    1. Image (container)
    2. File Name (text)
    3. File Path (text)
    4. Image Thumbnail (container)
  4. Go to File > Import Records > Folder...
  5. Click the Specify... button and select the OLE PDFs your file name folder.
  6. Click OK and then Continue...

  7. Although we defined 4 fields in our new file, the only values that need to be imported into the new file are the Image and the File Name field. The other two are optional.
  8. Make sure the field names are matched up and then click Import.
You now have a FileMaker file with PDFs that originated from the original .fp7 version. The File Name field will contain the Unique Id value used in your original pre-converted file. The File Name field will be used as a match field when importing the PDFs into your final converted file.

If you haven't already done so, convert your original file (.fp7) to the new file format (.fmp12). and import the PDF files using the File Name field (in the file OLE PDFs) and the Unique Id field (final converted FileMaker Pro file) in both files as the match key and performing an update.

  1. Open FileMaker Pro files OLE PDFs and the final converted file. 
  2. Go to a layout and Show all records in the OLE PDFs file.
  3. In the final converted file, go to File > Import Records > File...
  4. Open the OLE PDFs FileMaker Pro file.
  5. In the Import Field Mapping Window, for the Import Action, select "Update Matching records in found set".
  6. Use the Field Name field in the OLE PDF Import file and match it with the Unique Id field in the final converted FileMaker file. Here's how to do an import update: Methods of Importing Data into FileMaker


  7. Click Import

The records in your final converted file should now have the PDF files embedded in them. You should be able to open and view the PDFs, depending how you've configured the field for PDF interaction.


Some Thoughts About the Process


Testing and macro execution were done on Windows 7 OS. I shared my code with another FileMaker developer who used the setup in Windows 10. They claimed success in processing over 3000 PDFs embedded in container fields using the AHK script.

I found that the 64 bit version is faster than the 32 bit version. Therefore if you can, install the 64 bit version of AHK.

I haven't tried running this process on a file hosted on a server, if anything I'm hypothesizing that there may be some latency with the process because of the connection to the server.

You may speed up or slow down the execution of the process by tweaking the "Sleep" script step in  the AHK script. "Sleep" is the equivalent to the "Pause/Resume Script [for duration]" script step in FMP.  The Sleep script step values are set to milliseconds. I've observed that setting lower values in the "Sleep" script step, thereby speeding up the script, may cause the process to trip up on itself. During the testing phase, I empirically determined where to place the Sleep script steps in the script and set the values accordingly. The process has been tuned and optimized for my computing environment. Yours may be different and I encourage you to experiment.

I've processed thousands of PDFs and discovered that processing 200-300 records at a time works best.

I've also learned that the health or integrity of the file is important. Records which began their lives in .fp3, .fp5 files and were converted to .fp7 files were more difficult to process. FileMaker applicatoin would crash and the file would become damaged, this is why I strongly recommend having copies of your original file to work on. The AHK script had a difficult time opening the embedded files in Adobe Acrobat in older records. These records were singled out and exported manually. Sometimes container fields had corrupted files embedded in them. This would cause FileMaker to crash using the AHK script. 

Saturday, August 3, 2013

Capturing a Device's Persistent ID at Login

If you're developing a commercial FileMaker product you'll want to think about how to set limits on the number of permitted devices (iOS for mobile) that may access your solution. Part of solving this problem also touches on the issues of software piracy and registration.

The example file below revolves around the use of FileMaker 12's native function Get(PersistentId) to log and record each device (computer and mobile devices such as an iPhone or iPad) which may access the file. According to FileMaker Inc. the description of the Get(PersistentId) function is:

"Returns a unique, unchanging identifier for the computer on which FileMaker Pro is running or the device on which FileMaker Go is running, in the form of a 32-digit hexadecimal string." 

The example FileMaker file contains 2 tables, Registration (REG__Registration) and Device (DVC__Device). There is a one-to-many relationship between the Registration table and the Device table.

A Test Persistent ID button exists on the main layout (REG__Registration_detail) that simulates login to the file. Each time this button is clicked the logic is tested to determine:

  1. whether there's a record for the device's Persistent ID that's accessing the file AND
  2. if the device's Persistent ID has not been added in the Device table, has the solution met it's device storage limit?
If you're testing out the example file from your computer, you'll always get/return the same Persistent Id, so I've included a Change PersistentID button which is displayed in each portal record. Clicking the Change PersistentID button changes the persistent Id and will allow you to continue clicking the Test Persistent ID button and adding new records to the Device table until the limit is reached.

Of course, as a developer you'll deny users any access to the Registration and Device tables. You'll also have to think about how to update the device records if a user (perhaps someone that is a legitimate user) accesses the file with a new device. One hint would be at storing the Persistent IDs in a remote MySQL table and using calls, via PHP script from the FileMaker file to the MySQL table to record information about each device, but this is beyond the scope of this blog entry.

File: Device_Registration.fmp12.zip

UPDATE: 9-16-2013

This file may not work properly with iOS 7 as there is a bug with the Get(PersistentId) function: http://bit.ly/16dkdCX

Thursday, April 5, 2012

FileMaker Pro 12 Released

The highly anticipated new version of FileMaker 12 has been released. Below are some great links describing some of the new features of the product:

New features:
http://www.filemaker.com/12help/html/fmp_basics.3.2.html#1081648

http://help.filemaker.com/app/answers/detail/a_id/10002

Matt Petrowsky introduces FileMaker 12 with a great video:
http://www.youtube.com/watch?v=D7VHVeuNB1w&feature=youtu.be

http://www.filemaker.com/products/filemaker-pro/whats-new.html

Also FileMaker Go, FileMaker Pro's mobile counterpart for the iPad and iPhone are free in the App store:

http://itunes.apple.com/app/filemaker-go-12-for-iphone/id508238074?mt=8

http://itunes.apple.com/us/app/filemaker-go-12-for-ipad/id508235916?mt=8