Copy link to clipboard
Copied
hi every body why script for photoshop worked fine , and suddenly without understanding I have an error on line 80
var topFolder = (Folder.selectDialog("Select the top level folder."))
var fileandfolderArray = scanSubFolders(topFolder,/\.(psd)$/i);
var fileList = fileandfolderArray[0];
for (var i = 0; i < fileList.length; i++) {
var myFile = fileList[i];
app.open(myFile);
// check whether layer of a certain name exists in photoshop;
if (app.documents.length > 0) {
var theLayer = checkForLayerName("<< Doubleclick to change etiquette");
if (theLayer == true) {//alert ("layer of the name exists");
app.doAction("update pdf", "Actions par défaut");
while (app.documents.length > 0) {
var docName = fileList[i].name.replace(/\.[^\.]+$/, '');
var doc = app.activeDocument;
var outputFolder = Folder(doc.path);
var newFile = new File(decodeURI(outputFolder) + "/" + docName + ".png");
app.runMenuItem(stringIDToTypeID("placedLayerUpdateAllModified"));
exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.PNG8 = false; // false = PNG-24
exportOptions.transparency = true; // true = transparent
exportOptions.interlaced = false; // true = interlacing on
exportOptions.includeProfile = true; // false = don't embedd ICC profile
doc.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);
doc.close(SaveOptions.SAVECHANGES);
}
}
else {
app.doAction("close", "Actions par défaut");
}
}
}
alert("Done processing files!")
function scanSubFolders(tFolder, scanMask){
var subFolders = new Array();
var allFiles = new Array();
subFolders[0] = tFolder;
for (var j = 0; j < subFolders.length; j++){
var procFiles = subFolders[j].getFiles();
for (var i = 0; i< procFiles.length; i++){
if (procFiles[i] instanceof File ){
if(scanMask == undefined) allFiles.push(procFiles[i]);
if (procFiles[i].fullName.search(scanMask) != -1) allFiles.push(procFiles[i]);
}else if (procFiles[i] instanceof Folder){
subFolders.push(procFiles[i]);
scanSubFolders(procFiles[i], scanMask);
}
}
}
return [allFiles,subFolders];
};
////// collect layers //////
function checkForLayerName (theString) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated( charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
// process the layers;
var theLayers = new Array;
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
if (theName == theString) {return true};
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
theLayers.push([theName, theIndex, theID])
};
}
catch (e) {};
};
return false
};
Copy link to clipboard
Copied
You use indexes when accessing a layer. The peculiarity of working with layer index is that only the background layer can have a zero index. If the background layer is not in the document, then the indexes always start from one.
Before looping through layers, you need to add a check to see if the document has a background layer and change the variables in the loop definition accordingly.
Copy link to clipboard
Copied
i have reboot computer, now work ^^
Copy link to clipboard
Copied
Reboot cannot fix a bug in the code.
Copy link to clipboard
Copied
i m sorry i don t know what you mean with your drawing?
about reboot, i have no explain... now it work
Copy link to clipboard
Copied
Above i explained what's wrong: you're getting an error because one of the documents doesn't have a background layer. The peculiarity of layer indexing in ActionManager is that only the background layer can have index 0. That is, an attempt to execute the loop:
for (var m = 0; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex( charIDToTypeID( "Lyr " ), m);
var layerDesc = executeActionGet(ref);
}
will throw an error.
Try this:
var topFolder = (Folder.selectDialog("Select the top level folder."))
var fileandfolderArray = scanSubFolders(topFolder, /\.(psd)$/i);
var fileList = fileandfolderArray[0];
for (var i = 0; i < fileList.length; i++) {
var myFile = fileList[i];
app.open(myFile);
// check whether layer of a certain name exists in photoshop;
if (app.documents.length > 0) {
var theLayer = checkForLayerName("<< Doubleclick to change etiquette");
if (theLayer == true) {//alert ("layer of the name exists");
app.doAction("update pdf", "Actions par défaut");
while (app.documents.length > 0) {
var docName = fileList[i].name.replace(/\.[^\.]+$/, '');
var doc = app.activeDocument;
var outputFolder = Folder(doc.path);
var newFile = new File(decodeURI(outputFolder) + "/" + docName + ".png");
app.runMenuItem(stringIDToTypeID("placedLayerUpdateAllModified"));
exportOptions = new ExportOptionsSaveForWeb();
exportOptions.format = SaveDocumentType.PNG;
exportOptions.PNG8 = false; // false = PNG-24
exportOptions.transparency = true; // true = transparent
exportOptions.interlaced = false; // true = interlacing on
exportOptions.includeProfile = true; // false = don't embedd ICC profile
doc.exportDocument(newFile, ExportType.SAVEFORWEB, exportOptions);
doc.close(SaveOptions.SAVECHANGES);
}
}
else {
app.doAction("close", "Actions par défaut");
}
}
}
alert("Done processing files!")
function scanSubFolders(tFolder, scanMask) {
var subFolders = new Array();
var allFiles = new Array();
subFolders[0] = tFolder;
for (var j = 0; j < subFolders.length; j++) {
var procFiles = subFolders[j].getFiles();
for (var i = 0; i < procFiles.length; i++) {
if (procFiles[i] instanceof File) {
if (scanMask == undefined) allFiles.push(procFiles[i]);
if (procFiles[i].fullName.search(scanMask) != -1) allFiles.push(procFiles[i]);
} else if (procFiles[i] instanceof Folder) {
subFolders.push(procFiles[i]);
scanSubFolders(procFiles[i], scanMask);
}
}
}
return [allFiles, subFolders];
};
////// collect layers //////
function checkForLayerName(theString) {
// get number of layers;
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('numberOfLayers'));
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var applicationDesc = executeActionGet(ref);
var theNumber = applicationDesc.getInteger(stringIDToTypeID("numberOfLayers"));
var ref = new ActionReference();
ref.putProperty(stringIDToTypeID('property'), stringIDToTypeID('hasBackgroundLayer'));
ref.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var offset = executeActionGet(ref).getBoolean(stringIDToTypeID("hasBackgroundLayer")) ? 0 : 1;
// process the layers;
var theLayers = new Array;
for (var m = offset; m <= theNumber; m++) {
try {
var ref = new ActionReference();
ref.putIndex(charIDToTypeID("Lyr "), m);
var layerDesc = executeActionGet(ref);
var layerSet = typeIDToStringID(layerDesc.getEnumerationValue(stringIDToTypeID("layerSection")));
var isBackground = layerDesc.getBoolean(stringIDToTypeID("background"));
// if group collect values;
if (layerSet != "layerSectionEnd" /*&& layerSet != "layerSectionStart" && isBackground != true*/) {
var theName = layerDesc.getString(stringIDToTypeID('name'));
if (theName == theString) { return true };
var theID = layerDesc.getInteger(stringIDToTypeID('layerID'));
var theIndex = layerDesc.getInteger(stringIDToTypeID('itemIndex'));
theLayers.push([theName, theIndex, theID])
};
}
catch (e) {};
};
return false
};