|
| 1 | +#!/usr/bin/env osascript -l JavaScript |
| 2 | +/* |
| 3 | +A JXA script for exporting OmniGraffle documents from the commandline |
| 4 | +
|
| 5 | +usage: ./ogexport.js <source> <format> <target> <property_1=value_1>...<property_n>=<value_n> |
| 6 | +
|
| 7 | +e.g. ./ogexport.js /Users/beb/dev/ogtool/JXA/test-data/test-data.graffle PNG /Users/beb/tmp/fat scale=2 resolution=3 |
| 8 | +
|
| 9 | +although scope is not really a "property", it can be still handed in as if it were one. |
| 10 | +
|
| 11 | +for some strange reaseon the full path to the og document and for the target is required |
| 12 | +
|
| 13 | +Accessing and setting app properties: |
| 14 | +OmniGraffle.documents[0].name() |
| 15 | +OmniGraffle.documents[0].canvases[0].name.set("foo") |
| 16 | +OmniGraffle.currentExportSettings.exportScale() |
| 17 | +OmniGraffle.currentExportSettings.areaType() |
| 18 | +OmniGraffle.currentExportSettings.areaType.get() |
| 19 | +
|
| 20 | +*/ |
| 21 | + |
| 22 | +function run (argv) { // eslint-disable-line no-unused-vars |
| 23 | + var parameters = setParameters(argv) |
| 24 | + |
| 25 | + // open app if not already open |
| 26 | + var appOpened = false |
| 27 | + if (!Application('OmniGraffle').running()) { // eslint-disable-line no-undef |
| 28 | + ObjC.import('AppKit') // eslint-disable-line no-undef |
| 29 | + $.NSWorkspace.sharedWorkspace.launchApplication('/Applications/OmniGraffle.app') // eslint-disable-line no-undef |
| 30 | + // console.log("opening app") |
| 31 | + appOpened = true |
| 32 | + } |
| 33 | + |
| 34 | + var OmniGraffle = Application('OmniGraffle') // eslint-disable-line no-undef |
| 35 | + OmniGraffle.includeStandardAdditions = true |
| 36 | + // console.log("opening " + parameters.source) |
| 37 | + var doc = OmniGraffle.open(parameters.source) |
| 38 | + |
| 39 | + // doc.export({as:"PNG", scope:"entire document", to:Path("/Users/beb/tmp/"), withProperties: {scale:1.0, resolution:1.94444441795}}) |
| 40 | + doc.export({ as: parameters.format, |
| 41 | + scope: parameters.scope, |
| 42 | + to: Path(parameters.target), // eslint-disable-line no-undef |
| 43 | + withProperties: parameters.properties }) |
| 44 | + |
| 45 | + doc.close() |
| 46 | + if (appOpened) { |
| 47 | + // close app if it was not already open |
| 48 | + OmniGraffle.close() |
| 49 | + } |
| 50 | + console.log('...done') |
| 51 | +} |
| 52 | + |
| 53 | +function exportName (filename) { |
| 54 | + if (filename.substr(-8, 8) === '.graffle') { |
| 55 | + return filename.substr(0, filename.length - 8) |
| 56 | + } else { |
| 57 | + return filename.concat('.out') |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function setParameters (argv) { |
| 62 | + // parse commandline parameters, set default properties |
| 63 | + |
| 64 | + // console.log(JSON.stringify(argv)) |
| 65 | + var source = argv[0] |
| 66 | + var format = argv[1] |
| 67 | + var target = argv[2] |
| 68 | + var myproperties = argv.slice(3) |
| 69 | + console.log('Exporting OmniGraffle document -------------------------------------------') |
| 70 | + console.log('Source: ' + source) |
| 71 | + console.log('Format: ' + format) |
| 72 | + console.log('Target: ' + target) |
| 73 | + console.log('Properties: ' + JSON.stringify(myproperties)) |
| 74 | + |
| 75 | + // set defaults for properties |
| 76 | + var properties = { |
| 77 | + scale: 1.0, |
| 78 | + resolution: 1.94444441795, |
| 79 | + borderamount: 0.0, |
| 80 | + copylinkedimages: true, |
| 81 | + drawsbackground: true, // true for transparent |
| 82 | + readonly: false, |
| 83 | + htmlimagetype: 'PNG', |
| 84 | + includeborder: false, |
| 85 | + includenonprintinglayers: false, |
| 86 | + useartboards: false, |
| 87 | + scope: 'entire document' |
| 88 | + } |
| 89 | + |
| 90 | + myproperties.forEach(function (element) { |
| 91 | + var res = element.split('=') |
| 92 | + properties[res[0]] = res[1] |
| 93 | + }) |
| 94 | + |
| 95 | + var parameters = { |
| 96 | + source: source, |
| 97 | + format: format, |
| 98 | + scope: properties.scope, |
| 99 | + target: target, |
| 100 | + properties: properties |
| 101 | + } |
| 102 | + |
| 103 | + var validAreaTypes = ['all graphics', 'current canvas', 'entire document', 'selected graphics'] |
| 104 | + var validFormats = ['PNG', 'JPG', 'GIF', 'BMP', 'TIFF', 'PSD', 'OOutline', 'Graffle', 'Visio', 'PDF', 'EPS', 'SVG', 'HTML'] |
| 105 | + |
| 106 | + if (!validAreaTypes.includes(parameters.scope)) { |
| 107 | + throw new Error(`"${parameters.scope}"" is not a valid scope`) |
| 108 | + } |
| 109 | + if (!validFormats.includes(parameters.format)) { |
| 110 | + throw new Error(`"${parameters.format}" is not a valid format`) |
| 111 | + } |
| 112 | + return parameters |
| 113 | +} |
0 commit comments