If history state name exists, delete

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Skrippy

If history state name exists, delete

Post by Skrippy »

Been trying for a while to just do this:

Check if a named history state exist.
If yes, delete it.

Things like these are not working...

Code: Select allIf ( app.activeDocument.historyStates.getByName('history state name') ) {
app.activeDocument.historyStates.getByName('history state name').remove();
}

Thanks for any help

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

pfaffenbichler

If history state name exists, delete

Post by pfaffenbichler »

According to ESTK’s Object Model Viewer HistoryState has no Method »remove« – what made you think it does?

You could try recording the Action Manager code with ScriptingListener.plugin to see if that can be adapted to your needs.
Skrippy

If history state name exists, delete

Post by Skrippy »

Just trying to find a solution with sweat and instinct. Usally get there or halfway
Skrippy

If history state name exists, delete

Post by Skrippy »

When I right click an unselected history state and select Delete, it gets me to something like this:

Code: Select allfunction deleteHistoryState( HistoryStateName ) {
var idslct = charIDToTypeID( "slct" );
    var desc153 = new ActionDescriptor();
    var idnull = charIDToTypeID( "null" );
        var ref123 = new ActionReference();
        var idSnpS = charIDToTypeID( "SnpS" );
        ref123.putName( idSnpS, HistoryStateName );
    desc153.putReference( idnull, ref123 );
executeAction( idslct, desc153, DialogModes.NO );
}
However, when called it just selects the state w/o deleting it...
Even if it should work, it doesn't respect the current active state.

All seems to lead to a wrong path...

Easier for my application would be if I could make a snapshot named "snapshot" that doesn't ask to "overwrite?" a previous "snapshot" if one is there. It should overwrite w/o asking.
Is that easy?
Mike Hale

If history state name exists, delete

Post by Mike Hale »

The code you posted does not delete the snapshot because it is code for selecting a snapshot. That is why it has this line
var idslct = charIDToTypeID( "slct" );

To delete the snapshot you need to create an executeAction using charIDToTypeID('Dlt ') which is the delete ID

Code: Select allfunction removeNamedSnapshot(name) {
try{
    var desc = new ActionDescriptor();
        var ref = new ActionReference();
        ref.putName( charIDToTypeID('SnpS'), name );
    desc.putReference( charIDToTypeID('null'), ref );
    executeAction( charIDToTypeID('Dlt '), desc, DialogModes.NO );
}catch(e){};
};
Skrippy

If history state name exists, delete

Post by Skrippy »

Thank you Mike!

However, I believe there may be an easier way to get what I want.

Can I either:

- just rename a named layer? (if layer exists, rename) Seems cleanest solution.
or
- overwrite a named layer w/o "overwrite?" dialog? (if the previous is not possible)

I will try your solution, but it will probably give me problems reselecting the previous state.

update: I was wrong. It seems to work perfectly as is and leaves my current state untouched! Thanks again!