Skip to main content
Inspiring
February 16, 2015
Answered

Copy layer contents to other opened docs - crashing inDesign

  • February 16, 2015
  • 1 reply
  • 366 views

Hi everyone,

As part of my work flow, I've written a script that's meant to copy to contents of a particular layer (in this case "palette") from whatever document you currently have active, over to any other opened documents that begin with the same name (ie. "order 2341 - large" would copy and replace the contents of the palette layer in "order 2341 - small, but the script would ignore any documents that don't begin with "order 2341").

The problem is that the pasteInPlace(); component of my script is crashing inDesign (I'm running CS 5.5). Strangely enough, if I stop the script before this point and paste the contents in manually it works just fine, so it seems it's only the scripted component that has the issue. Would anyone be able to take a look and see if there's something I'm doing wrong, or if this is just a strange ID bug?

//script to copy contents of a specific layer over to all other opened documents that begin with the same name

//written by Steve Eberhardt

var myDocument = app.activeDocument;

var docName = myDocument.name.match(/[^-]+/);

var paletteLayer = null;

var count = 0;

paletteLayer = app.activeDocument.layers.itemByName("palette");

var paletteObjects = app.activeDocument.layers.itemByName("palette").allPageItems;

app.select(paletteObjects);

app.copy();

var docs = new Array();

for (var i = app.documents.length - 1; i >= 0; i--) {

    docs.push(app.documents.name);

}

for (var i = 0; i < docs.length; i++) {

    app.activeDocument = app.documents.itemByName(docs);

    var otherDoc = app.activeDocument.name.match(/[^-]+/);

    if (docName.toSource() == otherDoc.toSource() && app.activeDocument != myDocument) {

        paletteLayer = app.activeDocument.layers.itemByName("palette");

        app.select(paletteLayer.allPageItems);

        try {

            var paletteGroup = new Array;

            paletteGroup = app.selection;

            app.activeDocument.groups.add(paletteGroup);

        } catch (_) {}

        paletteLayer.groups[0].remove();

        app.pasteInPlace();//this action crashes indesign

    }

}

app.activeDocument = myDocument;

alert("Copied " + myDocument.name.replace(/.indd$/, '') + " palette to " + count + " documents");

Thanks!

This topic has been closed for replies.
Correct answer Jump_Over

Hi,

I suggest to use duplicate() method instead of UI selecting - copying - pasting

Something like:

var

  myDocument = app.activeDocument,

  docName = myDocument.name.match(/[^-]+/),

  paletteLayer = app.activeDocument.layers.itemByName("palette"),

  paletteObjects = paletteLayer.pageItems.everyItem(),

  docs = app.documents.everyItem().getElements(),

  i, otherDoc;

for ( i = docs.length - 1; i >= 0; i-- ) {

    app.activeDocument = docs;

    otherDoc = docs.name.match(/[^-]+/);

    if (docName.toSource() == otherDoc.toSource() && app.activeDocument != myDocument) {

       paletteLayer = app.activeDocument.layers.itemByName("palette");

       paletteLayer.pageItems.everyItem().remove();

       paletteObjects.duplicate(paletteLayer);

       }

  }

app.activeDocument = myDocument;

Jarek

1 reply

Jump_Over
Legend
February 16, 2015

Hi,

What is weird ==> paletteObjects is an array of all pageItems across every pages of doc body.

However app.select() is limited to one spread only.

So your code supposes to give an error at line 11 (so much more earlier).

Jarek

MelchiarAuthor
Inspiring
February 16, 2015

Hi Jarek,

I hadn't considered that, but as I'm working exclusively with single page documents (in my case label designs), this isn't really an issue for my purposes.

Steve

Jump_Over
Jump_OverCorrect answer
Legend
February 16, 2015

Hi,

I suggest to use duplicate() method instead of UI selecting - copying - pasting

Something like:

var

  myDocument = app.activeDocument,

  docName = myDocument.name.match(/[^-]+/),

  paletteLayer = app.activeDocument.layers.itemByName("palette"),

  paletteObjects = paletteLayer.pageItems.everyItem(),

  docs = app.documents.everyItem().getElements(),

  i, otherDoc;

for ( i = docs.length - 1; i >= 0; i-- ) {

    app.activeDocument = docs;

    otherDoc = docs.name.match(/[^-]+/);

    if (docName.toSource() == otherDoc.toSource() && app.activeDocument != myDocument) {

       paletteLayer = app.activeDocument.layers.itemByName("palette");

       paletteLayer.pageItems.everyItem().remove();

       paletteObjects.duplicate(paletteLayer);

       }

  }

app.activeDocument = myDocument;

Jarek