Skip to main content
brian_p_dts
Community Expert
Community Expert
October 16, 2022
Answered

Using pathItem.duplicate(Exception has occurred: 1200 an Illustrator error occurred: -108 ('ÿÿÿ”'))

  • October 16, 2022
  • 3 replies
  • 603 views

Having a strange issue. I am getting the following error when duplicating a pathitem to a differenet document. 

documents[0].pathItems[0].duplicate(documents[1].layers[0]);
The operation is being called in a loop, where I am opening document[1] and operating it, before closing it and moving onto the next doc[1]. The duplicate operation works correctly the first time I perform this function, but then fails on the second one, prompting this error. I have tried setting the activeDocument to doc[0] before duplication, but that doesn't work. 
Any thoughts? Appreciate it. 
 
This topic has been closed for replies.
Correct answer m1b

Hi @brian_p_dts, I've rejigged your code, to my taste, and might be not appropriate to your exact needs, but I hope it will give us a platform to understand your problem (which I don't understand yet). Can you have a look and see if you can glean anything further?

- Mark

 

 

var designDoc = app.activeDocument,
    designLayer = designDoc.layers[0];

var collarColors = getCollarColors(designDoc, designLayer);

if (collarColors != undefined)
    alert('We have collarColors');
else
    alert('Nothing');


function getCollarColors(doc, layer) {

    // I don't like the try-catch approach,
    // preferring to offload to a fault-tolerant
    // function getByName. MUCH easier to read.
    var collarItem = getByName(layer.pathItems, "Collar-Main");

    if (collarItem == undefined)
        collarItem = getByName(layer.groupItems, "Collar-Main");

    if (collarItem == undefined)
        return undefined;

    // I think it is good form to fill out
    // your object notation even if many
    // properties are undefined, so that when
    // you go back to look over your code
    // it is easy to see what you intended.
    // I also suggest you don't create unnecessary
    // structure yet (eg. collarColors), and give
    // more explicit property name (eg. mainCollar vs main)
    var mainCollar = {
        obj: collarItem,
        color: undefined,
        isSpot: undefined,
        isGradient: undefined,
        dupe: undefined
    };

    // target collarItem not collarColors.main
    // 'clipped' no 'isClipped'
    if (!collarItem.clipped) {
        mainCollar.color = collarItem.fillColor;
        mainCollar.isSpot = collarItem.fillColor.typename == "SpotColor";
        mainCollar.isGradient = collarItem.fillColor.typename == "GradientColor"
    }

    if (collarItem.clipped || mainCollar.isGradient) {
        app.activeDocument = doc;
        mainCollar.dupe = collarItem.duplicate(layer);
    }

    // can give the structure here
    // (if needed, which I doubt)
    return { mainCollar: mainCollar };

};


function getByName(styles, name) {

    for (var i = 0; i < styles.length; i++)
        if (styles[i].name == name)
            return styles[i];

};

 

3 replies

brian_p_dts
Community Expert
Community Expert
October 16, 2022

It's a pretty complicated bit of code, but here are the relevnt snippets. The template doc is closed at the end of processDocs execution.

 

try {
        var designDoc = app.documents[0];
    } catch(e) {
        alert("Open the design doc and try again. It should be the only doc open.");
        return;
    }

var con = control(sizeObjs);
    if (con == null) return;
    var p = con.length;
    var x = 0;
    for(x; x < p; x++) {
        templateDoc = app.open(templateFiles[x]);
        processDocs(designDoc, templateDoc, con[x], sizeObjs[x]);
    }

var processDocs = function(designDoc, templateDoc, con, sizeO) {
 
     //move grouped items from Design doc to own layer on template doc
    try {
        var designLayer = templateDoc.layers.getByName("Design");
        designLayer.move(templateDoc, ElementPlacement.PLACEATEND);

    } catch(e) {
        var designLayer = templateDoc.layers.add();
        designLayer.name = "Design";
        designLayer.move(templateDoc, ElementPlacement.PLACEATEND);
    }
    app.selection = null;
    
    var gs = designDoc.layers[0].groupItems;
    var i = gs.length;
    var bckrx = /back/gi;
    var frx = /front/gi;
    var srx = /sleeve/gi;
    var tmpG;
    while(i--) {
        if (bckrx.test(gs[i].name)) {
            tmpG = gs[i].duplicate(designLayer);
            tmpG.hidden = gs[i].hidden;
            if (tmpG.pageItems) {
                try {
                    hidePathItems(tmpG.pageItems, gs[i].hidden);
                } catch(e) { 
                }
            }
        }

        if (frx.test(gs[i].name)) {
            tmpG = gs[i].duplicate(designLayer);
            tmpG.hidden = gs[i].hidden;
            if (tmpG.pageItems) {
                try {
                    hidePathItems(tmpG.pageItems, gs[i].hidden);
                } catch(e) { 
                }
            }
        }
        if (srx.test(gs[i].name)) {
            tmpG = gs[i].duplicate(designLayer);
            tmpG.hidden = gs[i].hidden;
            if (tmpG.pageItems) {
                try {
                    hidePathItems(tmpG.pageItems, gs[i].hidden);
                } catch(e) { 
                }
            }
        }
    }

 

femkeblanco
Legend
October 16, 2022

If I remove the undefined variables and strip it down to the most basic elements of the duplication, it works as expected.

// 1 doc open with 1 group
var designDoc = app.documents[0];
for(x = 0; x < 3; x++) {
    templateDoc = app.documents.add();
    processDocs();
}
function processDocs() {
    var designLayer = templateDoc.layers.add();
    designLayer.name = "Design";
    var gs = designDoc.layers[0].groupItems;
    var i = gs.length;
    var tmpG;
    while(i--) {
        tmpG = gs[i].duplicate(designLayer);
        }
    templateDoc.close();
}

 

m1b
Community Expert
Community Expert
October 16, 2022

So the logic there is sound, great work @femkeblanco. Perhaps Brian, your templateDoc is the problem. When it crashes, can you interrogate templateDoc in the debugger? Make sure it is the document you expect and has the correct file associated with it. Just throwing ideas around...

- Mark

 

P.S. another idea: try to create a pageItem in templateDoc and see if that works; something like:

var tf = designLayer.textFrames.add();
tf.name = tf.contents = 'did this work?';
m1b
Community Expert
Community Expert
October 16, 2022

Hi @brian_p_dts, are the documents already open? If so, do the loop backwards, starting with the last document. And your source document must be document 0.

If not, get a reference to the opened doc and use that rather than documents[i].

A robust approach might be to write a little function that selects a document by looping over app.documents and comparing (for example) doc.name or doc.filePath.

Otherwise, posting a pared down script would help. 
- Mark

femkeblanco
Legend
October 16, 2022

Can you show what the loop looks like?  Which is the "index" document?  Is the other document constant?