Copy link to clipboard
Copied
Hello everyone, how are you? I tried to make a functional dialog box to import images from folders and subfolders with the script for that topic: https://community.adobe.com/t5/photoshop/photoshop-javascript-open-files-in-all-subfolders/td-p/5162...
However, I don't understand why it only imports images from subfolders. Could someone please identify the error and share? Thanks:
dlg = new Window("dialog"); dlg.text = "Dialog";
dlg.preferredSize.width = 300; dlg.orientation = "column";
dlg.alignChildren = ["center","top"]; dlg.spacing = 10; dlg.margins = 16;
st1 = dlg.add("statictext", undefined, undefined, {name: "st1" ,truncate:'middle'} ); st1.text = "diretorio";
st1.preferredSize.width = 150; st1.justify = "center";
b1 = dlg.add("button", undefined, undefined, {name: "b1"}); b1.text = "SelectFolder";
ck1 = dlg.add("checkbox", undefined, undefined, {name: "ck1"}); ck1.text = "inc.Subfolders";
b1.onClick=function(){
topFolder = Folder.selectDialog();
fileandfolderAr = scanSubFolders(topFolder, /\.(jpg|jpeg|tif|psd|bmp|gif|png|)$/i);
fileList = fileandfolderAr[0];
if( topFolder !=null){ st1.text = decodeURI(topFolder.fsName)}
dlg.text = "Dialog " + fileList.length + " Files";
}
b2 = dlg.add("button", undefined, undefined, {name: "b2"});
b2.text = "Processar";
b2.onClick=function(){
dlg.close();
for( a = 0 ; a < fileList.length; a++) {
docRef = open(fileList[a]);
////// FUNÇÕES SCRIPTS AQUI!
alert ("Script")
}
}
dlg.show();
function scanSubFolders(tFolder, mask) { // folder object, RegExp or string
sFolders = [];
allFiles = [];
sFolders[0] = tFolder;
for ( j = 0; j < sFolders.length; j++) { // loop through folders
procFiles = sFolders[j].getFiles();
for ( i = 0; i < procFiles.length; i++) { // loop through this folder contents
if (procFiles[i] instanceof File ) {
if(mask == undefined) {
allFiles.push(procFiles); // if no search mask collect all files
}
if (procFiles[i].fullName.search(mask) != -1) {
allFiles.push(procFiles[i]); // otherwise only those that match mask
}
}
else if (procFiles[i] instanceof Folder) {
sFolders.push(procFiles[i]); //OPÇÃO PARA IMPORTAR SUPBASTAS
scanSubFolders(procFiles[i], mask); // search the subfolder
}
}
}
return [allFiles,sFolders];
}
If you just want to process the same images as the last folder used just by clicking on the process button.
Add:
var topFolder= Folder(st1.text);
if (!topFolder.exists) { alert(" Folder not found!","error", true); return;}
else{
topFolder = new Folder(st1.text);
if(ck2.value){ fileandfolderAr = scanSubFolders( topFolder, /\.(psd|)$/i);}
else{ fileandfolderAr = scanSubFolders( topFolder, /\.(jpg|jpeg|tif|psd|bmp|gif|png|pdf|tga|)$/i);}
fileList = fileandfolderAr[0];
};
in betw
Copy link to clipboard
Copied
I miss some code on my windows machine image in the subfolder of my selected folder opened but the image in the selected folder did not open. You recursion is not coded correctly.
You may want to look at Adobe Image Processor script. the top of its dialog look like your dialog select a folder of image files to process and has a option to include sub folders.
Copy link to clipboard
Copied
Hi JJMack , I found the problem here, actually for some reason the "var" before "procFiles = sFolders [j] .getFiles ();" disappeared, so the solution was:
Change: procFiles = sFolders [j] .getFiles ();
Per: var procFiles = sFolders [j] .getFiles ();
Copy link to clipboard
Copied
I did not run the code, but here you put the entire files array of procFiles files in the allFiles array, so if you try to open this array element using open (fileList [a]), you will get an error, since there will be no link in fileList [a] per file, but subarray
if (procFiles[i] instanceof File ) {
if(mask == undefined) {
allFiles.push(procFiles); // if no search mask collect all files
}
You have already found another mistake using global variables in a recursive function yourself. Similarly, you need to add var to variables in loops
Copy link to clipboard
Copied
Hi jazz-y, Here it is working perfectly after the change I made, can you check this script and let me know if there was an error for you ?
dlg = new Window("dialog"); dlg.text = "Dialog";
dlg.preferredSize.width = 300; dlg.orientation = "column";
dlg.alignChildren = ["center","top"]; dlg.spacing = 10; dlg.margins = 16;
st1 = dlg.add("statictext", undefined, undefined, {name: "st1" ,truncate:'middle'} ); st1.text = "diretorio";
st1.preferredSize.width = 150; st1.justify = "center";
b1 = dlg.add("button", undefined, undefined, {name: "b1"}); b1.text = "SelectFolder";
ck1 = dlg.add("checkbox", undefined, undefined, {name: "ck1"}); ck1.text = "inc.Subfolders";
b1.onClick=function(){
topFolder = Folder.selectDialog();
fileandfolderAr = scanSubFolders(topFolder, /\.(jpg|jpeg|tif|psd|bmp|gif|png|)$/i);
fileList = fileandfolderAr[0];
if( topFolder !=null){ st1.text = decodeURI(topFolder.fsName)}
dlg.text = "Dialog " + fileList.length + " Files";
}
b2 = dlg.add("button", undefined, undefined, {name: "b2"});
b2.text = "Processar";
b2.onClick=function(){
dlg.close();
for( a = 0 ; a < fileList.length; a++) {
docRef = open(fileList[a]);
////// FUNÇÕES SCRIPTS AQUI!
alert ("Script")
}
}
dlg.show();
function scanSubFolders(tFolder, mask) { // folder object, RegExp or string
var sFolders = [];
var allFiles = [];
sFolders[0] = tFolder;
for (var j = 0; j < sFolders.length; j++) { // loop through folders
var procFiles = sFolders[j].getFiles();
for (var i = 0; i < procFiles.length; i++) { // loop through this folder contents
if (procFiles[i] instanceof File ) {
if(mask == undefined) {
allFiles.push(procFiles); // if no search mask collect all files
}
if (procFiles[i].fullName.search(mask) != -1) {
allFiles.push(procFiles[i]); // otherwise only those that match mask
}
}
else if (procFiles[i] instanceof Folder) {
if(ck1.value){ sFolders.push(procFiles[i]);} //OPÇÃO PARA IMPORTAR SUPBASTAS
scanSubFolders(procFiles[i], mask); // search the subfolder
}
}
}
return [allFiles,sFolders];
}
Not!
Note that I validated the checkbox element!
Copy link to clipboard
Copied
if(mask == undefined) {
allFiles.push(procFiles); // if no search mask collect all files
}
replace with
if(mask == undefined) {
allFiles.push(procFiles[i]); // if no search mask collect all files
}
I am writing from a mobile phone, I have no way to test your code.
Copy link to clipboard
Copied
Thanks for the tip!
Copy link to clipboard
Copied
Hello everyone again!
I am trying to complete this "beta version" of a script for batch processing that meets my needs, the script will work to check all folders and subfolders and in some cases will only process .PSD files from different locations, with the file path being were registered in the dialog box, this works very well, my frustration is that I was not able to configure the script so that I can process the same images again by just clicking on the "Process button". It presents an error and I was not able to correct it. All help is valid.
var cont = 0;
dlg = new Window("dialog"); dlg.text = "Dialog";
dlg.preferredSize.width = 300; dlg.orientation = "column";
dlg.alignChildren = ["center","top"]; dlg.spacing = 10; dlg.margins = 16;
st1 = dlg.add("statictext", undefined, undefined, {name: "st1" ,truncate:'middle'} ); st1.text = "diretorio";
st1.preferredSize.width = 200; st1.justify = "center";
var desc = null;
try { desc = app.getCustomOptions("8da280d0-518c-11ea-aaef-0800200c9a66")} catch (e) {};
if (desc) try { st1.text = desc.getString(0)} catch (e) {};
b1 = dlg.add("button", undefined, undefined, {name: "b1"}); b1.text = "SelectFolder";
ck1 = dlg.add("checkbox", undefined, undefined, {name: "ck1"}); ck1.text = "inc.Subfolders";
ck1.value=true;
ck2 = dlg.add("checkbox", undefined, undefined, {name: "ck2"}); ck2.text = "Somente PSD ";
b1.onClick=function(){
try{
topFolder = Folder.selectDialog();
if(ck2.value){ fileandfolderAr = scanSubFolders(topFolder, /\.(psd|)$/i);}
else{ fileandfolderAr = scanSubFolders(topFolder, /\.(jpg|jpeg|tif|psd|bmp|gif|png|)$/i);}
fileList = fileandfolderAr[0];
if( topFolder !=null){ st1.text = decodeURI(topFolder.fsName)}
dlg.text = "Dialog " + fileList.length + " Files";
}
catch(e){};
var desc = new ActionDescriptor();
desc.putString(0, st1.text);
app.putCustomOptions("8da280d0-518c-11ea-aaef-0800200c9a66", desc, true);
}
b2 = dlg.add("button", undefined, undefined, {name: "b2"}); b2.text = "Processar";
b2.onClick=function(){
dlg.close();
for( a = 0 ; a < fileList.length; a++) {
docRef = open(fileList[a]);
////// FUNÇÕES SCRIPTS AQUI!
cont = Number(cont)+1 ;
alert ( "Processing..."+ (cont) + " / " + fileList.length);
}
}
dlg.show();
function scanSubFolders(tFolder, mask) { // folder object, RegExp or string
try{
var sFolders = [];
var allFiles = [];
sFolders[0] = tFolder;
for (var j = 0; j < sFolders.length; j++) { // loop through folders
var procFiles = sFolders[j].getFiles();
for (var i = 0; i < procFiles.length; i++) { // loop through this folder contents
if (procFiles[i] instanceof File ) {
if(mask == undefined) {
allFiles.push(procFiles[i]); // if no search mask collect all files
}
if (procFiles[i].fullName.search(mask) != -1) {
allFiles.push(procFiles[i]); // otherwise only those that match mask
}
}
else if (procFiles[i] instanceof Folder) {
if(ck1.value){ sFolders.push(procFiles[i]);} //OPÇÃO PARA IMPORTAR SUPBASTAS
scanSubFolders(procFiles[i], mask); // search the subfolder
}
}
}
return [allFiles,sFolders];
}
catch(e){};
}
Copy link to clipboard
Copied
If you just want to process the same images as the last folder used just by clicking on the process button.
Add:
var topFolder= Folder(st1.text);
if (!topFolder.exists) { alert(" Folder not found!","error", true); return;}
else{
topFolder = new Folder(st1.text);
if(ck2.value){ fileandfolderAr = scanSubFolders( topFolder, /\.(psd|)$/i);}
else{ fileandfolderAr = scanSubFolders( topFolder, /\.(jpg|jpeg|tif|psd|bmp|gif|png|pdf|tga|)$/i);}
fileList = fileandfolderAr[0];
};
in between
b2.onClick = function () {.... and dlg.close ()