13 Replies Latest reply on Feb 27, 2012 10:16 PM by John Hawkinson

    [CS3 JS] Getting information about Photoshop images in InDesign

    Tom Tomasko Level 1

      Hello,

       

      Through an InDesign script I can get many pieces of information about an image such as the file name, resolution in PPI, dimension in inches, color mode, etc.

       

      What I am wondering is if it is possible to find out how many layers a Photoshop image may have. I would like a quick way to know if an image has been flattened (one layer) without having to open up the image in Photoshop.

       

      Tom

        • 1. Re: [CS3 JS] Getting information about Photoshop images in InDesign
          Muppet Mark Level 5

          One layer does NOT mean an image is flat…

          • 2. Re: [CS3 JS] Getting information about Photoshop images in InDesign
            Tom Tomasko Level 1

            I'm not sure what you mean. Every time out of thousands of times I have flattened an image (Layer>Flatten Image) the result is one layer.

             

            At any rate, I want to know how many layers a .tif, .psd or god-forbid a .jpeg has without having to open it up in Photoshop.

             

            Any pointers on where to go would be great.

             

            Tom

            • 3. Re: [CS3 JS] Getting information about Photoshop images in InDesign
              Muppet Mark Level 5

              Firstly I think you shouldn't need to test *.jpeg only *.tif & *.psd sure if you flatten a file the result is always going to be flat because the layer gets the 'isBackground' true. A single layered image can still contain transparency a test should really be for both conditions… That apart if you wanted to filter further using ID then…

               

              var i = app.activeDocument.pages[0].rectangles[0].images[0];
              
              $.writeln( i.graphicLayerOptions.graphicLayers.length );
              

               

              May work although Im not sure if its only top level… Not tested…

              • 4. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                Tom Tomasko Level 1

                Sorry, the length is always zero no matter how many layers are in the .tiff I am experminenting on. I'm not sure what "graphicLayers" is referrencing.

                 

                Tom

                • 5. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                  Muppet Mark Level 5

                  It should reference this stuff or so I thought… Mine returned 5. If you were confident on 'Background' you could have possibly included a name check of the last layer too…

                   

                  Screen shot 2012-01-19 at 20.19.07.png

                  • 6. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                    Tom Tomasko Level 1

                    I now see the problem. The Object>Object Layers Options was greyed out. The reason is that this option only works for .psd and PDF images. I had been applying your script to a .tif. When I apply it to a .psd the script works fine.

                     

                    So now my question is if there is some kind of work-around. Would it be possible with a script in ID to open up each image in Photoshop, count the layers and close it up and do this rapidly?

                     

                    By the way the graphicLayers.length returns 0 if there is only a background layer. It returns the number of all layers (including the background layer) if there is more than one layer. If there is no background layer it also returns the correct number of layers.

                     

                    Tom

                    • 7. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                      Tom Tomasko Level 1

                      I figured out a script that opens in Photoshop. It will count the layers of each file in a folder and spit out a .txt document with the information. It has a few bugs. You can only have .psd, .tif,  or .jpeg files in the folder. It wants to open up PDFs and eps files and save them as PSDs. And if there is a folder inside the folder the text document is a blank.

                       

                      Bu these can be solved later. I'm still wondering how this script could be initiated in ID so that I could use its results to make a contact sheet in ID.

                       

                      Here is the script:

                       

                      var fileList = listFiles();

                      var theText = "";

                      var fileName = fileList[0].parent.name;

                      try{

                      for(var i= fileList.length-1; i>= 0; i--){

                          var a = app.open(fileList[i]);

                          var b = a.name;

                          var c = a.artLayers.length;

                          theText+= c+" layers: "+b+"\r\r";

                          app.activeDocument.close();

                          }//end for

                      }//end try

                      catch (e){/*do nothing*/}

                      createTextFile(theText,fileName);

                      alert("I have created a folder called \"GraphicsCheck\" on your desktop which includes a text document that tells you how many layers are in each image contained in the folder:\r\r"+fileName);

                       

                      //******functions********

                      function chooseFolder(prompt){

                              var myFolder = Folder.selectDialog(prompt);

                              if(myFolder != null){

                                  return myFolder ;

                                  }//end if

                              else{

                                  throw ("User cancelled");

                                  }//end else

                              }//end chooseFolder

                       

                      function listFiles(){

                          var folderRef = chooseFolder("Select folder");

                              var fileList = folderRef.getFiles();

                          return fileList;

                          }//end listFiles

                       

                      function createTextFile(someText,folderWithFiles){

                          var folderPath = Folder("~/Desktop");

                          var folderName = "GraphicsCheck";

                          var fileName = folderWithFiles+"LayerCount.txt";

                          var filePath = folderPath+"/"+folderName+"/"+fileName;

                          Folder(folderPath+"/"+folderName).create();

                          var myFile = new File(filePath);

                          myFile.open("w", "TEXT");

                          myFile.write(someText);

                          myFile.close();

                      }//end function createTextFile

                      • 8. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                        Marijan Tompa [tomaxxi] Level 4

                        Tom,

                         

                        Here is short example how to count/parse all layers from InDesign.

                        You need to create recursive function

                        which will count layers in sub folders as well.

                        Make sure you select graphics frame with PSD placed in.

                         

                        var lCount = 0;
                        
                        countLayers(app.selection[0].graphics[0].graphicLayerOptions.graphicLayers);
                        
                        alert(lCount);
                        
                        function countLayers (level) {
                            for ( var i = 0 ; i < level.length ; i++ ) {
                                if ( level[i].graphicLayers.length == 0 )
                                    lCount ++;
                                else
                                    countLayers (level[i].graphicLayers);
                            }
                        }
                        

                         

                        Hope that helps.

                         

                        --

                        Marijan (tomaxxi)

                        http://tomaxxi.com

                        • 9. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                          Tom Tomasko Level 1

                          Marijan,

                           

                          As explained above the graphicLayerOptions object is only for .psds and PDFs. One cannot get a count of layers in a tif, which is the type of file I mostly use.

                           

                          Tom

                          • 10. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                            Kasyan Servetsky Level 5

                            You can do this via BridgeTalk, here is an example. Select a TIF or PSD image and run the script.

                             

                            const gScriptName = "Get number of layers";
                            const gScriptVersion = "1.0";
                            var gLayersNumber;
                            
                            Main();
                            
                            //===================================== FUNCTIONS  ======================================
                            function Main() {
                                var image, pixelDimensionsArr, folderPath, copyResult;
                                var doc = app.activeDocument;
                            
                                try {
                                    if (app.selection[0].constructor.name == "Image") {
                                        image = app.selection[0];
                                    }
                                    else if (app.selection[0].images.length > 0) {
                                        image = app.selection[0].images[0];
                                    }
                                    else {
                                        ErrorExit("The selection contains no image.");
                                    }
                                }
                                catch(err) {
                                    ErrorExit("The selection contains no image.");
                                }
                            
                                var link = image.itemLink;
                                if (!link.isValid) ErrorExit("Can't get the link.");
                            
                                if (link.linkType == "Photoshop" || link.linkType == "TIFF") {
                                    if (link.status == LinkStatus.NORMAL) {
                                        linkFile = new File(link.filePath);
                                        if (linkFile.exists) {
                                            CreateBridgeTalkMessage(link.filePath);
                                            alert("Number of layers: " + gLayersNumber);
                                        }
                                    }
                                }
                            }
                            //--------------------------------------------------------------------------------------------------------------------------------------------------------
                            function CreateBridgeTalkMessage(imagePath) {
                                var bt = new BridgeTalk();
                                bt.target = "photoshop";
                                var script = "GetLayersNumber = " + GetLayersNumber.toSource() + "\r";
                                script += "GetLayersNumber(\"" + imagePath + "\");";
                                bt.body = script;
                            
                                bt.onResult = function(resObj) {
                                    gLayersNumber = resObj.body;
                                }
                            
                                bt.onError = function(msg) {
                                    alert("Error: " + msg.body);
                                }
                            
                                bt.send(100);
                            }
                            //--------------------------------------------------------------------------------------------------------------------------------------------------------
                            function GetLayersNumber(imagePath) {
                                var f = new File(imagePath);
                                app.displayDialogs = DialogModes.NO;
                                var doc = app.open(f);
                                var layersNumber = doc.artLayers.length;
                                doc.close(SaveOptions.DONOTSAVECHANGES);
                                app.displayDialogs = DialogModes.ALL;
                                return layersNumber;
                            }
                            //--------------------------------------------------------------------------------------------------------------------------------------------------------
                            function ErrorExit(error, icon) {
                                alert(error, gScriptName + " - " + gScriptVersion, icon);
                                exit();
                            }
                            //--------------------------------------------------------------------------------------------------------------------------------------------------------
                            
                            • 11. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                              Tom Tomasko Level 1

                              Kasyan,

                               

                              Thanks! I will try this. As I have now been diverted to other things it will take a few weeks before I can get back to this.

                               

                              Tom

                              • 12. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                                Tom Tomasko Level 1

                                Kasyan,

                                 

                                Thank you very much for this script! I adapted it so that it gives me the info on the number of layers for every image in the file, not just the one selected.

                                 

                                I'm not sure if you are aware--I wasn't until I had Photoshop closed--that the script opens up Photoshop. Then there is a lot of flashing in the background as the script opens each image, counts the layers and then closes the image.

                                 

                                Most importantly, I do not have a clear concept on how the script works. I tried to find some documentation on BridgeTalk() but cannot find any. Can you point me in the right direction?

                                 

                                Again, thanks!

                                 

                                Tom

                                • 13. Re: [CS3 JS] Getting information about Photoshop images in InDesign
                                  John Hawkinson Level 5

                                  BridgeTalk is documented in the JavaScript Tools Guide. You can find it in the ESTK's Help menu.