Skip to main content
September 12, 2016
Answered

open documents(or files) in the subfolders of my target folder

  • September 12, 2016
  • 5 replies
  • 2533 views

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

}

}

This topic has been closed for replies.
Correct answer

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.getFiles("*.tif"); //get a list of all tifs in this subfolder

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+"/" + Name ) );

    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;

}

Chuck Uebele
Community Expert
Community Expert
September 18, 2016

Glad you got it working.

Correct answer
September 18, 2016

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.getFiles("*.tif"); //get a list of all tifs in this subfolder

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+"/" + Name ) );

    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;

}

JJMack
Community Expert
Community Expert
September 15, 2016

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

JJMack
Chuck Uebele
Community Expert
Community Expert
September 12, 2016

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.

September 13, 2016

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,theFiles[m+1]);

    var idAs = charIDToTypeID( "As  " );

    var idTIFF = charIDToTypeID( "TIFF" );

    desc52.putClass( idAs, idTIFF );

executeAction( idOpn, desc52, DialogModes.NO );

// save and close

}

}

}

Chuck Uebele
Community Expert
Community Expert
September 14, 2016

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)

September 12, 2016

also I am not sure how to use "includeSubFolders"

would that some how can be useful in my script?