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) { 
                }
            }
        }
    }

 

brian_p_dts
Community Expert
Community Expert
October 23, 2022

Apologies for the delayed reply. So, I think I have pinpointed the issue down to something specific. My main routine for duplicating files works, much like yours @femkeblanco . After I duplicate the main group items, I also target a specific object that can be either a PathItem or a GroupItem. If I exclude this routine from my script, everything works. If I include it, then, after successfully completing the first document, it throws the above-mentioned error on the second pass. 
I'm going to work to simplify the below code, but just wanted to close the loop on this. Thanks. 

 

var getCollarColors = function(designDoc, designLayer) {
    var collarColors = {
        main: {},
    };
    try {
        try {
            collarColors.main.obj = designDoc.layers[0].pathItems.getByName("Collar-Main");
            collarColors.main.isClipped = false;
        }
        catch(e) {
            try {
                collarColors.main.obj = designDoc.layers[0].groupItems.getByName("Collar-Main");
                collarColors.main.isClipped = true;
            }
            catch(e) {
                return undefined;
            }

        }
    } catch(e) {
        return undefined;
    }

        
    if (!collarColors.main.isClipped) {
        collarColors.main.color = collarColors.main.obj.fillColor;
        collarColors.main.isSpot = collarColors.main.obj.fillColor.typename == "SpotColor";
        collarColors.main.isGradient = collarColors.main.obj.fillColor.typename == "GradientColor"
    }
    if (collarColors.main.isClipped || collarColors.main.isGradient) {
        activeDocument = designDoc;
        collarColors.main.dupe = collarColors.main.obj.duplicate(designLayer);
    }
}

 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
October 24, 2022

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];

};

 

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?