Copy link to clipboard
Copied
Im seeing a strange issue with this script. When selecting more than 75 or so of images, the script appears to simply stall out. It fails to process any of the images nor replace the smart object with any of the selected images. very strange.
any ideas on what is causing this behavior?
// replace smart object’s content and save psd;
// 2011, use it at your own risk;
#target photoshop
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is smart object;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else {
// select files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
if (theFiles) {
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
theLayer = replaceContents(theFiles
var theNewName = theFiles
//save jpg;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
myDocument.saveAs((new File(thePath + "/" + theNewName + ".jpg")), jpegOptions, true);
}
}
}
};
////// get psds, tifs and jpgs from files //////
function getFiles(theFile) {
if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
};
////// replace contents //////
function replaceContents(newFile, theSO) {
app.activeDocument.activeLayer = theSO;
// =======================================================
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(newFile));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
return app.activeDocument.activeLayer
};
Copy link to clipboard
Copied
Try this version.
What is the OS and version of Photoshop?
I have everything is working.
try
{
if (app.documents.length > 0) {
var myDocument = app.activeDocument;
try { var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]; } catch(e) { alert(e); }
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
// psd options;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
// check if layer is smart object;
if (theLayer.kind != "LayerKind.SMARTOBJECT") {
alert("selected layer is not a smart object")
} else {
// select files;
if ($.os.search(/windows/i) != -1) {
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
alert("theFiles.length="+theFiles.length);
if (theFiles) {
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
alert("try to process file:\n"+theFiles
); theLayer = replaceContents(theFiles
, theLayer); var theNewName = theFiles
.name.match(/(.*)\.[^\.]+$/)[1]; //save jpg;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
myDocument.saveAs((new File(thePath + "/" + theNewName + ".jpg")), jpegOptions, true);
}
}
}
};
}
catch(e) { alert(e); }
alert("Done!");
////// get psds, tifs and jpgs from files //////
function getFiles(theFile) {
try
{
if (theFile.name.match(/\.(psd|tif|jpg)$/i) != null || theFile.constructor.name == "Folder") {
return true
};
}
catch(e) { alert(e); }
};
////// replace contents //////
function replaceContents(newFile, theSO) {
try
{
app.activeDocument.activeLayer = theSO;
// =======================================================
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(newFile));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
return app.activeDocument.activeLayer
}
catch(e) { alert(e); }
};
Copy link to clipboard
Copied
Windows 10
i7, 32GB Ram, 1TB hd
Photoshop CC 20.0.2 release
thank you, i'll give your script a try and let you know the results
ran modified script.
Error 21:null is not an object.
Line:29
-> alert("theFiles.length=" + theFiles.length);
thank you again for posting your code with alerts added.
in running it, i did see the issue occur at ~750 image files. it did work at 725 image files.
is there a limit setting somewhere? this is very odd
Copy link to clipboard
Copied
Copy link to clipboard
Copied
excellent, thank you! i would like to try to select the folder, instead of the individual files. unfortunately, my scripting knowledge is very limited. what would you recommend changing in the script to make this happen? thank you!
Copy link to clipboard
Copied
I am busy now and have not verified.
Replace line
var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)
to
var folder = Folder.selectDialog("Select folder", "C:\\");
if (folder) theFiles = folder.getFiles(/\.(psd|tif|jpg)$/);
Copy link to clipboard
Copied
Small error in Regexp (case not taken into account).
This is how it should work.
var theFiles = null;
try {
if (app.documents.length > 0)
{
var myDocument = app.activeDocument;
try { var theName = myDocument.name.match(/(.*)\.[^\.]+$/)[1]; } catch(e) { alert(e); }
var thePath = myDocument.path;
var theLayer = myDocument.activeLayer;
psdOpts = new PhotoshopSaveOptions();
psdOpts.embedColorProfile = true;
psdOpts.alphaChannels = true;
psdOpts.layers = true;
psdOpts.spotColors = true;
if (theLayer.kind != "LayerKind.SMARTOBJECT")
{
alert("selected layer is not a smart object")
}
else
{
var folder = Folder.selectDialog("Select folder", "C:\\");
if (folder) theFiles = folder.getFiles(/\.(psd|tif|jpg)$/i);
if (theFiles)
{
for (var m = 0; m < theFiles.length; m++)
{
theLayer = replaceContents(theFiles
, theLayer); var theNewName = theFiles
.name.match(/(.*)\.[^\.]+$/)[1]; var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
myDocument.saveAs((new File(thePath + "/" + theNewName + ".jpg")), jpegOptions, true);
}
}
}
}
}
catch(e) { alert(e); }
if (theFiles) alert("Processed " + theFiles.length + " files");
else alert("No files found");
function replaceContents(newFile, theSO)
{
try {
app.activeDocument.activeLayer = theSO;
var idplacedLayerReplaceContents = stringIDToTypeID("placedLayerReplaceContents");
var desc3 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
desc3.putPath(idnull, new File(newFile));
var idPgNm = charIDToTypeID("PgNm");
desc3.putInteger(idPgNm, 1);
executeAction(idplacedLayerReplaceContents, desc3, DialogModes.NO);
return app.activeDocument.activeLayer
}
catch(e) { alert(e); }
}
Copy link to clipboard
Copied
You will have the same problem when your select a folder for then you will get the Image files in the in the folder the file list will again not fit in a string it that was your problem.. I was most likely able to do not that you because I created a E:\tmp\replacememts.jpg folder my files paths length were smaller than yours. Each would be like 20 bytes so a file list of 876 files would be 17Kbytes If the limit is 64K I could do thousand in that case. Why did you start a second thread?
I would look at r-bin append he knows javascript well and regular expression are something I can not decode they are very powerful to parsing strings but can have errors so could produce something invalid. However case may only be a problem on a Mac its unix file system is likely case sensitive Windows File systen can have mixed case. But name cans not be duplicated in mixed case in the same folder. Case does not matter in Windows. That script is written to be use on PC and on Mac. There are some differences filtering file between Mac and PC.
I also saw RAM usage growing. Though there is only document open and it had one layer repeatedly replaced I saw RAM usage increase slowly grow from 4GB to 19GB processing 876 Smart Object replacements
Is this working on you machine OK now? I commented out the save and ran it on my machine. I also did not have thousands of like size file to use as replacement image. So the replacement would not work correctly anyway the output file would show that. I keep copying the file in my tmp into the temp folder toll I had 2000 files. When I ran the script I saw RAM use by PS to go over 20GB. I now copied the 2K files to 4K then 4K to 8K. I wanted to make sure the file list exceed 64K Hex bytes. This is running now RAM usage is approaching my 35GB limit for PS it ate 34.8GB now but still running.
Copy link to clipboard
Copied
i've modified the script and hard coded the directory of image files. so no popup occurs.
ran the script on 38k image files. 24hrs later, its still running (8k left to process), error free. every file takes 4-5 sec to process.
20% cpu, 9GB ram. computer is running great.
// select files;
if ($.os.search(/windows/i) != -1) {
// var theFiles = File.openDialog("please select files", "*.psd;*.tif;*.jpg", true)
var theFiles = Folder("/d/Canvas Project/Images/3x2-correct size/JPEG").getFiles(/\.(jpg|tif|psd)$/i);
} else {
var theFiles = File.openDialog("please select files", getFiles, true)
};
if (theFiles) {
// work through the array;
for (var m = 0; m < theFiles.length; m++) {
// replace smart object;
theLayer = replaceContents(theFiles
var theNewName = theFiles
//save jpg;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 10;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
myDocument.saveAs((new File(thePath + "/" + theNewName + ".jpg")), jpegOptions, true);
}
}
Copy link to clipboard
Copied
Yes it will take a very long time to process 38K replacement objects and a lot of disk space. Drink some wine that wat I do... Good to hear I will cancel my 8K job now. Terminated PS.....
Copy link to clipboard
Copied
Bonjour
I'm not sure I understand everything
But you should take a look HERE!