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

Run only selected items

Engaged ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

Hey guys!
I am working on creating a dialog box that displays several checkboxes that correspond to each open document, but I need to create a function that performs something only on the checked items. Any suggestions: Thank you

var w = new Window ("dialog"); w.orientation = "row";
var panel1 = w.add ("panel");

ImgConteudo = documents;
for(i=0;i <ImgConteudo.length;i++){
  var  checkbox=  panel1.add("checkbox",  undefined, decodeURI(ImgConteudo[i].name));  
}

var  b1= w.add("button"); b1.text="Run";  
b1.onClick = function(){runCkeckoxs()};

function runCkeckoxs() {
        for (var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].value) {
                    ImgConteudo[i];
                   alert("My Script");
              }
           }
}

w.show();
TOPICS
Actions and scripting

Views

4.3K

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

correct answers 1 Correct answer

LEGEND , May 31, 2021 May 31, 2021

In runCkexkoxs function change first and second checkbox to panel1.children

Votes

Translate

Translate
Adobe
LEGEND ,
May 31, 2021 May 31, 2021

Copy link to clipboard

Copied

In runCkexkoxs function change first and second checkbox to panel1.children

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
Engaged ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

The alert actually triggered according to the number of documents, but it did not go through all the documents marked to apply a specific task, but only in the active document.

var w = new Window ("dialog"); w.orientation = "row";
var panel1 = w.add ("panel");

ImgConteudo = documents;
for(i=0;i <ImgConteudo.length;i++){
  var  checkbox=  panel1.add("checkbox",  undefined, decodeURI(ImgConteudo[i].name));  
}

var  b1= w.add("button"); b1.text="Run";  
b1.onClick = function(){runCkeckoxs()};

function runCkeckoxs() {
        for (var i = 0; i < panel1.children.length; i++) {
            if(panel1.children[i].value) {
                    ImgConteudo[i];
                   //alert("My Script");
                   executeAction(app.charIDToTypeID('Dstt'), undefined, DialogModes.NO);
              }
           }
}

w.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
People's Champ ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

Check out this code. And don't listen to what Kukurykus says. 🙂

 

var w = new Window ("dialog"); w.orientation = "row";
var panel1 = w.add ("panel");

var ImgConteudo = documents;
var checkbox = [];
for(i=0;i <ImgConteudo.length;i++)
  checkbox[i] =  panel1.add("checkbox",  undefined, ImgConteudo[i].name);  

var  b1= w.add("button", undefined, "Run");  
b1.onClick = function(){runCkeckoxs()};

function runCkeckoxs() {
        for (var i = 0; i < checkbox.length; i++) {
            if(checkbox[i].value) {
                   app.activeDocument= ImgConteudo[i];
                   executeAction(app.charIDToTypeID('Dstt'), undefined, DialogModes.NO);
              }
           }
}

w.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
Engaged ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

😂🤣

@r-bin  Perfect! It worked really well now. Thank you for sharing your precious knowledge, a big hug to you and @Kukurykus  who had the good intentions to help me.

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
LEGEND ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

This reply isn't correct as it not fully refer to originally posted code, but to its updated version.

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
Engaged ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

@r-bin  or @Kukurykus  how to check the amount of marked items?

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
LEGEND ,
Jun 01, 2021 Jun 01, 2021

Copy link to clipboard

Copied

Put it at beginning of runCkeckoxs function, so over for loop statement:

 

chckbxs = [].slice.call(panel1.children), amnt = 0; while
(chckbxs.length) amnt += chckbxs.shift().value; alert(amnt)

 

or alternatively use this version the same way if you like it more:

 

for(vls = i = 0; i < (chld = panel1.children).length;)
	vls += chld[i++].value; alert(vls)

 

however you don't again specify everything to be sure where you want it to be checked.

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

To Kukurykus

Trying to understand what is the point of using such a complex structure?

 

chckbxs = [].slice.call(panel1.children)

 

To Shulipa Bernad

For the version of the code where checkbox is an array (of checkboxes)

var cnt = 0;
for (var i = 0; i < checkbox.length; i++) if(checkbox[i].value) ++cnt;

 

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
LEGEND ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

It's why I gave an alternative to that solution if someone like you wouldn't like that, so there should not be this question 😉 You may be surprised, but some users can one day use it for something else themselves, or just find it valuable.

 

Remember people are different, and you can't expect everyone will think the way you do. Everyone as you look around has own specific desires that are normal for them, so one does sport, other scripts, someone else shares time for both things, and there are also beeings who hate both activites. I would could also ask you, why you try to help others (even doing it for yourself) if instead of that you may spend more time using your abilities to create plugins only or anything else. You probably just like it, and have fun doing it the way you prefer.

 

To the point of question. That method let you make duplicate of original array without afecting the original items. For example you can't make while loop for present items unless you delete them, right? But you don't want them to delete but remain, so to solve it you may do a clone of the object. Why not just array2 = array1, because then shifting items from array2 you remove them from array1.

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

I ask why you do it this way and not like that

 

