Skip to main content
MattiaVoso
Known Participant
July 25, 2018
Answered

upgrde this script to subfolders

  • July 25, 2018
  • 2 replies
  • 4765 views

Hi There,

Thanks to this community i've just finished coding this script that:

1.search for psd files in a folder

2. opens the files and does some work with the layer and SO

3. saves the resulting tif in the same folder of the psd

I would try to upgrade this script to work also on subfolders... do you think is possibile?

the folder structure is

- Product name (folder)

  - product code XXX (folder)

      - filexxx.psd

      - fileyyy.psd

      - filezzz.psd

  - product code YYY (folder)

      - filexxx.psd

      - fileyyy.psd

      - filezzz.psd

Right now i'm able to launch the script for every product code folder. i would like to launch it on the Product name folder and let it work on every subfolders..

here's the code

// JavaScript Document

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts;

var i = 0;

// Select the source folder.

sourceFolder = Folder.selectDialog( 'Seleziona la cartella di origine');

// If a valid folder is selected

if ( sourceFolder != null )

{

    files = new Array();

    fileType = "*.psd"; //prompt( 'Select type of photoshop files to you want to process. Eg: *.psd', ' ' );

// Get all files matching the pattern

    files = sourceFolder.getFiles( fileType );

if ( files.length > 0 )

    {

        // Get the destination to save the files

        //destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted .tif files.', '~' );

        destFolder = sourceFolder;

        for ( i = 0; i < files.length; i++ )

        {

            sourceDoc = app.open(files); // returns the document object

     var docRef = sourceDoc;

with (docRef) {

chiusuraPesa();

}

}

}

}

function chiusuraPesa(){

cercaShadow();

searchSO(); //cerca style

openSO(); //apre oggetto avanzato

applicaVisibile(); // applica visibile, via tracciati e canali, salva e chiude l'oggetto avanzato

salvaTiffDEF(); //salva il tiff definitivo

}

function cercaShadow(){

var srcDoc = app.activeDocument;

srcDoc.channels.removeAll();

var layerSetRef = srcDoc.layerSets.getByName("shadow");

//srcDoc.activeLayer = srcDoc.layers.getByName("style");

layerSetRef.merge();

    var layerSetRef = srcDoc.layers.getByName("shadow");

    srcDoc.activeLayer = layerSetRef ;

hasMask();//controlla se esiste una maschera di livello su shadow e in tal caso la cancella

}

function hasMask() { 

    var srcDoc = app.activeDocument;

          var lm = true, pmd; 

          try { 

                    pmd = srcDoc.activeLayer.layerMaskDensity; 

                    srcDoc.activeLayer.layerMaskDensity = 50.0; 

                    srcDoc.activeLayer.layerMaskDensity = pmd; 

          } catch(e) { lm = false }; 

          if (lm == true) {

               function sTT(v) {return stringIDToTypeID(v)} 

 

                (ref = new ActionReference()).putEnumerated 

                (sTT('channel'), sTT('channel'), sTT('mask')); 

                (dsc = new ActionDescriptor()).putReference(sTT('null'), ref) 

                executeAction(sTT('select'), dsc); 

                 

                  var idDlt = charIDToTypeID( "Dlt " );

                    var desc110 = new ActionDescriptor();

                    var idnull = charIDToTypeID( "null" );

                        var ref49 = new ActionReference();

                        var idChnl = charIDToTypeID( "Chnl" );

                        var idOrdn = charIDToTypeID( "Ordn" );

                        var idTrgt = charIDToTypeID( "Trgt" );

                        ref49.putEnumerated( idChnl, idOrdn, idTrgt );

                    desc110.putReference( idnull, ref49 );

                    var idAply = charIDToTypeID( "Aply" );

                    desc110.putBoolean( idAply, true );

                executeAction( idDlt, desc110, DialogModes.NO );

          }

}; 

function searchSO(){

var srcDoc = app.activeDocument;

srcDoc.activeLayer = srcDoc.layers.getByName("style");

}

function openSO (){

var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" );

    var desc7 = new ActionDescriptor();

executeAction( idplacedLayerEditContents, desc7, DialogModes.NO );

}

