4 Replies Latest reply on Apr 29, 2016 3:21 AM by Mattmcquiff

    Script help Works as javascript but not photoshop

    Mattmcquiff Level 1

      I'm trying to get these 2 scripts to work together.

       

      this runs in Photoshop fine.

      #target photoshop
      if (app.documents.length > 0) {
          var myDocument = activeDocument;
          var fileNameNoExtension = myDocument.name;
          
        fileNameNoExtension = fileNameNoExtension.split( "." );
          
        if ( fileNameNoExtension.length > 1 ) {
             fileNameNoExtension.length--;
          }
      
          fileNameNoExtension = fileNameNoExtension.join(".");
          
          var BrandID = fileNameNoExtension.substr(0,4);
          var ItemType = fileNameNoExtension.substr(4,2);
          alert(ItemType)
      }
      

       

      The alert is just for a test run, A file named EM10NW021231.jpg returns the "ItemType" as "NW"

       

      This part of the script runs as javascript

       

      var itemTable = [ 
      {itemType: "RI", BorderIncrease:"180"}, 
      {itemType: "NW", BorderIncrease:"115"}, 
      {itemType: "CL", BorderIncrease:"115"}, 
      {itemType: "BW", BorderIncrease:"120"}, 
      {itemType: "SC", BorderIncrease:"120"}, 
      {itemType: "ER", BorderIncrease:"150"}, 
      {itemType: "WW", BorderIncrease:"140"}, 
      {itemType: "WA", BorderIncrease:"140"}, 
      {itemType: "HR", BorderIncrease:"140"}, 
      {itemType: "JM", BorderIncrease:"100"}, 
      {itemType: "NA", BorderIncrease:"100"} 
      ];
      
      var BorderIncrease = itemTable.filter(function ( obj ) { 
          return obj.itemType === "BW"; //BW is to be replaced with ItemType
      })[0];
      
      alert(BorderIncrease.BorderIncrease);
      

       

      How can I link these together where line 16 is to replace the "BW" with the ItemType from the first script?

      Many Thanks

      Matt

        • 1. Re: Script help Works as javascript but not photoshop
          Chuck Uebele Adobe Community Professional & MVP

          Have you tried the $.evalFile() command? Is there a reason you don't want to combine the scripts into one, and make life easier?

          • 2. Re: Script help Works as javascript but not photoshop
            Mattmcquiff Level 1

            Hi I am trying to combine the 2 scripts. but I get errors  with this line

             

            1. var BorderIncrease = itemTable.filter(function ( obj ) {  
            2.     return obj.itemType === "BW"; //BW is to be replaced with ItemType

             

            I am very inexperienced with script, but what does $.evalFile() command mean/do?

            • 3. Re: Script help Works as javascript but not photoshop
              SuperMerlin Level 5

              Please try this.

              #target photoshop  
              if (app.documents.length > 0) {  
                  var myDocument = activeDocument;  
                  var fileNameNoExtension = myDocument.name;  
                    
                fileNameNoExtension = fileNameNoExtension.split( "." );  
                    
                if ( fileNameNoExtension.length > 1 ) {  
                     fileNameNoExtension.length--;  
                  }  
                
                  fileNameNoExtension = fileNameNoExtension.join(".");  
                    
                  var BrandID = fileNameNoExtension.substr(0,4);  
                  var ItemType = fileNameNoExtension.substr(4,2);  
                  alert(ItemType)  
              
              var itemTable = [   
              {itemType: "RI", BorderIncrease:"180"},   
              {itemType: "NW", BorderIncrease:"115"},   
              {itemType: "CL", BorderIncrease:"115"},   
              {itemType: "BW", BorderIncrease:"120"},   
              {itemType: "SC", BorderIncrease:"120"},   
              {itemType: "ER", BorderIncrease:"150"},   
              {itemType: "WW", BorderIncrease:"140"},   
              {itemType: "WA", BorderIncrease:"140"},   
              {itemType: "HR", BorderIncrease:"140"},   
              {itemType: "JM", BorderIncrease:"100"},   
              {itemType: "NA", BorderIncrease:"100"}   
              ];  
              
              var BI = BorderIncrease(itemTable, ItemType );
              alert(BI);
              
              function BorderIncrease(Arr, Item){
              for(var z in Arr){
              if(Arr[z].itemType === Item){
                 return Arr[z].BorderIncrease;
                  }
               }
              return 0;
              }; 
              };