10 Replies Latest reply on Nov 18, 2017 7:10 PM by 健児舟54531165

    onClick = function () {a (i);}; I want to repeat processing with variables

    健児舟54531165

      Hello, I usually do animation etc in python.

      Please kindly advise. OS windows 7 software PhotoshopCC

      It is not English - speaking countries.

       

      I am using photoshop at work, but I've recently started making photoshop script, trying to make a simple script that opens a template that I always use. I can create a scriptUI that opens with an arbitrary button, but I want to organize the increasing number of buttons by iterating. However, it does not work.

       

       

      button0.onClick = function(){openfile (0);};  // ← I want to repeat this by iteration

      button1.onClick = function(){openfile (1);};

      button2.onClick = function(){openfile (2);};

      :

      :

       

       

      Below is the actual script. Buttons will appear, but nothing will happen when pressed.

       

      filePath = [
      "C:\\Users\\staff\\Desktop\\templateA.psd",
      "C:\\Users\\staff\\Desktop\\templateB.psd",
      "C:\\Users\\staff\\Desktop\\templateC.psd",
      "C:\\Users\\staff\\Desktop\\templateD.psd"]; // File path
      
      win = new Window("dialog", "title"); // Wind, button creation
      btn0 = win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateA");
      btn1 = win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateB");
      btn2 = win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateC");
      btn3 = win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateD");
      
      for (var i = 0; i <filePath.length; i ++){
          bNo = "btn" + i.toString();
          bNo.onClick = function(){opnFile(i);};
      }
      
      win.show(); // button window appearance
      
       function opnFile(btNo){
          filename = new File(filePath [btNo]);
          open(filename); // Open the file
          win.close();
      };
      

       

      It works fine if it is below

       

      filePath = [
      "C:\\Users\\staff\\Desktop\\templateA.psd",,
      "C:\\Users\\staff\\Desktop\\templateB.psd",,
      "C:\\Users\\staff\\Desktop\\templateC.psd",
      "C:\\Users\\staff\\Desktop\\templateD.psd"]; // File path
      
      win = new Window ("dialog", "title"); // Wind, button creation
      btn0 = win.add ("button", {width: 80, height: 25, x: 40, y: 25}, "templateA");
      btn1 = win.add ("button", {width: 80, height: 25, x: 40, y: 25}, "templateB");
      btn2 = win.add ("button", {width: 80, height: 25, x: 40, y: 25}, "templateC");
      btn3 = win.add ("button", {width: 80, height: 25, x: 40, y: 25}, "templateD");
      
      btn0.onClick = function(){ opnFile(0); };       //Button action definition
      btn1.onClick = function(){ opnFile(1); };
      btn2.onClick = function(){ opnFile(2); };
      btn3.onClick = function(){ opnFile(3); };
      
      win.show(); // button window appearance
      
       function opnFile(btNo){
          filename = new File(filePath [btNo]);
          open(filename); // Open the file
          win.close();
      };
      

       

        • 1. Re: onClick = function () {a (i);}; I want to repeat processing with variables
          r-bin Level 5

          Try

           

          filePath = [  
          "C:\\Users\\staff\\Desktop\\templateA.psd",  
          "C:\\Users\\staff\\Desktop\\templateB.psd",  
          "C:\\Users\\staff\\Desktop\\templateC.psd",  
          "C:\\Users\\staff\\Desktop\\templateD.psd"]; // File path  
          
          var btn = new Array();
            
          win = new Window("dialog", "title"); // Wind, button creation  
          btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateA"));  
          btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateB"));  
          btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateC"));  
          btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateD"));  
          
          for (var i = 0; i < btn.length; i ++)
              btn[i].onClick = function() { opnFile(i) };  
            
          win.show(); // button window appearance  
            
           function opnFile(btNo){ try {
              filename = new File(filePath [btNo]);  
              open(filename); // Open the file  
              win.close();  
          } catch(e){alert(e)} };  
          
          • 2. Re: onClick = function () {a (i);}; I want to repeat processing with variables
            SuperMerlin Level 5

            Yet another version...

            #target photoshop; 
            var filePath = "/C/Users/staff/Desktop/";
            var alphabet=["A","B","C","D","E","F","G","H"]; //etc
            btn=[];
            win = new Window("dialog", "title");
            for (var i = 0; i <4; i ++){  
            btn[i]=win.add("button", undefined,"template"+alphabet[i]);
            btn[i].onClick = function(){
            win.close(0);
            app.open(File(filePath + this.text + ".psd"));
                }  
            }       
            win.show();
            
            
            • 3. Re: onClick = function () {a (i);}; I want to repeat processing with variables
              健児舟54531165 Level 1

              r-bin, Mr. SuperMerlin. Thank you for your prompt reply.

              But it will not work. In r-bin's code I came out to refer to the file.

              This time we may release unresolved long code. As a legacy of my childish code (lol)

              • 4. Re: onClick = function () {a (i);}; I want to repeat processing with variables
                SuperMerlin Level 5

                This should work as well.

                filePath = [    
                "C:\\Users\\staff\\Desktop\\templateA.psd",    
                "C:\\Users\\staff\\Desktop\\templateB.psd",    
                "C:\\Users\\staff\\Desktop\\templateC.psd",    
                "C:\\Users\\staff\\Desktop\\templateD.psd"]; // File path    
                  
                var btn = new Array();  
                    
                win = new Window("dialog", "title"); // Wind, button creation    
                btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateA"));    
                btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateB"));    
                btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateC"));    
                btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateD"));    
                  
                for (var i = 0; i < btn.length; i ++)  
                    btn[i].onClick = function() {
                        for(var a in btn){if(this.text == btn[a].text) {opnFile(a); break;}}
                        };       
                win.show(); // button window appearance    
                    
                 function opnFile(btNo){ 
                     try {  
                    filename = new File(filePath [btNo]);    
                   if(fileName.exists) open(filename); // Open the file    
                    win.close();    
                } catch(e){alert(e)} }; 
                
                
                
                1 person found this helpful
                • 5. Re: onClick = function () {a (i);}; I want to repeat processing with variables
                  r-bin Level 5

                  My mistake. Did not check (
                  this works.

                   

                  filePath = [   
                  "C:\\Users\\staff\\Desktop\\templateA.psd",   
                  "C:\\Users\\staff\\Desktop\\templateB.psd",   
                  "C:\\Users\\staff\\Desktop\\templateC.psd",   
                  "C:\\Users\\staff\\Desktop\\templateD.psd"]; // File path   
                  
                  var btn = new Array(); 
                     
                  win = new Window("dialog", "title"); // Wind, button creation   
                  btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateA"));   
                  btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateB"));   
                  btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateC"));   
                  btn.push(win.add("button", {width: 80, height: 25, x: 40, y: 25}, "templateD"));   
                  
                  for (var i = 0; i < btn.length; i ++) 
                      {
                      btn[i].n = i;
                      btn[i].onClick = function() { try { opnFile(this.n); } catch(e){alert(e)} }
                      }
                     
                  win.show(); // button window appearance   
                  
                  function opnFile(btNo){ try { 
                      filename = new File(filePath [btNo]);   
                      open(filePath[btNo]); // Open the file   
                      win.close();   
                  } catch(e){alert(e)} };           
                  
                  1 person found this helpful
                  • 6. Re: onClick = function () {a (i);}; I want to repeat processing with variables
                    geppettol66959005 Level 2

                    "C: \\ Users \\ Desktop \\ \\ staff templateA.psd"

                    "C: \\ Users \\ Desktop \\ \\ staff templateB.psd"

                    "C: \\ Users \\ Desktop \\ \\ staff templateC.psd"

                    "C: \\ Users \\ Desktop \\ \\ staff templateD.psd"]; // File path

                     

                    these are paths for windows

                    how should it be for mac?

                    • 7. Re: onClick = function () {a (i);}; I want to repeat processing with variables
                      SuperMerlin Level 5

                      filePath = [ Folder.desktop + "/templateA.psd",

                                      Folder.desktop + "/templateB.psd",

                                      Folder.desktop + "/templateC.psd",

                                      Folder.desktop + "/templateD.psd"

                                      ];

                      alert(File(filePath[2]));

                       

                       

                      Windows or Mac.

                      1 person found this helpful
                      • 8. Re: onClick = function () {a (i);}; I want to repeat processing with variables
                        健児舟54531165 Level 1

                        Mr. r-bin

                        Mr. SuperMerlin

                        Both went well. Thank you!

                        I did not understand how to put variables of iteration in "function ()” function. I was glad that you taught me.

                         

                        r-binさん

                        SuperMerlinさん

                        ありがとう!

                        Thank you!

                        • 9. Re: onClick = function () {a (i);}; I want to repeat processing with variables
                          健児舟54531165 Level 1

                          I rewrote some and succeeded. Thanks

                           

                           

                          function opnFile(btNo){try{
                              filename = new File(filePath[btNo]);
                              open(filePath[btNo]); ←open(filename)
                              win.close();
                          }catch(e){alert(e)}};
                          
                          • 10. Re: onClick = function () {a (i);}; I want to repeat processing with variables
                            健児舟54531165 Level 1

                            I rewrote some and succeeded. Thanks

                             

                             function opnFile(btNo){   
                                 try {    
                                filename = new File(filePath [btNo]);      
                               if(fileName.exists) open(filename); ←if(filename.exists)
                                win.close();      
                            } catch(e){alert(e)} };