Skip to main content
Participant
January 12, 2023
Answered

Need help with error that never occurred in the past with this script for Illustrator 2023

  • January 12, 2023
  • 2 replies
  • 409 views
When we run this script it generates an error at line 16 which is the 
doc.layers.getByName(name).visible = false;
and I am not sure why this is happening. Below is the script and it is created as an action item and a specific folder is identifed to run against which then creates new files and places the in the two designated folders at the top of the script.
 
#target illustrator
function test(){
 
var folder_1 = Folder("~/Desktop/Header CAD");
var folder_2 = Folder("~/Desktop/Clean CAD");
 
app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
 
function revealAllLayers(doc) {
for (var i = doc.layers.length - 1; i >= 0; i--) {
doc.layers[i].visible = true;
}
};
 
function hideLayer(doc, name) {
doc.layers.getByName(name).visible = false;
};
 
function exportMyPng(dest, doc, props) {
if(props.hasOwnProperty("antiAliasing")){
switch(props.antiAliasing){
case "ARTOPTIMIZED" : {
app.preferences.setIntegerPreference("plugin/PNGFileFormat/AntiAlias", 1);
break;
}
default : {
break;
}
}
}
var pngOpts = new ImageCaptureOptions();
pngOpts.antiAliasing = true;
for (var all in props) {
if (pngOpts.hasOwnProperty(all) && all != "antiAliasing") {
pngOpts[all] = props[all];
}
}
doc.imageCapture(File(dest + "/" + doc.name.replace(/\.\w+$/, props.extraStuff + ".png").substr(props.charsOffStart)), doc.visibleBounds, pngOpts);
};
 
 
var doc = app.activeDocument;
revealAllLayers(doc);
hideLayer(doc, "Materials");
exportMyPng(folder_1, doc, {
transparency: false,
antiAliasing: "ARTOPTIMIZED",
resolution: 300,
extraStuff: "_header",
charsOffStart: 0
});
 
hideLayer(doc, "Header");
hideLayer(doc, "Detail Artwork");
exportMyPng(folder_2, doc, {
transparency: false,
antiAliasing: "ARTOPTIMIZED",
resolution: 300,
extraStuff: "",
charsOffStart: -9
});
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
 
};
test();
This topic has been closed for replies.
Correct answer Charu Rajput

@Leon_Lincoln 

This error is when layer does not exists with the passed name. There are three layers name used in the code, Materials, Header and Detail Artwork. It is possible that layers with these names or one of the name does not exists in your document.

 

hideLayer(doc, "Materials");
hideLayer(doc, "Header");
hideLayer(doc, "Detail Artwork");

2 replies

m1b
Community Expert
Community Expert
January 13, 2023

By the way, this is one way to handle the error:

function hideLayer(doc, name) {
    var layr = getThing(doc.layers, 'name', name);
    if (layr != undefined)
        layr.visible = false;
};

function getThing(things, key, value) {
    for (var i = 0; i < things.length; i++)
        if (things[i][key] == value)
            return things[i];
};

 I will only try to access the "visible" property if the layer exists.

- Mark

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
January 12, 2023

@Leon_Lincoln 

This error is when layer does not exists with the passed name. There are three layers name used in the code, Materials, Header and Detail Artwork. It is possible that layers with these names or one of the name does not exists in your document.

 

hideLayer(doc, "Materials");
hideLayer(doc, "Header");
hideLayer(doc, "Detail Artwork");
Best regards
Participant
January 13, 2023

Hello Charu,

 

Your insight help very much, I appreciate the your guideance and the issue has been resolved, the designers were not naming the art board/layers properly.

 

Leon