function applicaVisibile(){

var newLayer = activeDocument.artLayers.add();

var idMrgV = charIDToTypeID( "MrgV" );

    var desc3 = new ActionDescriptor();

    var idDplc = charIDToTypeID( "Dplc" );

    desc3.putBoolean( idDplc, true );

executeAction( idMrgV, desc3, DialogModes.NO );

var newDoc = app.activeDocument;

newDoc.activeLayer.name = "Complete";

    var quota = "quota";

var layName = newDoc.activeLayer.name;

var l = Number(newDoc.layers.length)+1; 

while(--l){ 

if(newDoc.layers[l-1].name!=layName && newDoc.layers[l-1].name!=quota){ 

newDoc.layers[l-1].remove(); 

else{continue;} 

newDoc.pathItems.removeAll();

newDoc.channels.removeAll();

newDoc.save();

newDoc.close();

}

function salvaTiffDEF(){

tiffFile = new File(activeDocument.path + "/" + activeDocument.name )

tiffSaveOptions = new TiffSaveOptions()

TiffSaveOptions.embedColorProfile = true

TiffSaveOptions.layers = true

TiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW

app.activeDocument.saveAs(tiffFile, tiffSaveOptions, true, Extension.LOWERCASE)

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

}

Thank you in advance to all of you

This topic has been closed for replies.
Correct answer r-bin

Try this. Should work.

Handles all subfolders.

// JavaScript Document 

///////////////////////////////////////////////////

function init_files(folder, mask)

    {

    try

        {

        function push_files(folder)

            {

            try

                {

                var f = folder.getFiles();

                for (var i = 0; i < f.length; i++)

                    {

                    if (f instanceof Folder)

                        push_files(f);

                    else

                        if ( f.name.match(RegExp(mask+"$", "i")) ) files.push(f);

                    }

                f = null;

                }

            catch (e) { _alert(e); }

            }

        push_files(folder);

        }

    catch (e) { _alert(e); }

    }

///////////////////////////////////////////////////

 

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts; 

var i = 0; 

 

 

// Select the source folder. 

sourceFolder = Folder.selectDialog( 'Seleziona la cartella di origine'); 

 

 

// If a valid folder is selected 

if ( sourceFolder != null ) 

    files = new Array(); 

    fileType = ".psd";

    init_files(sourceFolder, fileType); 

 

if ( files.length > 0 ) 

    { 

        // Get the destination to save the files 

        //destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted .tif files.', '~' ); 

        destFolder = sourceFolder; 

        for ( i = 0; i < files.length; i++ ) 

        { 

            sourceDoc = app.open(files); // returns the document object 

 

 

     var docRef = sourceDoc; 

 

with (docRef) { 

 

chiusuraPesa(); 

 

 

 

////////////////////////////////////////////////////////////////// 

//           Next everything is unchanged                       //

////////////////////////////////////////////////////////////////// 

 

 

function chiusuraPesa(){ 

cercaShadow(); 

searchSO(); //cerca style 

openSO(); //apre oggetto avanzato 

applicaVisibile(); // applica visibile, via tracciati e canali, salva e chiude l'oggetto avanzato 

salvaTiffDEF(); //salva il tiff definitivo  

 

 

 

 

function cercaShadow(){ 

var srcDoc = app.activeDocument; 

srcDoc.channels.removeAll(); 

var layerSetRef = srcDoc.layerSets.getByName("shadow"); 

//srcDoc.activeLayer = srcDoc.layers.getByName("style"); 

layerSetRef.merge(); 

    var layerSetRef = srcDoc.layers.getByName("shadow"); 

    srcDoc.activeLayer = layerSetRef ; 

 

hasMask();//controlla se esiste una maschera di livello su shadow e in tal caso la cancella 

 

 

function hasMask() {   

    var srcDoc = app.activeDocument; 

          var lm = true, pmd;   

          try {   

                    pmd = srcDoc.activeLayer.layerMaskDensity;   

                    srcDoc.activeLayer.layerMaskDensity = 50.0;   

                    srcDoc.activeLayer.layerMaskDensity = pmd;   

          } catch(e) { lm = false };   

 

 

          if (lm == true) { 

               function sTT(v) {return stringIDToTypeID(v)}   

   

                (ref = new ActionReference()).putEnumerated   

                (sTT('channel'), sTT('channel'), sTT('mask'));   

                (dsc = new ActionDescriptor()).putReference(sTT('null'), ref)   

                executeAction(sTT('select'), dsc);   

                   

                  var idDlt = charIDToTypeID( "Dlt " ); 

                    var desc110 = new ActionDescriptor(); 

                    var idnull = charIDToTypeID( "null" ); 

                        var ref49 = new ActionReference(); 

                        var idChnl = charIDToTypeID( "Chnl" ); 

                        var idOrdn = charIDToTypeID( "Ordn" ); 

                        var idTrgt = charIDToTypeID( "Trgt" ); 

                        ref49.putEnumerated( idChnl, idOrdn, idTrgt ); 

                    desc110.putReference( idnull, ref49 ); 

                    var idAply = charIDToTypeID( "Aply" ); 

                    desc110.putBoolean( idAply, true ); 

                executeAction( idDlt, desc110, DialogModes.NO ); 

          } 

};   

function searchSO(){ 

var srcDoc = app.activeDocument; 

srcDoc.activeLayer = srcDoc.layers.getByName("style"); 

 

 

function openSO (){ 

var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" ); 

    var desc7 = new ActionDescriptor(); 

executeAction( idplacedLayerEditContents, desc7, DialogModes.NO ); 

 

 

function applicaVisibile(){ 

var newLayer = activeDocument.artLayers.add(); 

var idMrgV = charIDToTypeID( "MrgV" ); 

    var desc3 = new ActionDescriptor(); 

    var idDplc = charIDToTypeID( "Dplc" ); 

    desc3.putBoolean( idDplc, true ); 

executeAction( idMrgV, desc3, DialogModes.NO ); 

 

var newDoc = app.activeDocument; 

 

newDoc.activeLayer.name = "Complete"; 

    var quota = "quota"; 

 

var layName = newDoc.activeLayer.name;  

var l = Number(newDoc.layers.length)+1;   

while(--l){   

if(newDoc.layers[l-1].name!=layName && newDoc.layers[l-1].name!=quota){   

newDoc.layers[l-1].remove();   

}   

else{continue;}   

}   

 

newDoc.pathItems.removeAll(); 

newDoc.channels.removeAll(); 

 

newDoc.save(); 

newDoc.close(); 

 

 

function salvaTiffDEF(){ 

 

tiffFile = new File(activeDocument.path + "/" + activeDocument.name ) 

tiffSaveOptions = new TiffSaveOptions() 

TiffSaveOptions.embedColorProfile = true 

TiffSaveOptions.layers = true 

TiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW 

app.activeDocument.saveAs(tiffFile, tiffSaveOptions, true, Extension.LOWERCASE) 

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 

2 replies

Kukurykus
Legend
July 26, 2018

You may also use this if you recreate a little  your script, so instead of putting files to array you can to process each at the time it's its turn:

function FSf(sF, dst, i) {

     sF = Folder(sF).getFiles()

     for(; i < sF.length; i++) {

          if (sF instanceof Folder) {

               dst.push(sF)

               FSf(sF, dst, 0)

          }

          else {

               open(sF)// tasks:

               aD = activeDocument

               aD.save(), aD.close()

          }

     }

     return dst

}

FSf(Folder.selectDialog

('Top Folder:'), [], 0)

Just specify file format in getFiles(), then instead of aD = activeDocument put your script or call the function with and finally instead of aD.save(), aD.close() use your saving options.

r-binCorrect answer
Legend
July 25, 2018

Try this. Should work.

Handles all subfolders.

// JavaScript Document 

///////////////////////////////////////////////////

function init_files(folder, mask)

    {

    try

        {

        function push_files(folder)

            {

            try

                {

                var f = folder.getFiles();

                for (var i = 0; i < f.length; i++)

                    {

                    if (f instanceof Folder)

                        push_files(f);

                    else

                        if ( f.name.match(RegExp(mask+"$", "i")) ) files.push(f);

                    }

                f = null;

                }

            catch (e) { _alert(e); }

            }

        push_files(folder);

        }

    catch (e) { _alert(e); }

    }

///////////////////////////////////////////////////

 

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pdfSaveOpts; 

var i = 0; 

 

 

// Select the source folder. 

sourceFolder = Folder.selectDialog( 'Seleziona la cartella di origine'); 

 

 

// If a valid folder is selected 

if ( sourceFolder != null ) 

    files = new Array(); 

    fileType = ".psd";

    init_files(sourceFolder, fileType); 

 

if ( files.length > 0 ) 

    { 

        // Get the destination to save the files 

        //destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted .tif files.', '~' ); 

        destFolder = sourceFolder; 

        for ( i = 0; i < files.length; i++ ) 

        { 

            sourceDoc = app.open(files); // returns the document object 

 

 

     var docRef = sourceDoc; 

 

with (docRef) { 

 

chiusuraPesa(); 

 

 

 

////////////////////////////////////////////////////////////////// 

//           Next everything is unchanged                       //

////////////////////////////////////////////////////////////////// 

 

 

function chiusuraPesa(){ 

cercaShadow(); 

searchSO(); //cerca style 

openSO(); //apre oggetto avanzato 

applicaVisibile(); // applica visibile, via tracciati e canali, salva e chiude l'oggetto avanzato 

salvaTiffDEF(); //salva il tiff definitivo  

 

 

 

 

function cercaShadow(){ 

var srcDoc = app.activeDocument; 

srcDoc.channels.removeAll(); 

var layerSetRef = srcDoc.layerSets.getByName("shadow"); 

//srcDoc.activeLayer = srcDoc.layers.getByName("style"); 

layerSetRef.merge(); 

    var layerSetRef = srcDoc.layers.getByName("shadow"); 

    srcDoc.activeLayer = layerSetRef ; 

 

hasMask();//controlla se esiste una maschera di livello su shadow e in tal caso la cancella 

 

 

function hasMask() {   

    var srcDoc = app.activeDocument; 

          var lm = true, pmd;   

          try {   

                    pmd = srcDoc.activeLayer.layerMaskDensity;   

                    srcDoc.activeLayer.layerMaskDensity = 50.0;   

                    srcDoc.activeLayer.layerMaskDensity = pmd;   

          } catch(e) { lm = false };   

 

 

          if (lm == true) { 

               function sTT(v) {return stringIDToTypeID(v)}   

   

                (ref = new ActionReference()).putEnumerated   

                (sTT('channel'), sTT('channel'), sTT('mask'));   

                (dsc = new ActionDescriptor()).putReference(sTT('null'), ref)   

                executeAction(sTT('select'), dsc);   

                   

                  var idDlt = charIDToTypeID( "Dlt " ); 

                    var desc110 = new ActionDescriptor(); 

                    var idnull = charIDToTypeID( "null" ); 

                        var ref49 = new ActionReference(); 

                        var idChnl = charIDToTypeID( "Chnl" ); 

                        var idOrdn = charIDToTypeID( "Ordn" ); 

                        var idTrgt = charIDToTypeID( "Trgt" ); 

                        ref49.putEnumerated( idChnl, idOrdn, idTrgt ); 

                    desc110.putReference( idnull, ref49 ); 

                    var idAply = charIDToTypeID( "Aply" ); 

                    desc110.putBoolean( idAply, true ); 

                executeAction( idDlt, desc110, DialogModes.NO ); 

          } 

};   

function searchSO(){ 

var srcDoc = app.activeDocument; 

srcDoc.activeLayer = srcDoc.layers.getByName("style"); 

 

 

function openSO (){ 

var idplacedLayerEditContents = stringIDToTypeID( "placedLayerEditContents" ); 

    var desc7 = new ActionDescriptor(); 

executeAction( idplacedLayerEditContents, desc7, DialogModes.NO ); 

 

 

function applicaVisibile(){ 

var newLayer = activeDocument.artLayers.add(); 

var idMrgV = charIDToTypeID( "MrgV" ); 

    var desc3 = new ActionDescriptor(); 

    var idDplc = charIDToTypeID( "Dplc" ); 

    desc3.putBoolean( idDplc, true ); 

executeAction( idMrgV, desc3, DialogModes.NO ); 

 

var newDoc = app.activeDocument; 

 

newDoc.activeLayer.name = "Complete"; 

    var quota = "quota"; 

 

var layName = newDoc.activeLayer.name;  

var l = Number(newDoc.layers.length)+1;   

while(--l){   

if(newDoc.layers[l-1].name!=layName && newDoc.layers[l-1].name!=quota){   

newDoc.layers[l-1].remove();   

}   

else{continue;}   

}   

 

newDoc.pathItems.removeAll(); 

newDoc.channels.removeAll(); 

 

newDoc.save(); 

newDoc.close(); 

 

 

function salvaTiffDEF(){ 

 

tiffFile = new File(activeDocument.path + "/" + activeDocument.name ) 

tiffSaveOptions = new TiffSaveOptions() 

TiffSaveOptions.embedColorProfile = true 

TiffSaveOptions.layers = true 

TiffSaveOptions.imageCompression = TIFFEncoding.TIFFLZW 

app.activeDocument.saveAs(tiffFile, tiffSaveOptions, true, Extension.LOWERCASE) 

app.activeDocument.close(SaveOptions.DONOTSAVECHANGES); 

MattiaVoso
Known Participant
July 26, 2018

WOOOW THANK YOU A LOT !!!!!!