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

Script for selecting multiple objects

Community Beginner ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

I have this script where I recognize layer object by color. I now have multiple objects in my list listOfRest but how do I select them and get their combined left and top values?

 

 

TOPICS
Scripting

Views

826

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

Community Expert , Mar 24, 2021 Mar 24, 2021

My apologies. I had a bug in there. I wasn't comparing the values properly. 

 

The part you asked about is a ternary statement. It's just a condensed if/else clause. Here's some reading if you're interested (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).

 

topMost = listOfRest[i].top > topMost ? listOfRest[i].top : topMost;

 

Here i've broken it down into sections. Green is the variable definition. I'm setting "topMost" equal to the result of t

...

Votes

Translate

Translate
Adobe
Community Beginner ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

tfm9.png

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 Expert ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

Hi,

Please share the script instead of the screenshot of the code.

That will helps us to use the existing code.

Best regards

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 Expert ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

Hi @Matěj5CD8 

Would you like us to handwrite the code of your picture?

 

Please write your code as plain text in your posting. In the answer window there is an extra button [</>], which is available especially for code to be inserted.

 

One more thing: Your If-clause doesn't make sense:
If the color is right - then add the pageItem to the array.
Else if it is wrong - then add the pageItem to the array.

 

This is how you add all of the pageItems to the 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
Community Expert ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

the else statement pushes to a different array.

 

if the "if" condition is true, it pushes the path to an array of "listOfRed", else, push the path to an array of "listOfRest".

 

having said that, it's still impossible to help with the code at all since it only shows the loop and not any of the variable declarations/definitions or other init steps to get to this point.

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 Expert ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

Seen very well.

listOfRed vs listOfRest

😉

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 Expert ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

 

 

var topMost = listOfRest[0].top;
var leftMost = listOfRest[0].left;

for(var i=0; i < listOfRest.length; i++)
{
	listOfRest[i].selected = true;
	topMost = listOfRest[i].top > topMost ? listOfRest[i].top : topMost;
	leftMost = listOfRest[i].left < leftMost ? listOfRest[i].left : leftMost;
}

alert("Non-Red items have been selected.\nCombined top and left vaues:\nleft: " + leftMost.left + "\ntop: " + topMost.top);

 

 

 

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 ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

Thank you for your reply. I am sorry, but I cant post the code! Website wont let me! Always having trouble with some kind of post flooding error. Could you explain to me a little bit in detail the "topMost ? listOfRest[i] : topMost" bit? 

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 Expert ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

My apologies. I had a bug in there. I wasn't comparing the values properly. 

 

The part you asked about is a ternary statement. It's just a condensed if/else clause. Here's some reading if you're interested (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator).

 

topMost = listOfRest[i].top > topMost ? listOfRest[i].top : topMost;

 

Here i've broken it down into sections. Green is the variable definition. I'm setting "topMost" equal to the result of the rest of the expression.

 

Blue is the ternary condition.

    If Blue evaluates to true, Green will be set equal to the value of Red

    If Blue evaluates to false, Green will be set equal to the value of Orange/yellow

 

Here's a fixed version. I fixed the bug in the comparrison and i used regular if clauses so it's more clear.

var topMost = listOfRest[0].top;
var leftMost = listOfRest[0].left;

for(var i=0; i < listOfRest.length; i++)
{
	listOfRest[i].selected = true;
	// topMost = listOfRest[i].top > topMost ? listOfRest[i].top : topMost;
	// leftMost = listOfRest[i].left < leftMost ? listOfRest[i].left : leftMost;

	//check to see if this piece is "higher" than the topMost piece
		//ie.. is the top value of the current piece greater than the topmost
	//if it is, set the value of topMost to the top value of this item
	if(listOfRest[i].top > topMost)
	{
		topMost = listOfRest[i].top;
	}

	//check to see if this piece is "farther left" than the leftMost value
	//if it is, set the value of leftMost to the left value of this item
	if(listOfRest[i].left < leftMost)
	{
		leftMost = listOfRest[i].left;
	}
}

alert("Non-Red items have been selected.\nCombined top and left vaues:\nleft: " + leftMost + "\ntop: " + topMost);

 

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 ,
Mar 24, 2021 Mar 24, 2021

Copy link to clipboard

Copied

LATEST

Thank you, it's working. God bless you. Have a nice day

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