Exporting Documents with JXA


#1

I’m currently experimenting with porting my suite of tools for OmniGraffle to JXA, and I must admit I’m having a harder time here than I thought. I have hacked together a very simple exporter service:

Omnigraffle Export Service
 var app = Application.currentApplication()
app.includeStandardAdditions = true

function run(input, parameters) {
    OmniGraffle = Application('OmniGraffle')
    OmniGraffle.includeStandardAdditions = true
    const result = input.map(exportDocument);
    const dialogText = "Exported documents \n\n" + (result.reduce(combineOutput, ""))
    app.displayDialog(dialogText)
    return result
}

function combineOutput(result, tuple) {
    return result.concat(tuple[0], "\n")
}

function exportDocument(file) {
    var doc = OmniGraffle.open(file)
    var target = exportName(file+"")
    doc.export({as:"PNG", 
                scope:"entire document", 
                to:Path(target), 
                withProperties: {scale:1,
                                 resolution:2
                                }
              })
    doc.close()
    return [file, target]
}

function exportName(filename) {
  if (filename.substr(-8, 8) === ".graffle") { 
    return filename.substr(0,filename.length - 8)
  } else {
    return filename.concat(".out")
  }
}

I would expect this code to export all of the contents of the documents I selected to PNG, however in many cases, some shapes are not exported. This appears to be random, when I run the script again on the same document, sometimes all shapes are exported. Do I miss something obvious, or is this maybe a bug in 7.10.1 (v195.9 r327375)?

The settings for resolution appear to no longer create the same result as in OmniGraffle up to v7.7, where with a value of 1.94444441795 I was able to render to 140dpi, but through JXA with v7.10.1 the result I get is about 300dpi.

Also, for some strange reason when I try to access OmniGraffle.currentExportSettings.resolution() in the JS debugger, I get an error “AppleEvent handler failed.”, while other properties, e.g. OmniGraffle.currentExportSettings.areaType() or borderAmount() yield correct values. Is the documentation misleading, or am I missing something? It appears to me the way JXA is designed interferes with inspecting objects through Object.getOwnPropertyNames().

I wonder if anyone else was successful in scripting export through JXA and might be so kind and point me into the right direction.