Search code examples
javascript-automation

How to make new document with JXA?


How to make new document and close? Need this to workaround apple automation buggy insanity. What I try is this:

var app = Application('Keynote')
var doc = app.make(new document)  // How to write this correctly?
doc.close({saving: 'no'})

Solution

  • AppleScript and JavaScript syntax is completely different. You have to think more in terms of JavaScript

    For example JXA doesn't understand make(new).

    You have to create an instance from the class name (note the uppercase spelling) and then call make().
    Actually the var keywords and the trailing semicolons are not needed.

    keynote = Application('Keynote')
    keynote.activate()
    newDocument = keynote.Document().make()
    

    Within the parentheses of Document() you can pass parameters similar to AppleScript’s with properties for example

    newDocument = keynote.Document({
        documentTheme: keynote.themes["Gradient"], 
        width:1920, 
        height:1080
    })
    

    AppleScript’s multiple word properties like document theme are written as one camelCased word.

    To close the frontmost document write

    keynote.documents[0].close()