Copy link to clipboard
Copied
Having a strange issue. I am getting the following error when duplicating a pathitem to a differenet document.
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('Nothi
...
Copy link to clipboard
Copied
Can you show what the loop looks like? Which is the "index" document? Is the other document constant?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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) {
}
}
}
}
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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?';
Copy link to clipboard
Copied
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);
}
}
Copy link to clipboard
Copied
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];
};
Copy link to clipboard
Copied
Thanks @m1b I refactored the code similarly to the way you suggested. Good call on initializing the undefined properties of the object. I was getting sloppy 🙂 I'll go ahead and mark this as resolved. I never could figure out what was going awry, but fixing up those objects to be a bit more clean seemed to have solved it.
Copy link to clipboard
Copied
Glad you got it working. Yeah at some point clean code becomes the lazy (ie. best) option. You don't have to do so much thinking when the code makes instant sense! All the best.