Exporting Images from FileMaker DB fields
Mac OS X — 3 Jun 2006 18:55 — 1516 days ago

I found out that exporting images from a FileMaker database is a major pain. Here’s what I came up with, I’d be interested to hear about better ways to do this.

Sice FileMaker’s export commands cannot export the container fields used to hold image fields, the goal is to get an XML export of the regular data and a bunch of image files in a directory. The filenames must correspond to some key field in the database so that the exported records can be linked with their images.

To export the images automatically from a FileMaker script, I had to combine FileMaker scripting, AppleScript and shell scripting.

The core of the solution is to use the clipboard to get at the image data:

  • iterate through the records
  • copy the content of the image container field to the clipboard
  • use the AppleScript file manipulation functions to open a file
  • write the clipboard contents to the file
  • close the file
  • move the file to the destination directory with a unique filename determined by a DB field

A difficult part is that the image filename is different for every record so the AppleScript code is dynamic and must be generated in a FileMaker script expression. The generated AppleScript code itself uses shell commands so there are three levels of quoting.

Another issue is that the code that writes the clipboard contents to the file worked fine in Script Debugger, but when used unchanged in FileMaker, FileMaker complained about the write to file command. Turns out that there is a clash because the write verb seems to mean something different in the context of AppleScript running inside FileMaker. I had to work around the issue by using the raw chevron AppleScript syntax for that line.

Here’s the native AppleScript for writing the image to a file:

set thePic to the clipboard try set thePic to JPEG picture of thePic set thePath to (path to home folder as Unicode text) & "picture001.jpeg" set fileRef to open for access file thePath with write permission set eof fileRef to 0 «event rdwrwrit» thePic given «class refn»:fileRef close access fileRef on error end try

Here’s the calculation expression to dynamically generate the AppleScript for moving the image to its final destination:

"set thePicturePath to (path to home folder as Unicode text) & \"picture001.jpeg\"" & ¶ & "set theDirPath to (path to home folder as Unicode text) & \"picture_export\"" & ¶ & "set thePosixPicturePath to POSIX Path of thePicturePath" & ¶ & "set thePosixDirPath to POSIX Path of theDirPath" & ¶ & "set thePosixDestinationPath to thePosixDirPath & \"/\" & \"" & items::id & ".jpeg\"" & ¶ & "display dialog thePosixDestinationPath" & ¶ & "do shell script \"test -f \" & thePosixPicturePath & \" && mv \" & thePosixPicturePath & \" \" & thePosixDestinationPath & \"; true\""

Wow, how FileMaker still sucks after all these years... It’s insane what has to be done to get something so easy to work.


Comments
Posted by Axel Roest on 17 Nov 2006 13:42

Rock on!
Thanks for the inside info. It took me a while to figure out the multiple quote levels as well while implementing a script to generate MS Word stationaries.

Posted by Micah Woods on 17 Nov 2006 23:09

Hello Marc, I put together a quick example file that shows an approach that uses "Export Field Contents". I had to rely on an image name field, not necessarily to generate the name, but to know the extension. As long as you can calculate the extension, then I think this is a reasonable work around although I agree image export would be nice.

http://homepage.mac.com/micah.woods/Misc/Image_Export.zip

Posted by Terry J Fundak on 17 Nov 2006 23:12

You can now do "exports" directly with FileMaker scripting. FileMaker has come a long, long way since the days this was not directly possible without a plug-in ( FileMaker 5, 6 ). With FileMaker 8 it is easy.

You can "export" the contains of a container field (any contents - whatever is in the container field - including just about any kind of file )

Create a FileMaker Script that looks like this:

Set Variable ( $filepath; Case( Get ( SystemPlatform ) = -2; "filewin:"; Get ( SystemPlatform ) = -1; "filemac:"; "file:" ) & Get( DesktopPath ) & YourDBName::t_filename )
-- t_filename should include the filetype .pdf, .png, etc.
Export Field Contents [ YourDBName::YourContainerFieldName; "$filepath"; Automatically open ]

This two line script will get you most of the way.

Have fun.

Terry

Posted by Jonathan Oxer on 27 Mar 2007 04:29

I needed to do the same thing with FMP v7 but I couldn't quite get Marc's code above to work for me, so I managed to simplify it a bit and achieve the same end result without having to do the step of generating shell dynamically. Details at

http://jon.oxer.com.au/blog/id/190

Powered By blojsom