chckbxs = panel1.children.slice();

 

And further. Imagine you have an array of 100,000 elements. Will such a structure work quickly?

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
LEGEND ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

I wouldn't use it for so many items if that had to work slowly, but where it has no performance difference there is no problem with it. So it is just an alternative. You may play with for loops or while's by 'cloning' the array.

 

Maybe I'm stupid but where to use the line you suggested? It says that part 'is not a function'.

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

quote

Maybe I'm stupid but where to use the line you suggested? It says that part 'is not a function'.


By @Kukurykus

 

No, this is probably what I am. I was sure that children is an array. : )

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

quote

I would could also ask you, why you try to help others (even doing it for yourself) if instead of that you may spend more time using your abilities to create plugins only or anything else. You probably just like it, and have fun doing it the way you prefer.


By @Kukurykus
 
I'm not a programmer at all. I myself do not know what I am doing on this forum. Probably out of boredom : )

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
LEGEND ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

I think you do good job on this forum. Your boredom is our best friend 😄

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
Engaged ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Thanks for the support @r-bin  and @Kukurykus ! It's great to be able to count on everyone in this community, especially the two of you! I have learned a lot from everyone and may our dear r @r-bin  continue with a lot of boredom so that he can always help us.

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Cool way to call methods that don't exist in the object. I'll have to take note of this.
 
However, this code causes Photoshop to crash.
[].shift.call(panel1.children)

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
LEGEND ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

What do you mean that does not exist in the object? It is there:

 

Array().slice.reflect.methods

 

I learnt it many years ago, when started scripting in Photoshop, but used only trying to do anything with HTML and Javascript. [].slice.call() is the old method from one of very first ECMA implementation, and it is very useful in ExtendScript DOM.

 

Normally you can't perform below code because activeDocument.layers are dynamic, so they change their indexes when you remove for example invisible ones:

 

lngth = (lrs = activeDocument.layers).length
for(i = 0; lngth;) !(lyr = lrs[i++]).visible && lyr.remove()

 

The above code of course will fail unless you make backward loop, so:

 

lngth = (lrs = activeDocument.layers).length
for(i = lngth - 1; i >= 0;) !(lyr = lrs[i--]).visible && lyr.remove()

 

Or you make like follows (by cloning the original array):

 

cln = [].slice.call(activeDocument.layers); while(cln.length)
	!(shft = cln.shift()).visible && shft.remove()

 

Then you can go through layer(Set)s forward without an error 😄

In other words saying, the second loop controls the original one.

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

No, I'm not talking about that.

As I understand it, we call the method of the class we need, but we slip it absolutely anything as an object (this).

Not sure if this can always work

try {

var x = 3;

alert((1).toFixed.call(x))

var x = new String("12345");

alert((1).toFixed.call(x))


} catch(e) { alert(e.line +"\n"+ 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
LEGEND ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

I have no clue what you want to achieve. Maybe show expected result or use +new String?

Anyway I just found you can make arrays from objects without making a loop over them:

 

obj = {1: 'one', 2: 'two', 3: 'three', 4: 'four', 5: 'five'}
alert(arr = Array().slice.call(obj, 1, -1)), alert(arr.length)

 

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Maybe it's clearer what I mean

try {

function AA()
    {
    this.x = 0;
    }

function BB()
    {
    this.y = 0;
    }

AA.prototype.foo = function()
    {
    ++this.x;
    alert(this.x);
    }

BB.prototype.foo = function()
    {
    ++this.y;
    alert(this.y);
    }


var a = new AA();
var b = new BB();

a.foo();
b.foo();

BB().foo.call(a)


} catch(e) { alert(e.line +"\n"+ e); }

 

It works for you because Array and Collection are very similar. There is access by index, there is a property length. For a slice from an Array, it is important that only this is, moreover, this is enough. And it will not distinguish Collection from Array. If there were any non-existent properties or methods in the Collection in the implementation of the slice method, then there would probably be an error as in the example.

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
LEGEND ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Loll, I have no idea what it's all about but I did it 😄

I used return in prototypes, and now there is no error.

The a gives undefined, but when calling b, you have 2:

 

try {
	function AA() {
		return this.x = 0
	}

	function BB() {
		return this.y = 0
	}

	AA.prototype.foo = function() {
		++this.x
		alert(this.x)
	}

	BB.prototype.foo = function() {
		++this.y
		alert(this.y)
	}

	var a = new AA()
	var b = new BB()

	a.foo()
	b.foo()

	BB().foo.call(b)
}
catch(err) {alert(err.line + '\n' + err)}

 

 

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 ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Why did you write return. It is not needed at all.
You changed
BB().foo.call(b)
What for? In this case, there is no error.
This is the same as
b.foo();
 
P.S. Okay, I've got things to do, so I'll go and do it...

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
LEGEND ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

LATEST

So for now you can't figure out how to call one function with other function input?

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
Engaged ,
Jun 02, 2021 Jun 02, 2021

Copy link to clipboard

Copied

Thank you for your support

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