Skip to main content
Zesty_wanderlust15A7
Known Participant
May 25, 2021
Answered

Script to delete the last (recent) Layer Comps? (specify amount to a func)

  • May 25, 2021
  • 8 replies
  • 8166 views

I sometimes like to have an action lay down some variations + Layer Comps.

 

I flip through the Comps and when I've made my choice, I want to delete those temporary, say, last 2 to 5 Comps.

 

Is this possible with a script where I specify the amount of Comps to delete from the bottom?

 

After I decided on my favorite, I don't need any of the Comps anymore, and I can handle the rest with the action.  Would just like a few scripts that "Delete 2 Last Comps", 3, 4, 5.

 

As they always seem to be added on the bottom, I'm hoping it's not elusive to do this...

Until now, I've always deleted the whole lot – but that's not professional... 😉

Correct answer Kukurykus

 

while((lCs = (aD = activeDocument).layerComps).length &&
(lC = lCs[lCs.length - 1]).name.split(/^TMP_/)[1]) lC.remove()

 

8 replies

New Participant
January 22, 2023

Install this program

Brainiac
May 26, 2021

It is probably easier to create temporary LayerComps with a fixed or standard name, for example, TMP or TMP1, TMP2, etc., perhaps using a script (or action), and then delete these temporary LayerComps based on the name.

Zesty_wanderlust15A7
Known Participant
May 26, 2021

I have this idea often but never tried it.

I would wanna keep meaningful names though, so I'm usually wondering if I can delete layers based on a prepended code.

 

For example, ...

tmp_ Meaningful description 1

tmp_ Meaningful description 2

tmp_ Meaningful description 3

 

Then delete all Comps starting with tmp_

Zesty_wanderlust15A7
Known Participant
May 26, 2021

I then wouldn't need the DUMMY Comp and it would be a touch less hardcoded (just the 'tmp_')

Brainiac
May 25, 2021

app.activeDocument.layerComps.removeAll();

 

Do you need this?

Zesty_wanderlust15A7
Known Participant
May 25, 2021

I probably got that out of a PDF... 😉

That's what I've been using.

But it doesn't respect what was already there. That's what I now wanna improve on.

 

Zesty_wanderlust15A7
Known Participant
May 25, 2021

Visual example ...

An action makes 3 layers: R, G, B

I use the Layer Comps the action also made to flip through them and see what I prefer.

I decide to keep Green.  My action removes Red and Blue layer.

I now want the R, G, B Comps gone (3 last ones). They were just for flipping through the variations.

Zesty_wanderlust15A7
Known Participant
May 25, 2021

In this simple example, I can let the action do the cleanup of the Comps too. But it's messier and more work than calling a script to "Delete last 3 Comps."  It feels safer to me too (while it may not be...).

Brainiac
May 25, 2021
If you had read Photoshop JavaScript Ref.pdf at least once, there would have been no questions.
 
 
 
Zesty_wanderlust15A7
Known Participant
May 25, 2021

That's possible, but then forums should not exist maybe.

That is some pretty "boring stuff" and I always end up having to search the net to see an example I understand..

I do always consult the two 2 pdfs but I'm not familiar with methods by just seeing keywords...

The pdfs hardly have an example in them.

I just need small bits usually, but less obvious ones on many occasions.

More to follow... 😉

I am someone that hates asking and have usually spent half the day (or even years) on it already... 😉

I'm now just asking a bunch in a row, so I am done.

Well, I guess it never stops.... 😜

Kukurykus
Brainiac
May 25, 2021

I agree, Adobe docs is unfriendly for beginners, especially those with no coding experience.

Brainiac
May 25, 2021
Who said the last one on the list is the most recent? 🙂
Zesty_wanderlust15A7
Known Participant
May 25, 2021

I don't know if it is so on the inside...

 

I am just assuming since I'm removing them very soon after I created them... (and visually they always seem to add on to the bottom).

Kukurykus
Brainiac
May 25, 2021
while((lCs = activeDocument.layerComps).length > 2) lCs[lCs.length - 1].remove()
Zesty_wanderlust15A7
Known Participant
May 25, 2021

You replied before I asked!  😉

 

I'm waiting for r-bin's faster version now... 😉

 

Seriously... Thanks again!

 

But I can't figure out how to do it in one line, if possible?

 

Why does this give an error if I wanna remove the last 3?

while( (lCs = activeDocument.layerComps).length > 0) lCs[lCs.length - 3].remove()

 

Kukurykus
Brainiac
May 25, 2021

Is it then correct answer or not?

 

Ps. if you want to remove last 3 then change 2 to 1 in my code.

 

Btw you didn't ask for fastest solution. Additionally in this case speed does no big difference.

Zesty_wanderlust15A7
Known Participant
May 25, 2021

An action *does* record when you activate a Comp (not when you select, AFAIK), so in theory I might be able to do it in the action if I activate them first, but the amount of extra steps and the "jumping around" of Comps feels "messy," and depending on the situation might be error prone...