25 Replies Latest reply on May 22, 2017 5:21 AM by Silly-V

    Export Sublayers to JPG

    jeffreye38338035

      Hello,

       

      I'm trying to find a script that can export sublayers to JPG files. I did find some scripts that work but they only look at the top-layers, not at the sublayers.

      My Illustrator files looks like this:

      Schermafbeelding 2017-04-13 om 14.55.17.png

       

      What I want is a a script that will export the sub-layers in the layer Print into individual JPG files. The filename will be the name of the file itself + the layer name of that specific layer. So Filename_Option 1.jpg etc... Sometimes we have a second layer with sublayers that also needs to be exported to JPG's. I'm thinking to make an Action that first will delete the layers Languages, Working sheet and Background leaving only the layers in place that needs to be exported. But I can't find a script that can export the sub-layers.

       

      This is the script I could find that almost does the job: Scripting Illustrator: Export Layers as Images

      But this script only exports the layer Print and does not look at the sublayers in it.

      I hope someone can help!

       

      Jeffrey

        • 2. Re: Export Sublayers to JPG
          Silly-V Adobe Community Professional

          Try this script out, it exports a jpeg with all default options.

           

          #target illustrator

          function test(){

           

            var doc = app.activeDocument;

            var destPath = (doc.path != "")? doc.path : "~/Desktop";

            var hiddenTopLayers = [], thisTopLayer;

            for(var i=0; i<doc.layers.length; i++){ // hide all visible top-level layers

              thisTopLayer = doc.layers[i];

              if(thisTopLayer.name != "Print" && thisTopLayer.visible == true){

                thisTopLayer.visible = false;

                hiddenTopLayers.push(thisTopLayer);

              }

            };

            var printLayer = doc.layers.getByName("Print"), thisSubLayer, thisSubLayer2;

            for(var i=0; i<printLayer.layers.length; i++){ // go through every "Print" sublayer and hide all others to show only the one

              thisSubLayer = printLayer.layers[i];

              for(var j=0; j<printLayer.layers.length; j++){

                thisSubLayer2 = printLayer.layers[j];

                if(j == i){

                  thisSubLayer2.visible = true;

                } else {

                  thisSubLayer2.visible = false;

                }

              };

               // export the file with all default options

              doc.exportFile(File( destPath + "/" + doc.name.replace(/\.\w{2,3}$/, '') + "_" + thisSubLayer.name ), ExportType.JPEG, new ExportOptionsJPEG());

            };

            for(var i=0; i<doc.layers.length; i++){ // make visible all previously-hidden top-level layers

              doc.layers[i].visible = true;

            };

          };

          test();

          • 3. Re: Export Sublayers to JPG
            jeffreye38338035 Level 1

            Thanks but this script doesn't seem to work. I tried it in Illustrator CS4 and in the newest Illustrator CC. I'm not into the scripting.

            I just pasted this into a .jsx script file and then went to Illustrator > File > Scripts > Other Script to load this. Maybe I'm doing something wrong?

            • 4. Re: Export Sublayers to JPG
              Silly-V Adobe Community Professional

              Hmm, I did notice a mistake at the end of the script, which should not affect the general ability though. Do you have your "Print" layer in the document? And, is it saved - because if it's new and not saved, the files will be exported to the desktop.

              1 person found this helpful
              • 5. Re: Export Sublayers to JPG
                jeffreye38338035 Level 1

                Unfortunately I still can't get it to work...

                Silly-V  schreef

                 

                Do you have your "Print" layer in the document? And, is it saved - because if it's new and not saved, the files will be exported to the desktop.

                Yes, the Print layer is in the document and the document is saved. Then I try to load the script but nothing happens. No files on my desktop...

                • 6. Re: Export Sublayers to JPG
                  Silly-V Adobe Community Professional

                  So are you sure that what you have inside of the Print layer are sub-layers and not just groups?

                  • 7. Re: Export Sublayers to JPG
                    jeffreye38338035 Level 1

                    How can I check the difference? If I unfold a ''sub-layer/group'' in the Print layer this is what I get:

                    Schermafbeelding 2017-04-18 om 16.32.25.png

                     

                    So as you can see there are multiple objects inside the ''sub-layers/groups'' inside the Print layer but I don't know how to see the difference between a sub-layer or a group.

                    • 8. Re: Export Sublayers to JPG
                      jeffreye38338035 Level 1

                      I just read this:

                      ''Layers and sublayers appear with a grey background in the layer window. Groups appear with a white background in the layer window. But they both seem to have an identical behaviour.''

                      Source: Adobe Illustrator - What is the difference between a Layer and a Group? - Stack Overflow

                       

                      As you can see the background of the ''sub-layers'' are white meaning they are Groups (I think)

                       

                       

                      • 9. Re: Export Sublayers to JPG
                        Silly-V Adobe Community Professional

                        Okay give this one a try now.

                         

                        #target illustrator

                        function test(){

                         

                          var doc = app.activeDocument;

                          var destPath = (doc.path != "")? doc.path : "~/Desktop";

                          var hiddenTopLayers = [], thisTopLayer;

                          for(var i=0; i<doc.layers.length; i++){ // hide all visible top-level layers

                            thisTopLayer = doc.layers[i];

                            if(thisTopLayer.name != "Print" && thisTopLayer.visible == true){

                              thisTopLayer.visible = false;

                              hiddenTopLayers.push(thisTopLayer);

                            }

                          };

                          var printLayer = doc.layers.getByName("Print"), thisSubLayer, thisSubLayer2;

                          for(var i=0; i<printLayer.groupItems.length; i++){ // go through every "Print" sublayer group and hide all others to show only the one

                            thisSubLayer = printLayer.groupItems[i];

                            for(var j=0; j<printLayer.groupItems.length; j++){

                              thisSubLayer2 = printLayer.groupItems[j];

                              if(j == i){

                                thisSubLayer2.hidden = false;

                              } else {

                                thisSubLayer2.hidden = true;

                              }

                            };

                             // export the file with all default options

                            doc.exportFile(File( destPath + "/" + doc.name.replace(/\.\w{2,3}$/, '') + "_" + thisSubLayer.name ), ExportType.JPEG, new ExportOptionsJPEG());

                          };

                          for(var i=0; i<hiddenTopLayers.length; i++){ // make visible all previously-hidden top-level layers

                            hiddenTopLayers[i].visible = true;

                          };

                        };

                        test();

                        • 10. Re: Export Sublayers to JPG
                          jeffreye38338035 Level 1

                          Thanks a lot for your help so far!

                          But with this script I'm getting an error (tried it in CS4 and CC):

                          Schermafbeelding 2017-04-19 om 10.12.04.png

                          Schermafbeelding 2017-04-19 om 10.16.55.png

                          • 11. Re: Export Sublayers to JPG
                            Silly-V Adobe Community Professional

                            Hmm, can you post a link to your file? I want to be able to do a test on it.

                            • 12. Re: Export Sublayers to JPG
                              jeffreye38338035 Level 1

                              No problem, you can download two test AI files with this link:

                              https://we.tl/XN5DyqE63t

                               

                              (files will automatically be deleted in 7 days)

                              • 13. Re: Export Sublayers to JPG
                                claudieg72423534

                                It doesn't work for me (I use illustratorCC). But I think that script have a lot of potential.I'd like to turn it into a .eps files save, with a destination folder. Do you think it would be possible?

                                • 14. Re: Export Sublayers to JPG
                                  Silly-V Adobe Community Professional

                                  OH, I think that's cause you've got "print" and my script , at least one part of it works with it being case-sensitive, so it was looking for "Print". For those who are not aware, the getByName() function is case-insensitive - so the script failed only due to the very first part of the routine where it hides all layers not named "Print". It does so by comparing names exactly, so because "print" isn't "Print", the "print" layer got hidden. The hiding is so any other layers that are not the "Print" will be out of view when a JPG is exported, and at the very end those layers which were hidden get un-hidden again to return to the original view. Well, since the layer we want to work on ("print") got hidden, the rest of the script fails because you can't mess with items of a layer which is locked or hidden (I'm sure there's various situations when this is true & not, for example you can probably rename something via script inside a hidden layer... So - what can you do, you can either stick to calling them "Print", or just replace all "Print" with "print" in the script. And yes it's definitely groups, not sub-layers - which could also be confusing at first because of the colloquial tendency to refer to any art object inside a top-level layer as "sublayer" whereas those objects could be groups and paths and images, etc.

                                  • 15. Re: Export Sublayers to JPG
                                    jeffreye38338035 Level 1

                                    Great!! It works, thanks a lot! I'm almost there, two more questions and if that works it's PERFECT

                                     

                                    Question 1

                                    Is there any way the export quality of the exported JPG can be set? Now it's exported at (low quality) 72DPI. For me it works better to get 300DPI JPG files. Where can this be changed?

                                     

                                    Question 2

                                    Also, in the second test file I send you you can see a few other layers+sublayers that needs to be exported.

                                    Schermafbeelding 2017-04-24 om 08.44.45.png

                                    Now, the script only looks at the Print layer. Is there a way you tell the script that he has to export every single layer+sublayer except Languages, Working sheet & Background ? Because sometimes we have files that have look like that also. So more than the Print layer.

                                     

                                    Now that I'm starting to understand how the script works, I think that when the script is told that Languages, Working sheet & Background should be hidden and the other layers should be exported it should work right? Because these 3 layers are always there and always shouldn't be exported.

                                    • 16. Re: Export Sublayers to JPG
                                      jeffreye38338035 Level 1

                                      @Silly-V

                                      I'm hoping you can answer my last question. Because that's the only thing that I would like to have changed. After that the script is perfect. Thanks for your help so far!

                                      • 17. Re: Export Sublayers to JPG
                                        Silly-V Adobe Community Professional

                                        Hello there, say, can you upload a file that's a typical one which describes your 2nd question situation for my testing purposes please?

                                        • 18. Re: Export Sublayers to JPG
                                          jeffreye38338035 Level 1

                                          Sure!

                                          Download link: https://we.tl/sK9yOKPNyo (files will automatically be deleted in 7 days)

                                          Which contains:

                                          • Test file-1.ai
                                          • Test file-2-extra.ai
                                          • Test file-3-extra.ai

                                           

                                          Test file-1.ai is the exact same one which I'm describing in my 2nd question.

                                          As you can see I also added two extra test files so you have enough files to try the script on.

                                          • 19. Re: Export Sublayers to JPG
                                            Silly-V Adobe Community Professional

                                            OkY, I got quite a bit of work to get through, hopefully can squeeze this in sometime soon.

                                            • 20. Re: Export Sublayers to JPG
                                              jeffreye38338035 Level 1

                                              Great! No rush man! Just look at it when you have time, no problem!

                                              I'm already very thankful for all your help!

                                              • 21. Re: Export Sublayers to JPG
                                                jeffreye38338035 Level 1

                                                Silly-V Not trying to rush you but did you had a chance to look at this?

                                                • 22. Re: Export Sublayers to JPG
                                                  Silly-V Adobe Community Professional

                                                  Hey, I think their inbox thing is broken - I can't access or read or click on any of your communication messages

                                                  Guess we'll have to chat right here. Can you please put the link back up, I need to DL the files on my other computer, sorry for the delay!

                                                  • 23. Re: Export Sublayers to JPG
                                                    jeffreye38338035 Level 1

                                                    Oh no problem! Thanks for answering anyway

                                                    This is the new link with the same files as the first download link!

                                                    https://we.tl/NjypMn6pWV

                                                     

                                                    Which contains:

                                                    • Test file-1.ai
                                                    • Test file-2-extra.ai
                                                    • Test file-3-extra.ai

                                                     

                                                    Test file-1.ai is the exact same one which I'm describing in my 2nd question.

                                                    As you can see I also added two extra test files so you have enough files to try the script on.

                                                    • 24. Re: Export Sublayers to JPG
                                                      jeffreye38338035 Level 1

                                                      Silly-V Have you downloaded the files already? Because the link is almost expired.

                                                      • 25. Re: Export Sublayers to JPG
                                                        Silly-V Adobe Community Professional

                                                        Yes, I got them all on all my machines. Sorry I've been caught up with this thing I have to get ready before I go to this convention, for a 5-minute demonstration among the people who are tons and tons smarter than I! But I'm working on this on my home computer, I'll get to it asap.