Skip to content
fabiantheblind edited this page Nov 19, 2015 · 4 revisions

###documents
Everything happens in documents it is something like the base container. You can access the document in different ways. Like so:

    /**
     *  Look for an active document
     *  without try catch it could produce an error
     *  
     */
    var doc;
    try{
            
        doc = app.activeDocument; // works with no doc
        
        var name = doc.name; // this is what throws an error
        alert("You have an active document.");
        }catch(error){
        // error is a variable that contains 
        // a message that can be read
        // alert(error);
        alert("You dont have an active document.");
        }

A document can be created with the values already set. This can be pretty handy but is hard to debug. You should always first test a property solo and than combine them in an object. So this code:

    var doc = app.documents.add();
    var val = 150;
    var prefs = doc.documentPreferences;
    prefs.pageWidth = val;
    prefs.pageHeight = val;

Could be minified to the following code. This is pretty handy, isn't it?

    app.documents.add({
    documentPreferences:{
        pageWidth:150,pageHeight:150
        }
    });

This means you can create a JSON object with the right syntax and than inject it into an app.documents.add()

!Achtung! This works only in InDesign CS5+

    /**
     *  You can create objects with the same structure as
     *   a documents to define values
     *  and than in inject them within a loop
     *  **!Achtung!** This works only in InDesign CS5+
     */
    
    main();
    // everyting is in here
    function main(){

    var doc_data = {
        "documentPreferences":{
            "pageWidth": 10 ,"pageHeight":10
            }
        };
    /*
    now you could do a lot of fancy stuff
    and still have your important data
    like the settings for the doc on the top of the script
    also you could place them into another
    file or something like this.
    */
    // now loop and create some documents
    var i = 1;
    var num = 13;
    var arr = new Array();

    while(i < num){
        // do something fancy here
        doc_data.documentPreferences.pageWidth+=10;
        doc_data.documentPreferences.pageHeight+=10;
        // now push it into an array
        arr.push(app.documents.add(doc_data));
       i++;
    }
    
    // add some text. just for fun
    add_text (arr);
    
    }
    
    /**
     *  This function adds text to an doc
     *  it takes an array of documents as argument
     */
    function add_text(arr){
        for(var j in arr){
        var d = arr[j];
        var pw = d.documentPreferences.pageWidth;
        var ph = d.documentPreferences.pageHeight;
        var gutter = pw/10;
        var gb = [gutter,gutter,ph - gutter, pw - gutter];
        var pg = d.pages.item(0);
            pg.textFrames.add({
                geometricBounds:gb,
                contents:"I'm doc number " + j
                });
            }
        }

Some useful code when working with lots of documents or developing scripts that creates documents.

Revert to last version:

    app.documents.everyItem().revert()

Close all open documents:

    // Like Uncle Ben saz: "With great power comes great responsibility!"
    app.documents.everyItem().close(SaveOptions.NO);

Clone this wiki locally