Advertisement


Add text to InDesign with script?


Question

We have about 500 1 page InDesign files which each need a bit of text added to them in the format:

Completion Year xxxx

The text needs to be right justified and placed below other text boxes which hold information such as client and location, in the same font/format.

Ideally, a script which would add a text box with "Completion Year" and the appropriate date (perhaps taken from an excel or csv file?) would be great.

Failing that, a script that just inserts the text box with "Completion Year" in the correct location on the page and font would also be very helpful.

Any help/insight is appreciated.

ETA: We also have these as PDFs. Will it be easier to add this via script to a pdf vs. InDesign?

2015/02/23
1
0
2/23/2015 3:14:00 PM

Accepted Answer

Thats already a lot you want there. Did you do any prior research or tests?
Anyway I think this is what you want.
It reads a textfile you select. Add your content to each line. Use a paragraph style to set behaviour like alignment and fonts.

  var main = function() {
    // check for active document
    if (app.documents.length < 1) {
      // no doc
      return;
    } else {
      // sleect the file
      var file = File.openDialog("Select a textfile", "*.*", false);
      if (file === null) {
        // aborted by user
        return;
      }
      // read in the file line per line
      var lines = [];
      file.open('r');
      while (!file.eof) {
        lines[lines.length] = file.readln();
      }
      file.close(); // always close files

      var doc = app.activeDocument;
      // set the origin to the page
      var curr_origin = doc.viewPreferences.rulerOrigin;
      doc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;

      // the locations for the textframe
      var y1 = 0;
      var x1 = 0;
      var y2 = 100;
      var x2 = 100;

      // loop all pages
      for (var i = 0; i < doc.pages.length; i++) {
        var page = doc.pages[i];
        // get the content
        var content = "not enough lines";
        if (lines.length > i) {
          // we still have lines in the file
          content = lines[i];
        }
        // create the textframe
        var tf = page.textFrames.add({
          contents: content,
          geometricBounds: [y1, x1, y2, x2]
        });
        // apply a style by name
        // tf.paragraphs[0].appliedParagraphStyle = doc.paragraphStyles.item("myStyle");
      }
      doc.viewPreferences.rulerOrigin = curr_origin;

    }

  };
  main();
2015/02/24
1
2/24/2015 7:45:00 AM