22 Replies Latest reply: Sep 15, 2009 2:40 AM by mazmax RSS

    Export Groups to Files in Folders

    mazmax Community Member

      I'm following up on the great script posted here:

      http://forums.adobe.com/thread/488255?tstart=0

       

      I'm trying to add 2 things

       

      • Save using ExportType.SAVEFORWEB
      • Display an options box in the dialog to choose whether to save the files in separate folders named after the group names

       

      But I cannot get any of those to work.

       

      Any ideas how to do it?

       

      Thanks!

        • 1. Re: Export Groups to Files in Folders
          c.pfaffenbichler Community Member

          Good day!

          It’s monday morning here and work has begun, so I’m not able to look into those things right away, but I’ll probably be able to check it out in the course of the week.

           

          As regards the optionsbox You could add a checkbox-item (in any panel really), set up an if-clause referring to its value and depending on that create a new Folder in the proper location and modify the filepath for saving etc.

           

          Regards,

          Pfaffenbichler

          • 2. Re: Export Groups to Files in Folders
            c.pfaffenbichler Community Member

            Turns out I’ve exaggerated …

            You might have to edit the saveForWeb-settings to Your requirements, but You could give this a try:

             

            // creates save for web-jpg-copies of layersets with dialog to select layersets and suffix;

            // be advised: existing files of identical names will be overwritten without warnings;

            // 2009, pfaffenbichler, use it at your own risk;

            #target photoshop

            if (app.documents.length > 0) {

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

            // get document-path and -title;

            var myDocument = app.activeDocument;

            var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

            var myPath = myDocument.path;

            var theLayerSets = collectLayerSets(myDocument);

            ////// filter for checking if entry is numeric, thanks to xbytor //////

            numberKeystrokeFilter = function() {

            this.text = this.text.replace(",", "");

            this.text = this.text.replace(".", "");

            if (this.text.match(/[^\-\.\d]/)) {

            this.text = this.text.replace(/[^\-\.\d]/g, "");

            };

            if (this.text == "") {

            this.text = "0"

            }

            };

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

            // create the dialog;

            var dlg = new Window('dialog', "pdfs from layersets", [500,300,930,870]);

            //create list for layer-selection;

            dlg.layerRange = dlg.add('panel', [21,20,279,475], "select layersets to create pdfs of");

            dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,435], '', {multiselect: true});

            for (var q = 0; q < theLayerSets.length; q++) {

            dlg.layerRange.layersList.add ("item", theLayerSets[q].name);

            dlg.layerRange.layersList.items[q].selected = true

            };

            // entry for suffix;

            dlg.suffix = dlg.add('panel', [290,20,410,85], "enter suffix");

            dlg.suffix.suffixText = dlg.suffix.add('edittext', [11,20,99,40], "", {multiline:false});

            // entry for number;

            dlg.number = dlg.add('panel', [290,100,410,165], "start with #");

            dlg.number.startNumber = dlg.number.add('edittext', [11,20,45,40], "1", {multiline:false});

            dlg.number.addNumber = dlg.number.add('checkbox', [55,20,105,40], "add", {multiline:false});

            dlg.number.startNumber.onChange = numberKeystrokeFilter;

            // field to add layer-name;

            dlg.layerName = dlg.add('panel', [290,180,410,270], "layer-name");

            dlg.layerName.doAddName = dlg.layerName.add('radiobutton', [11,20,120,40], "add it");

            dlg.layerName.dontAddName = dlg.layerName.add('radiobutton', [11,45,120,65], "don’t add it");

            dlg.layerName.doAddName.value = true;

            // field to select target-folder;

            dlg.target = dlg.add('panel', [290,285,410,475], "target-folder");

            dlg.target.folderPerSet = dlg.target.add('checkbox', [11,20,100,40], "folder each");

            dlg.target.targetSel = dlg.target.add('button', [11,50,100,70], "select");

            dlg.target.targetField = dlg.target.add('statictext', [11,80,100,185], String(myPath), {multiline:true});

            dlg.target.targetSel.onClick = function () {

            var target = Folder.selectDialog("select a target folder");

            dlg.target.targetField.text = target.fsName

            };

            // ok- and cancel-buttons;

            dlg.buildBtn = dlg.add('button', [220,490,410,505], 'OK', {name:'ok'});

            dlg.cancelBtn = dlg.add('button', [21,490,210,505], 'Cancel', {name:'cancel'});

            dlg.warning = dlg.add('statictext', [21,520,410,560], "be advised: existing files of the same name will be replaced without prompting", {multiline: true});

            dlg.center();

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

            var myReturn = dlg.show ();

            // in case of OK;

            if (myReturn == true && dlg.layerRange.layersList.selection.length > 0) {

            // get the number instead of the name;

            var theLayerSelection = new Array;

            var theColl = dlg.layerRange.layersList.items;

            for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) {

            if (dlg.layerRange.layersList.items[p].selected == true) {

            theLayerSelection = theLayerSelection.concat(p);

            }

            };

            // collect the rest of the variables,

            var theSuffix = dlg.suffix.suffixText.text;

            var theNumber = Number (dlg.number.startNumber.text) - 1;

            var theLayerNameAdd = dlg.layerName.doAddName.value;

            var theDestination = dlg.target.targetField.text;

            var theNumbering = dlg.number.addNumber.value;

            var folderEach = dlg.target.folderPerSet.value;

            // save for web options;

            var webOptions = new ExportOptionsSaveForWeb();

            webOptions.format = SaveDocumentType.JPEG;

            webOptions.includeProfile = false;

            webOptions.interlaced = 0;

            webOptions.optimized = true;

            webOptions.quality = 80;

            // create the pdf-name;

            if (theSuffix.length > 0) {

            var aSuffix = "_" + theSuffix

            }

            else {

            var aSuffix = ""

            };

            // create a flattened copy;

            var theCopy = myDocument.duplicate("thecopy", true);

            // do the operation;

            for (var m = theLayerSelection.length - 1; m >= 0; m--) {

            app.activeDocument = myDocument;

            var theLayer = theLayerSets[theLayerSelection[m]];

            if (theNumbering == true) {

            theNumber = bufferNumberWithZeros((Number (theNumber) + 1), 2);

            theNumberString =  "_" + theNumber

            };

            else {

            theNumberString = ""

            };

            // get the layername for the pdf-name;

            if (theLayerNameAdd == true) {

            var aLayerName = "_" + theLayer.name.replace("/", "_")

            }

            else {

            var aLayerName = ""

            }

            // transfer layerset over to the copy;

            theLayer.duplicate (theCopy, ElementPlacement.PLACEATBEGINNING);

            app.activeDocument = theCopy;

            // create the individual folders;

            if (folderEach == true) {

            var aFolder = new Folder (theDestination+"/"+(theLayer.name.replace("/", "_"))).create();

            var theDestination2 = theDestination+"/"+(theLayer.name.replace("/", "_"))

            }

            else {theDestination2 = theDestination};

            // hide the llast added layer;

            theCopy.layers[1].visible = false;

            theCopy.exportDocument(new File(theDestination2+"/"+myDocName+aSuffix+aLayerName+theNumberString+".jpg"), ExportType.SAVEFORWEB, webOptions);

            };

            theCopy.close(SaveOptions.DONOTSAVECHANGES);

            }

            };

            else {alert ("no document open")};

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

            ////// buffer number with zeros //////

            function bufferNumberWithZeros (number, places) {

            var theNumberString = String(number);

            for (var o = 0; o < (places - String(number).length); o++) {

            theNumberString = String("0" + theNumberString)

            };

            return theNumberString

            };

            ////// function collect all layersets //////

            function collectLayerSets (theParent) {

            if (!allLayerSets) {

            var allLayerSets = new Array}

            else {};

            for (var m = theParent.layers.length - 1; m >= 0;m--) {

            var theLayer = theParent.layers[m];

            // apply the function to layersets;

            if (theLayer.typename == "ArtLayer") {

            // allLayerSets = allLayerSets.concat(theLayer)

            }

            else {

            // this line includes the layer groups;

            allLayerSets = allLayerSets.concat(theLayer);

            allLayerSets = allLayerSets.concat(collectLayerSets(theLayer))

            }

            }

            return allLayerSets

            };

            • 3. Re: Export Groups to Files in Folders
              mazmax Community Member

              Great! Thanks Chris

               

              It's Monday morning here in London as well and I'm at work too, gonna try and do what I can with my bare basic js skills

              • 4. Re: Export Groups to Files in Folders
                c.pfaffenbichler Community Member

                Does the Script I posted not work?

                • 5. Re: Export Groups to Files in Folders
                  mazmax Community Member

                  Yep that works great Chris! Wunderbar

                   

                  I'm testing it here with a PSD which at top level has graphical layers and language layer groups containing the translated texts

                   

                  As things are now, to have it export the complete localized versions, I should duplicate the graphic layers for each language and move the copies inside the groups.

                   

                  Would there be a way to have it to export the graphical layers and alternate the language layergroups?

                   

                  It looks a bit like this:

                   

                  - button

                  - image

                  --- [english]

                  --- [german]

                  --- [french]

                  - gradient

                  - background

                  • 6. Re: Export Groups to Files in Folders
                    c.pfaffenbichler Community Member

                    In that case the whole layerSet-selection would appear to be moot.

                    One could instead just set the layersets to invisible and save the files out with one of them visible at a time.

                    Are there more than one file of this layer-structure and is it the same in all the files, are the names identical, too?

                    Could You, just to make sure, post a Screenshot of the Layers-panel?

                    • 7. Re: Export Groups to Files in Folders
                      mazmax Community Member

                      Yes all of the files have this kind of structure normally, also the layer groups typically have the same name too (Sometimes it's 'English' and sometimes it's 'UK', or 'French' and FR), only sometimes there are more languages and sometimes fewer.

                       

                      I'm attaching a screengrab of the layers panel

                      • 8. Re: Export Groups to Files in Folders
                        c.pfaffenbichler Community Member

                        I’ve added a clause to show only one of the selected layerSets at a copy and leave the rest of the layers’ visibility unchanged … maybe it works for Your purpose:

                         

                        // creates save for web-jpg-copies of mutually hidden layersets with dialog to select layersets and suffix;

                        // be advised: existing files of identical names will be overwritten without warnings;

                        // 2009, pfaffenbichler, use it at your own risk;

                        #target photoshop

                        if (app.documents.length > 0) {

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

                        // get document-path and -title;

                        var myDocument = app.activeDocument;

                        var myDocName = myDocument.name.match(/(.*)\.[^\.]+$/)[1];

                        var myPath = myDocument.path;

                        var theLayerSets = collectLayerSets(myDocument);

                        ////// filter for checking if entry is numeric, thanks to xbytor //////

                        numberKeystrokeFilter = function() {

                        this.text = this.text.replace(",", "");

                        this.text = this.text.replace(".", "");

                        if (this.text.match(/[^\-\.\d]/)) {

                        this.text = this.text.replace(/[^\-\.\d]/g, "");

                        };

                        if (this.text == "") {

                        this.text = "0"

                        }

                        };

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

                        // create the dialog;

                        var dlg = new Window('dialog', "pdfs from layersets", [500,300,930,870]);

                        //create list for layer-selection;

                        dlg.layerRange = dlg.add('panel', [21,20,279,475], "select layersets to create pdfs of");

                        dlg.layerRange.layersList = dlg.layerRange.add('listbox', [11,20,240,435], '', {multiselect: true});

                        for (var q = 0; q < theLayerSets.length; q++) {

                        dlg.layerRange.layersList.add ("item", theLayerSets[q].name);

                        dlg.layerRange.layersList.items[q].selected = true

                        };

                        // entry for suffix;

                        dlg.suffix = dlg.add('panel', [290,20,410,85], "enter suffix");

                        dlg.suffix.suffixText = dlg.suffix.add('edittext', [11,20,99,40], "", {multiline:false});

                        // entry for number;

                        dlg.number = dlg.add('panel', [290,100,410,165], "start with #");

                        dlg.number.startNumber = dlg.number.add('edittext', [11,20,45,40], "1", {multiline:false});

                        dlg.number.addNumber = dlg.number.add('checkbox', [55,20,105,40], "add", {multiline:false});

                        dlg.number.startNumber.onChange = numberKeystrokeFilter;

                        // field to add layer-name;

                        dlg.layerName = dlg.add('panel', [290,180,410,270], "layer-name");

                        dlg.layerName.doAddName = dlg.layerName.add('radiobutton', [11,20,120,40], "add it");

                        dlg.layerName.dontAddName = dlg.layerName.add('radiobutton', [11,45,120,65], "don’t add it");

                        dlg.layerName.doAddName.value = true;

                        // field to select target-folder;

                        dlg.target = dlg.add('panel', [290,285,410,475], "target-folder");

                        dlg.target.folderPerSet = dlg.target.add('checkbox', [11,20,100,40], "folder each");

                        dlg.target.targetSel = dlg.target.add('button', [11,50,100,70], "select");

                        dlg.target.targetField = dlg.target.add('statictext', [11,80,100,185], String(myPath), {multiline:true});

                        dlg.target.targetSel.onClick = function () {

                        var target = Folder.selectDialog("select a target folder");

                        dlg.target.targetField.text = target.fsName

                        };

                        // ok- and cancel-buttons;

                        dlg.buildBtn = dlg.add('button', [220,490,410,505], 'OK', {name:'ok'});

                        dlg.cancelBtn = dlg.add('button', [21,490,210,505], 'Cancel', {name:'cancel'});

                        dlg.warning = dlg.add('statictext', [21,520,410,560], "be advised: existing files of the same name will be replaced without prompting", {multiline: true});

                        dlg.center();

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

                        var myReturn = dlg.show ();

                        // in case of OK;

                        if (myReturn == true && dlg.layerRange.layersList.selection.length > 0) {

                        // get the number instead of the name;

                        var theLayerSelection = new Array;

                        var theColl = dlg.layerRange.layersList.items;

                        for (var p = 0; p < dlg.layerRange.layersList.items.length; p++) {

                        if (dlg.layerRange.layersList.items[p].selected == true) {

                        theLayerSelection = theLayerSelection.concat(p);

                        }

                        };

                        // collect the rest of the variables,

                        var theSuffix = dlg.suffix.suffixText.text;

                        var theNumber = Number (dlg.number.startNumber.text) - 1;

                        var theLayerNameAdd = dlg.layerName.doAddName.value;

                        var theDestination = dlg.target.targetField.text;

                        var theNumbering = dlg.number.addNumber.value;

                        var folderEach = dlg.target.folderPerSet.value;

                        // save for web options;

                        var webOptions = new ExportOptionsSaveForWeb();

                        webOptions.format = SaveDocumentType.JPEG;

                        webOptions.includeProfile = false;

                        webOptions.interlaced = 0;

                        webOptions.optimized = true;

                        webOptions.quality = 80;

                        // history state;

                        var historyState = myDocument.activeHistoryState;

                        // create the pdf-name;

                        if (theSuffix.length > 0) {

                        var aSuffix = "_" + theSuffix

                        }

                        else {

                        var aSuffix = ""

                        };

                        // do the operation;

                        for (var m = theLayerSelection.length - 1; m >= 0; m--) {

                        var theLayer = theLayerSets[theLayerSelection[m]];

                        theLayer.visible = true;

                        // numbers;

                        if (theNumbering == true) {

                        theNumber = bufferNumberWithZeros((Number (theNumber) + 1), 2);

                        theNumberString =  "_" + theNumber

                        };

                        else {

                        theNumberString = ""

                        };

                        // get the layername for the pdf-name;

                        if (theLayerNameAdd == true) {

                        var aLayerName = "_" + theLayer.name.replace("/", "_")

                        }

                        else {

                        var aLayerName = ""

                        }

                        // hide other layers;

                        for (var n = theLayerSelection.length - 1; n >= 0; n--) {

                        var aLayer = theLayerSets[theLayerSelection[n]];

                        if (aLayer != theLayer && aLayer.visible == true) {

                        aLayer.visible = false

                        }

                        };

                        // create the individual folders;

                        if (folderEach == true) {

                        var aFolder = new Folder (theDestination+"/"+(theLayer.name.replace("/", "_"))).create();

                        var theDestination2 = theDestination+"/"+(theLayer.name.replace("/", "_"))

                        }

                        else {theDestination2 = theDestination};

                        // hide the llast added layer;

                        myDocument.exportDocument(new File(theDestination2+"/"+myDocName+aSuffix+aLayerName+theNumberString+".jpg"), ExportType.SAVEFORWEB, webOptions);

                        };

                        myDocument.activeHistoryState = historyState;

                        }

                        };

                        else {alert ("no document open")};

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

                        ////// buffer number with zeros //////

                        function bufferNumberWithZeros (number, places) {

                        var theNumberString = String(number);

                        for (var o = 0; o < (places - String(number).length); o++) {

                        theNumberString = String("0" + theNumberString)

                        };

                        return theNumberString

                        };

                        ////// function collect all layersets //////

                        function collectLayerSets (theParent) {

                        if (!allLayerSets) {

                        var allLayerSets = new Array}

                        else {};

                        for (var m = theParent.layers.length - 1; m >= 0;m--) {

                        var theLayer = theParent.layers[m];

                        // apply the function to layersets;

                        if (theLayer.typename == "ArtLayer") {

                        // allLayerSets = allLayerSets.concat(theLayer)

                        }

                        else {

                        // this line includes the layer groups;

                        allLayerSets = allLayerSets.concat(theLayer);

                        allLayerSets = allLayerSets.concat(collectLayerSets(theLayer))

                        }

                        }

                        return allLayerSets

                        };

                        • 9. Re: Export Groups to Files in Folders
                          mazmax Community Member

                          Wow! I tried it on that same PSD and it worked great, even faster than the previous one

                           

                          I will test it on other PSD's... let's see...

                           

                          Chris you are a star

                          • 10. Re: Export Groups to Files in Folders
                            mazmax Community Member

                            Seems to be working fine with others, it's just giving the MACOS filename issue with all files ending "..._#0"

                             

                            I tried adding "webOptions.byteOrder = ByteOrder.MACOS; " but it doesn't seem to be fixing the problem...

                            • 11. Re: Export Groups to Files in Folders
                              mazmax Community Member

                              Hmmm it's only giving that problem on 1 PSD though, I guess it's just that file that's messed up

                              • 12. Re: Export Groups to Files in Folders
                                mazmax Community Member

                                Still testing and working fine so far...

                                 

                                I've just come across some PSD which require to be exported with transparent PNG... I guess I could try and add options in the dialog box to select filetype JPG, PNG or GIF, transparency on/off, and input the quality with default 80.

                                • 13. Re: Export Groups to Files in Folders
                                  c.pfaffenbichler Community Member

                                  What is the exact filename and what are the layers’ names?

                                  It seems possible the layer-names could cause problems, if they contain »/«.

                                  Well, actually the Script causes the problem then, because one should consider such possibilities and ensure they don’t cause trouble, but it’s quite easy to overlook such eventualities.

                                  • 14. Re: Export Groups to Files in Folders
                                    mazmax Community Member

                                    The filename is BTS_homepage_split_musicgirl.psd and the output is BTS_homepage_split_musicgirl_#0.jpg

                                     

                                    The layers panel looks like the image attached

                                    • 15. Re: Export Groups to Files in Folders
                                      mazmax Community Member

                                      Chris, do you think it would be possible to have a script that having this initial situation:

                                       

                                       

                                      ----- [English]

                                          |-- text

                                          |-- text-2

                                          |-- text-3

                                       

                                      -- button

                                      -- gradient

                                      -- background

                                       

                                       

                                      Reads a CSV file like this

                                       

                                      lang         text          text-2          text-3

                                      English     text          text-2          text-3

                                      French     frtext         frtxt2          frtxt-3

                                      German    detex        de-txt2        detxt-3

                                      IT            ittex          it-txt2          it-txt-3

                                      ...

                                       

                                      And creates folders with the translations?

                                       

                                       

                                      ----- [IT]

                                          |-- text

                                          |-- text-2

                                          |-- text-3

                                       

                                      ----- [German]

                                          |-- text

                                          |-- text-2

                                          |-- text-3

                                       

                                      ----- [French]

                                          |-- text

                                          |-- text-2

                                          |-- text-3

                                       

                                      ----- [English]

                                          |-- text

                                          |-- text-2

                                          |-- text-3

                                       

                                      -- button

                                      -- gradient

                                      -- background

                                      • 16. Re: Export Groups to Files in Folders
                                        c.pfaffenbichler Community Member

                                        I think You could even do something along those lines with Image – Variables; You might want to check it out in the Help-file.

                                        Not my field of expertise though.

                                        • 17. Re: Export Groups to Files in Folders
                                          mazmax Community Member

                                          Hmmm I would only need text layers to be updated with localised content, not images. What help file do you mean?

                                          • 18. Re: Export Groups to Files in Folders
                                            c.pfaffenbichler Community Member

                                            Either the menu Help – Photoshop Help or (available somewhere on the Adobe-homepage) »photoshop_cs4_help.pdf«, a search should turn that up quickly.

                                             

                                            When You have a text-layer selected Variables offers Text Replacement, too.

                                            I have no real experience with that feature; but I suspect it might be easier than writing a Script for this.

                                            • 19. Re: Export Groups to Files in Folders
                                              mazmax Community Member

                                              Ah yeh data sets does that but it's all enclosed in the same layer and it won't split the translations in different text layers.

                                              • 20. Re: Export Groups to Files in Folders
                                                c.pfaffenbichler Community Member

                                                I thought it can split the texts … I think I’ve seen a phtoshopusertv-episode once where they demonstrated that, I can't find it now though.

                                                • 21. Re: Export Groups to Files in Folders
                                                  Michael L Hale Community Member

                                                  It can. You can define as many text replacements as you need. At least I have never hit a limit.

                                                  • 22. Re: Export Groups to Files in Folders
                                                    mazmax Community Member

                                                    I too hate when I remember seeing that something's possible but I cannot remember how they did it!