Batch exporting binary files (e.g. images)

news · 5 years ago
by Petr Hamernik, David Pech


In Instant JChem you can create binary fields and load binary files into them (for example images or pdf). This can be a quite useful feature for academics and teachers, as “a picture is worth more than a thousand words”. Pictures are also quite informative in case of various studies and clinical trials. Instant JChem’s binary field is then ideal for tracking a progress of for example growth or regression of a tumor, under the influence of various chemotherapeutics. But what if we need to get hands on the pictures for whatever reason and we do not want to have them in the database only. At the moment there is no way to export the batch of images directly from Instant JChem. We must resort to Groovy scripting which offers up extension of Instant JChem’s basic functionality. For further details, please refer to the documentation.

The following script can help with this. Under the data tree which you want to export from, create a new script.


Copy the following to the newly created script:

[groovy firstline="1"] import def ety = dataTree.rootVertex.entity def edp = ety.schema.dataProvider.getEntityDataProvider(ety) // Modify the name of the directory. // Images or other binary files will be exported into it. def targetDir = "c:\\temp\\exported_images" // for UNIX systems “/tmp” for example // Name of image field imageField = ety.fields.items.find { == "Image" } // Name of text field which has values used as file names filenameField = ety.fields.items.find { == "Filename" } ids = edp.queryForIds(DFTermExpression.ALL_DATA, SortDirective.EMPTY, env) ids.each { id -> values = edp.getData([id], env)[id] filename = values[] byte[] image = values[] File target = new File(targetDir + File.separator + filename) Files.write(image, target) } [/groovy]

This script exports all images in the table. It can be easily modified to export only current results.

A sample data project including working script and demo data is attached to this post. Download