Skip to main content
Participant
October 19, 2022
Question

Script - New Document

  • October 19, 2022
  • 3 replies
  • 240 views

Could you give me an advice please?

 

Is there a Script that would do - create a new document with a specific size, default name?

For example - Create a new file with dimensions 210x297mm.

 

Thank you.

This topic has been closed for replies.

3 replies

Loic.Aigon
Legend
October 19, 2022

What's the need precisely? Is the create document routine part of a bigger script? 

Because document presets would do the same without even coding. 

rob day
Community Expert
Community Expert
October 19, 2022

Hi @grafika75532081 , Also, in case you don’t know the preset name, or it might not exist, try this. It makes a temporary preset with your 210mm x 297mm dimensions—sets facing pages to true, and  margins to 25mm. You can check the API for other properties:

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DocumentPreset.html

 

 

 

var dn = "My New Document";
app.scriptPreferences.measurementUnit = MeasurementUnits.MILLIMETERS;

//A temporary preset with a unique name
var currdate = new Date
var ct =  "Temp-" + currdate.getTime();
var dp = makeDocPreset(ct);
dp.properties = {facingPages:false, pageHeight:297, pageWidth:210, pageOrientation:PageOrientation.PORTRAIT, top:25, bottom:25, left:25, right:25}

//make the new doc
var doc = app.documents.add(true, dp);
doc.name = dn
doc.viewPreferences.properties = {horizontalMeasurementUnits:MeasurementUnits.MILLIMETERS, verticalMeasurementUnits:MeasurementUnits.MILLIMETERS}

//remove the temp preset
dp.remove();

/**
* Makes a new document preset 
* @ param preset name name 
* @ return the new preset 
*/
function makeDocPreset(n){
    if (app.documentPresets.itemByName(n).isValid) {
        return app.documentPresets.itemByName(n);
    } else {
        return app.documentPresets.add({name:n});
    }
}

 

 

 

BarlaeDC
Community Expert
Community Expert
October 19, 2022

Hi,

 

You can call app.documents.add() - documentation here - https://www.indesignjs.de/extendscriptAPI/indesign-latest/#Documents.html

one of the params can be a document preset that allows you to set what you want - https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DocumentPreset.html#d1e316149

 

Using these together you should be able to do something like

// you might want to pick a specific one, or create it from scratch
var newPrefs = app.documentPresets.anyItem();

newPrefs.pageHeight = "297";
newPrefs.pageWidth = "210";

app.documents.add ( true, newPrefs);