Copy link to clipboard
Copied
Hi guys,
I am new to javascript and I need help to open documents that is in the subfolders of my target folder. There are several subfolders(say subfolder 1 to 10) contained in a folder ("theFolder" in the script below). I want to open up all the documents in one subfolder at a time and do something to these documents . Then close them and open documents in another subfolder (loop).
I figure out how to open documents that are directly under a folder. The problem is I dont know to how to open a document under the subfolder of a folder
I need some code that could do something like "getfolders" (but"getfolders" doesn't exist) in order to number the subfolder from 1 to 10 and loop through them to open the documents.
Could some please kindly help me with the problem?
That's where I am so far:
#target photoshop
var theFolder = Folder.selectDialog("select folder");
if (theFolder) {
var theFiles = theFolder.getFiles(/\.(jpg|tif|eps|psd)$/i);
for (var m = 0; m < theFiles.length; m++)
{
// open all the documents in theFolder
var idOpn = charIDToTypeID( "Opn " );
var desc52 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc52.putPath( idnull, theFiles
var idAs = charIDToTypeID( "As " );
var idTIFF = charIDToTypeID( "TIFF" );
desc52.putClass( idAs, idTIFF );
executeAction( idOpn, desc52, DialogModes.NO );
//.... do something to the documents
//... save and close
}
}
I finally get the correct script by combining little pieces from here and there
After selecting the top folder, this script will loop through each subfolder, one at a tim.e, to open up two tif files -- which contain cy5 and texas respectively. Then the script will overlay the cy5 file with texas file in a new RBG file and save it with a similar name as the texas file except that the overlay file will replace Texasred with Overlay:
#target photoshop
app.bringToFront();
main(); //call the main funct
...Copy link to clipboard
Copied
also I am not sure how to use "includeSubFolders"
would that some how can be useful in my script?
Copy link to clipboard
Copied
The problem with what you want to do is opening just one folder at a time. To do this, you will have to have the script stop, and then if you start the script, it won't know where you left off. One way to get around this would be to have the script write the folders names to a csv or xml file that can be edited to include where you stopped so it can pick up where you left off. To get the subfolders, you would need to use the normal getFiles() command, but then put it through a recursive loop to both detect which files are folders and which are files that can be opened with photoshop.
Copy link to clipboard
Copied
Thanks for the help!
However, I dont know the commend to detect a file or a folder.
I tried to modifying my code ; but the "if(!theSubFolders.name.match(/\.(eps|ai|jpg|tif|psd|pdf|)$/i))" didnt work
#target photoshop
var theTopFolder = Folder.selectDialog("select folder");
if (theTopFolder) {
var theSubFolders = theTopFolder.getFiles();
if(!theSubFolders.name.match(/\.(eps|ai|jpg|tif|psd|pdf|)$/i)) {
theFiles = theSubFolders.getFiles(/\.(tif)$/i);
for (var m = 0; m < theFiles.length; m+2)
{
// =======================================================
var idOpn = charIDToTypeID( "Opn " );
var desc52 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
desc52.putPath( idnull, theFiles
var idAs = charIDToTypeID( "As " );
var idTIFF = charIDToTypeID( "TIFF" );
desc52.putClass( idAs, idTIFF );
executeAction( idOpn, desc52, DialogModes.NO );
// save and close
}
}
}
Copy link to clipboard
Copied
Here's a recursive script that will get all the files and folder names in a selected folder. What you would need to do it store that info in a CSV or XML file with your stopping place, so you could pick up from where you left off.
#target photoshop
var myFolder = new Folder('/c/')
myFolder= Folder.selectDialog('Select a folder to process');
var folderArray = new Array()
var fileArray = new Array()
findAllFiles (myFolder)
function findAllFiles(theFolder) {
var fileFolderArray = theFolder.getFiles();
for ( var i = 0; i < fileFolderArray.length; i++ ) {
var fileFoldObj = fileFolderArray;
if (fileFoldObj instanceof File ) {fileArray.push(fileFoldObj.name.toString())};//detectes if the the element is a file and not a folder
else{
folderArray.push(fileFoldObj.name.toString());
findAllFiles (fileFoldObj)//recursive part. if a folder runs the function again to get lower level folders.
}
};
};//end function FindAllFiles
$.writeln(folderArray);
$.writeln('=====================');
$.writeln(fileArray)
Copy link to clipboard
Copied
Actually neither do I know how to tore that info in a CSV or XML file with your stopping place. It may take me a while to get there. I am really just a beginner.
So I tried to think an alternative way. However, I got an error of "expected a reference of an existing file"-->app.open(File("/Users/icy/Desktop/topFolder.name/folderList/fileList[F-1]" ));
I'd greatly appreciate your kindness if you could please help me further with the problem!!
There's my code by modifying yours:
(basically I separate Folder and File array generation into two similar functions in an attempt to set the path of the file utilizing the "s" and "f " loop variables. Thus presumably I should be able to open files under one subfolder at a time )
#target photoshop
var topFolder = Folder.selectDialog('Select a folder to process');
var folderList = new Array();
var fileList = new Array();
var s;
var f;
var F;
findsubFolders (topFolder);
function findsubFolders(theFolder) {
var subFolderArray = theFolder.getFiles();
for ( s = 0; s < subFolderArray.length; s++ ) {
var subFoldObj = subFolderArray;
if (subFoldObj instanceof File ) // False
{
fileList.push(subFoldObj.name.toString())
};
// True
else{
folderList.push(subFoldObj.name.toString());
F = findAllFiles (subFoldObj);//recursive part. if a folder runs the function again to get lower level folders.
}
app.open(File("/Users/icy/Desktop/topFolder.name/folderList/fileList[F-1]" ));
app.open(File("/Users/icy/Desktop/topFolder.name/folderList/fileList
// there're only two files per each subfolder I need to operate on
};
};//end function findsubFolders
function findAllFiles(thesubFolder) {
var FileArray = thesubFolder.getFiles();
for ( f = s; f < (FileArray.length + s); f++ ) {
var fileObj = FileArray
if (fileObj instanceof File ) // true
{
// i add the if statement to confirm the file contains "cy5.5" or "texsred" in the file's name perspectively
if (FileArray
fileList.push(fileObj.name.toString())}
};//detectes if the the element is a file and not a folder
};
return f;
};//end function findAllFiles
Copy link to clipboard
Copied
oops
I forgot to indicate the place i want to code for manipulation
app.open(File("/Users/icy/Desktop/topFolder.name/folderList/fileList[F-1]" ));
app.open(File("/Users/icy/Desktop/topFolder.name/folderList/fileList
// do something
// save and close
}; end of the function
Copy link to clipboard
Copied
I'm not sure how well this all will work for you, as you can't really edit anything with the photos if the script is running, and once the script stops, everything is set back to ground 0. You're error is that you're putting your array variable inside a string in the path. You would need something like:
app.open(File("/Users/icy/Desktop/topFolder.name/" + folderList
+ "/" + fileList[F-1] ));
My original script I made so it just captured the names of the folders and files so show how it works. You really wouldn't just get the names. Instead of this:
fileList.push(subFoldObj.name.toString())
Use this:
fileList.push(subFoldObj)
Then to open the file:
var docRef1 = open(fileList[0])//if you have only two files
var docRef2 = open(fileList[2])
Copy link to clipboard
Copied
yes. That should work. I forgot to mention: my purpose of writing the script is just to overlay the cy5.5.TIFF file (red channel) with texsred.TIFF file (green channel) in a new RGB file. But since there are hundreds of subfolders(which contains the two files of interest )under one topfolder , I need a code to automatically do the overlay to save time.
Copy link to clipboard
Copied
h48846574 wrote:
Hi guys,
I am new to javascript and I need help to open documents that is in the subfolders of my target folder. There are several subfolders(say subfolder 1 to 10) contained in a folder ("theFolder" in the script below). I want to open up all the documents in one subfolder at a time and do something to these documents . Then close them and open documents in another subfolder (loop).
If you do something to the document I think you would want to save the changes before you close them. If that is what you want to do I do not think you need to bother writing such a script. Photoshop includes that script in menu File>Scripts>Image Processor... and there is an ever better Plug-in script you can download from the web Image Processor Pro. Once installed that will be in menu File>Automate>Image Processor Pro... with these script all you need to do is record actions which can include runing scripts to make the changes you want. The Image processor scripts will take care of opening files saving files and closing the open document.
However these scripts will not open all the files in a folder at once. Files will be process one at a time. If you have hundreds or thousands of files in a folder opening all of the in Photoshop at the sane time could cause serious problems on your system. If you actually need all the files open in Photoshop at the same time you may want to look at these scripts. IMO they would not be easy to change but will give you the way you need handle different image files formats. These script also filter folders they only process image files. Text files, html file, system files etc will be bypassed. If you want to process only one image file types you then must only have that type of image file in your folders of Download the Picture processor script that may still be on the Web Its like the Image processor scripts and has a source image file type filter feature
Copy link to clipboard
Copied
I finally get the correct script by combining little pieces from here and there
After selecting the top folder, this script will loop through each subfolder, one at a tim.e, to open up two tif files -- which contain cy5 and texas respectively. Then the script will overlay the cy5 file with texas file in a new RBG file and save it with a similar name as the texas file except that the overlay file will replace Texasred with Overlay:
#target photoshop
app.bringToFront();
main(); //call the main function
function main(){
var folders =[];
var topLevel = Folder.selectDialog(""); // select top folder
if(topLevel == null) return; //if cancelled quit
folders = FindAllFolders(topLevel, folders); // call function FindAllFolders
folders.unshift(topLevel); // add new items to the beginning of an array
for(var z=0; z < folders.length; z++){// loop through all subfolders
var fileList = folders
for(var a=0; a < fileList.length; a+=2)
{//loop through all files in folder; 2 at a time
if (fileList.name.match(/cy5/i)){// the file should contain cy5
var docref= app.open(fileList)};//open file
if (fileList[a+1].name.match(/texas/i)){// the file should contain texas
var docref1=app.open(fileList[a+1])};//open file
//** insert "entire overlay script final" recorded by scriptistener
//Marker - Sep 17, 2016, 6:21:49 PM
// start=======================================================
//create one new RGB customized file
var idMk = charIDToTypeID( "Mk " );
var desc136 = new ActionDescriptor();
var idNw = charIDToTypeID( "Nw " );
var desc137 = new ActionDescriptor();
var idMd = charIDToTypeID( "Md " );
var idRGBM = charIDToTypeID( "RGBM" );
desc137.putClass( idMd, idRGBM );
var idWdth = charIDToTypeID( "Wdth" );
var idRlt = charIDToTypeID( "#Rlt" );
desc137.putUnitDouble( idWdth, idRlt, 665.760000 );
var idHght = charIDToTypeID( "Hght" );
var idRlt = charIDToTypeID( "#Rlt" );
desc137.putUnitDouble( idHght, idRlt, 498.720000 );
var idRslt = charIDToTypeID( "Rslt" );
var idRsl = charIDToTypeID( "#Rsl" );
desc137.putUnitDouble( idRslt, idRsl, 150.000000 );
var idpixelScaleFactor = stringIDToTypeID( "pixelScaleFactor" );
desc137.putDouble( idpixelScaleFactor, 1.000000 );
var idFl = charIDToTypeID( "Fl " );
var idFl = charIDToTypeID( "Fl " );
var idBckC = charIDToTypeID( "BckC" );
desc137.putEnumerated( idFl, idFl, idBckC );
var idDpth = charIDToTypeID( "Dpth" );
desc137.putInteger( idDpth, 16 );
var idprofile = stringIDToTypeID( "profile" );
desc137.putString( idprofile, "sRGB IEC61966-2.1" );
var idDcmn = charIDToTypeID( "Dcmn" );
desc136.putObject( idNw, idDcmn, desc137 );
executeAction( idMk, desc136, DialogModes.NO );
//end of new file creation Marker - Sep 17, 2016, 6:22:19 PM
// start=======================================================
// run the Overlay action
var idPly = charIDToTypeID( "Ply " );
var desc138 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref90 = new ActionReference();
var idActn = charIDToTypeID( "Actn" );
ref90.putName( idActn, "Overlay" );
var idASet = charIDToTypeID( "ASet" );
ref90.putName( idASet, "Default Actions" );
desc138.putReference( idnull, ref90 );
executeAction( idPly, desc138, DialogModes.NO );
// end of Overlay action Marker - Sep 17, 2016, 6:24:08 PM
// start=======================================================
// save the overlay file
var idsave = charIDToTypeID( "save" );
var desc139 = new ActionDescriptor();
var idAs = charIDToTypeID( "As " );
var desc140 = new ActionDescriptor();
var idBytO = charIDToTypeID( "BytO" );
var idPltf = charIDToTypeID( "Pltf" );
var idMcnt = charIDToTypeID( "Mcnt" );
desc140.putEnumerated( idBytO, idPltf, idMcnt );
var idTIFF = charIDToTypeID( "TIFF" );
desc139.putObject( idAs, idTIFF, desc140 );
var idIn = charIDToTypeID( "In " );
var Name= fileList[a+1].name.replace("Texasred", "Overlay"); //rename the new RGB Overlay file
desc139.putPath( idIn, new File( folders
var idLwCs = charIDToTypeID( "LwCs" );
desc139.putBoolean( idLwCs, true );
executeAction( idsave, desc139, DialogModes.NO );
// end of file saving Marker - Sep 17, 2016, 6:26:43 PM
// start =======================================================
// close all files
var idCls = charIDToTypeID( "Cls " );
executeAction( idCls, undefined, DialogModes.NO );
// =======================================================
var idCls = charIDToTypeID( "Cls " );
executeAction( idCls, undefined, DialogModes.NO );
// =======================================================
var idCls = charIDToTypeID( "Cls " );
executeAction( idCls, undefined, DialogModes.NO );
// end of file closing Marker - Sep 17, 2016, 6:27:43 PM
//** end of insert of entire overlay script final
}//end filelist loop
}//end subfolders loop
}//end main function
function FindAllFolders( srcFolderStr, destArray) {
var fileFolderArray = Folder( srcFolderStr ).getFiles();
for ( var i = 0; i < fileFolderArray.length; i++ ) {
var fileFoldObj = fileFolderArray;
if ( fileFoldObj instanceof File ) {
} else {
destArray.push( Folder(fileFoldObj) ); // add new items to the end of an array
FindAllFolders( fileFoldObj.toString(), destArray );
}
}
return destArray;
}
Copy link to clipboard
Copied
Glad you got it working.