Skip to main content
Cyril500
Known Participant
October 2, 2017
Answered

I need to delete all snapshots and layer comps by a script

  • October 2, 2017
  • 1 reply
  • 1350 views

I need to delete all Snapshots and Layer Comps in current .psd by a script.

Photoshop CC 2017 and CS6.

Would be grateful for any idea!

This topic has been closed for replies.
Correct answer c.pfaffenbichler

// 2017, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

try {

var myDocument = app.activeDocument;

myDocument.layerComps.removeAll();

var theStates = myDocument.historyStates;

for (m = theStates.length - 1; m >=0; m--) {

if (theStates.snapshot == true) {

  myDocument.activeHistoryState = theStates;

  delHist()

  }

};

} catch (e) {}

};

function delHist() {

var desc20 = new ActionDescriptor();

var ref23 = new ActionReference();

ref23.putProperty( charIDToTypeID('HstS'), charIDToTypeID('CrnH') );

desc20.putReference( charIDToTypeID('null'), ref23 );

executeAction( charIDToTypeID('Dlt '), desc20, DialogModes.NO );

};

1 reply

c.pfaffenbichler
Community Expert
c.pfaffenbichlerCommunity ExpertCorrect answer
Community Expert
October 4, 2017

// 2017, use it at your own risk;

#target photoshop

if (app.documents.length > 0) {

try {

var myDocument = app.activeDocument;

myDocument.layerComps.removeAll();

var theStates = myDocument.historyStates;

for (m = theStates.length - 1; m >=0; m--) {

if (theStates.snapshot == true) {

  myDocument.activeHistoryState = theStates;

  delHist()

  }

};

} catch (e) {}

};

function delHist() {

var desc20 = new ActionDescriptor();

var ref23 = new ActionReference();

ref23.putProperty( charIDToTypeID('HstS'), charIDToTypeID('CrnH') );

desc20.putReference( charIDToTypeID('null'), ref23 );

executeAction( charIDToTypeID('Dlt '), desc20, DialogModes.NO );

};

Cyril500
Cyril500Author
Known Participant
October 4, 2017

Thank you so much! Maybe I have used wrong terms ...

It works great but it deletes all history stages not only snapshots.

I need to keep the first history stage which, as I understand, is not a snapshot I made.

Please see the pic.

c.pfaffenbichler
Community Expert
Community Expert
October 4, 2017
It works great but it deletes all history stages not only snapshots.

You seem to be mistaken.

The non-snapshots-History States do not seem to be deleted at all, to select the last one you can include the line

myDocument.activeHistoryState = myDocument.historyStates[myDocument.historyStates.length-1];

I need to keep the first history stage which, as I understand, is not a snapshot I made.

It is a snapshot Photoshop created in accordance with the settings you chose or never changed (History Options > Automatically Create First Snapshot).

If you want to exclude that you can change the for-clause accordingly.