4 Replies Latest reply on Sep 30, 2016 10:12 AM by DevilsChariot

    Need help editing strings from document info

    DevilsChariot Level 1

      I am trying to create a string based on info from the active doument that is then saved as tab delimited text for use in spreadsheets.

       

      I am new to scripting but I have been able to get the info from Photoshop but now I need to process some of it.

       

      Here is the info I want in the order I want: filename, file size ( either working size or actual), pixel width, pixel height, inches width, inches height, image resolution, color mode.

       

      example: CoolPicture_LYRD.psd     5.3 mb     3810    3810    5     5     762     RGB

       

      Here is the info I have gotten back:

       

      CoolPicture_LYRD.psd3810 px3810 px5 px5 px762 DPIDocumentMode.RGB

       

      I need to make these changes:

      1. add the document size
      2. trim the px form the strings OR convert the "px" in the inches string to "in"
      3. short numbers to 6 characters ( for a number like 5.33333333333333 px)
      4. trim theDocumentMode. from the color mode info

       

      I have tried using substrings which gets an error about functions

      I have tried toString( ) and setting the range but with no luck.

       

      any help would be appreciated. Thanks for looking.

       

      Here is my script so far

       

      //get the info from document that I need for my excel sheet
      var imageHeightPixels = activeDocument.height
      var imageWidthPixels = activeDocument.width
      var fileResolution = activeDocument.resolution
      var imageMode = activeDocument.mode
      // get size in inches from pixel based measurement
      var imageHeightInches = imageHeightPixels / fileResolution
      var imageWidthInches = imageWidthPixels  / fileResolution
      
      
      var imageName = activeDocument.name.toString()
      var imageDimensions = new File ("~/desktop/File Info Report.txt")  
      
      
      app.preferences.rulerUnits = Units.PIXELS
      
      imageDimensions.open('a');  
      imageDimensions.write(imageName + "\t" + imageHeightPixels + "\t" + imageWidthPixels + "\t" + imageWidthInches + "\t" + imageHeightInches + "\t" + fileResolution + " DPI" + "\t" + imageMode + "\n");  
      activeDocument.close(SaveOptions.DONOTSAVECHANGES)  
        
      imageDimensions.close(); 
      
        • 1. Re: Need help editing strings from document info
          Tomas Sinkunas Level 3

          There you go buddy:

           

          var doc = app.activeDocument;
          var currentUnits = app.preferences.rulerUnits;
          
          app.preferences.rulerUnits = Units.PIXELS;
          
          var fileName = doc.name;
          var fileSize = (File(doc.fullName).length / (1024 * 1024)).toFixed(2);
          var fileWidthPx   = doc.width.value;
          var fileHeightPx = doc.height.value;
          var fileResolution = doc.resolution;
          var fileWidthInch = (fileWidthPx  / fileResolution).toFixed(6);
          var fileHeigthInch = (fileHeightPx / fileResolution).toFixed(6);
          var colorMode = doc.mode.toString().split(".")[1];
          
          app.preferences.rulerUnits = currentUnits;
          
          var outputString = fileName + "\t" + 
            fileSize + " mb\t" + 
            fileWidthPx + "\t" + 
            fileHeightPx + "\t" + 
            fileWidthInch + "\t" + 
            fileHeigthInch + "\t" + 
            fileResolution + " DPI\t" + 
            colorMode + "\n";  
          
          var logFile = new File ("~/desktop/File Info Report.txt");
            logFile.open('a');    
            logFile.write(outputString);
            logFile.close();
          
          doc.close(SaveOptions.DONOTSAVECHANGES);
          
          • 2. Re: Need help editing strings from document info
            DevilsChariot Level 1

            I was hoping for advice but this will work! I am going to figure out what you did. Then  I will try to make it change the formula for determining the file size based on file name substring. I appreciate your help immensely!

            • 3. Re: Need help editing strings from document info
              natrev Level 3

              Hi DevilsChariot,

               

              Use this formula for file size

               

              var fileSize = (File(doc.fullName).length / (1000 * 1000)).toFixed(1);
              

               

              -yajiv

              • 4. Re: Need help editing strings from document info
                DevilsChariot Level 1

                Both equations work.

                 

                Yajiv's equation will give the file size as calculated on a mac, and i believe on linux based systems.

                 

                Tomas's equation gives Windows interpretation of MB and GB.

                 

                I made a variable each so thanks to both of you.