javascriptprintingadobe-indesignextendscriptpreset

Extendscript InDesign CS6: Print using a print preset


This one should be simple enough for you Javascript/Extendscript wizards. I want to print a document using a print preset, while also specifying a page range (and maybe other options as well, after having chosen the preset). Consulting the InDesign CS6 JavaScript Scripting Guide it has this wonderful, detailed explanation of how to do it:

Printing with printer presets

To print a document using a printer preset, include the printer preset in the print command.

Wow. So descriptive and helpful. Um, can anyone help me make better sense of this?


Edit (01/21/2019)

I was asked how I was able to tell the script which pages I wanted to print. It turns out that this is not stored in the PrinterPreset.

Document has a property called printPreferences which allows access to the PrintPreference object. This object allows the developer to set the pageRange by either specifying a PageRange enum or a String with the page range (with "1" being the first page).

So, to illustrate:

var document = app.activeDocument; // Presumes the document you want to print is already open.

document.printPreferences.pageRange = PageRange.ALL_PAGES; // Will print all pages in the document.
document.printPreferences.pageRange = "1-3,7,10,12-15" // Prints pages 1, 2, 3, 7, 10, 12, 13, 14, and 15.

Note: PageRange.SELECTED_ITEMS seems to only be used for exporting items, not printing (since the PageRange enum is used for both operations). I have not tested this, however.

There are many other PrintPreference properties that can be set before document.print() is called, so it's worth looking them up.


Solution

  • The app.print() method can take a PrinterPreset object as one of it's arguments. Here's a link to a reference of the method for more information.

    Here's an example (untested):

    var doc = app.activeDocument;
    
    var file = File(doc.fullName);      // Get the active document's file object
    
    var preset = app.printerPresets[0]; // Use your printer preset object
    
    app.print(file, null, preset);
    

    The InDesign reference lists the app.print() method more or less like this:

    void print (from: varies[, printDialog: bool][, using: varies])
    Prints the specified file(s).
    
    Parameter    Type                Description
    from         Array of Files      One or more file paths. Can accept: File or Array of Files.
                 File
    printDialog  bool                Whether to invoke the print dialog (Optional)
    using        PrinterPreset       Printer preset to use. Can accept: PrinterPresetTypes enumerator or PrinterPreset. (Optional)
                 PrinterPresetTypes
    

    The first information listed is the return value of the method, void, in this case which means it doesn't return anything.

    The next information listed is the name of the method print followed by it's named parameters: from, printDialog, and using as well as what each parameter type should be.

    The parameters are also listed in the chart for further explanation. The from parameter expects an object of type File for example. So, in the example above, I make an "instance" of a File object by calling it's constructor: var file = File(doc.fullName);. Then I get an already existing PrinterPreset object: var preset = app.printerPresets[0];. Finally, I pass each object to the function inserting null for the middle variable (since it's optional, I just decide to ignore it): app.print(file, null, preset);.