• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Nov 17, 2017 Nov 17, 2017

Copy link to clipboard

Copied

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();

};

TOPICS
Actions and scripting

Views

874

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
People's Champ ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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.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)} }; 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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=win.add("button", undefined,"template"+alphabet);

btn.onClick = function(){

win.close(0);

app.open(File(filePath + this.text + ".psd"));

    } 

}      

win.show();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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.onClick = function() {

        for(var a in btn){if(this.text == btn.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)} };

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

LATEST

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)} };  

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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.n = i;

    btn.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)} };          

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

"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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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

                Folder.desktop + "/templateB.psd",

                Folder.desktop + "/templateC.psd",

                Folder.desktop + "/templateD.psd"

                ];

alert(File(filePath[2]));

Windows or Mac.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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)}};

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 18, 2017 Nov 18, 2017

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines