Highlighted

Trying to delete history state with a dialog button

Explorer ,
Oct 25, 2020

Copy link to clipboard

Copied

Hi,

according to the script at the link below:

https://community.adobe.com/t5/photoshop/slider-help-to-adjust-scale-and-rotation/td-p/10581897?page...

 

In addition to the script found at the link above I have added a Cancel button where it has to delete the history state upon closing the dialog:

buttonCancel = dialog.add("button"); buttonCancel.text = "Cancel";
buttonCancel.preferredSize.width = 80;

buttonCancel.onClick = function(){
//Select current history state
ref1 = new ActionReference();
desc1 = new ActionDescriptor();
ref1.putProperty(cTID('HstS'), cTID('CrnH'));
desc1.putReference(cTID('null'), ref1);
executeAction(cTID('slct'), desc1, DialogModes.NO);

//Delete selected history state
ref2 = new ActionReference();
desc2 = new ActionDescriptor();
ref2.putProperty(cTID('HstS'), cTID('CrnH'));
desc2.putReference(cTID('null'), ref2);
executeAction(cTID('Dlt '), desc2, DialogModes.NO);

dialog.close();
}

 

The code simply does nothing except closing the dialog box.

 

In addition, I would like to know how to create a condition for what histrory state to be deleted upon closing the dialog box.

 

Thanks,

Damian

Adobe Community Professional
Correct answer by r-bin | Adobe Community Professional
Yes, I realized that I messed up the functionality of your script.
Sorry.

What do you want to happen on the OK and Cancel buttons?

I noticed that the scale() and rotate() functions are exactly the same, but you create a step for them in history with different names.
Use the same name for the history step and then it will almost solve your problem with your original code. Don't use my code.
 
TOPICS
Actions and scripting, How to, Make It, Windows

Views

642

Likes

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

Trying to delete history state with a dialog button

Explorer ,
Oct 25, 2020

Copy link to clipboard

Copied

Hi,

according to the script at the link below:

https://community.adobe.com/t5/photoshop/slider-help-to-adjust-scale-and-rotation/td-p/10581897?page...

 

In addition to the script found at the link above I have added a Cancel button where it has to delete the history state upon closing the dialog:

buttonCancel = dialog.add("button"); buttonCancel.text = "Cancel";
buttonCancel.preferredSize.width = 80;

buttonCancel.onClick = function(){
//Select current history state
ref1 = new ActionReference();
desc1 = new ActionDescriptor();
ref1.putProperty(cTID('HstS'), cTID('CrnH'));
desc1.putReference(cTID('null'), ref1);
executeAction(cTID('slct'), desc1, DialogModes.NO);

//Delete selected history state
ref2 = new ActionReference();
desc2 = new ActionDescriptor();
ref2.putProperty(cTID('HstS'), cTID('CrnH'));
desc2.putReference(cTID('null'), ref2);
executeAction(cTID('Dlt '), desc2, DialogModes.NO);

dialog.close();
}

 

The code simply does nothing except closing the dialog box.

 

In addition, I would like to know how to create a condition for what histrory state to be deleted upon closing the dialog box.

 

Thanks,

Damian

Adobe Community Professional
Correct answer by r-bin | Adobe Community Professional
Yes, I realized that I messed up the functionality of your script.
Sorry.

What do you want to happen on the OK and Cancel buttons?

I noticed that the scale() and rotate() functions are exactly the same, but you create a step for them in history with different names.
Use the same name for the history step and then it will almost solve your problem with your original code. Don't use my code.
 
TOPICS
Actions and scripting, How to, Make It, Windows

Views

643

Likes

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
Oct 25, 2020 0
Explorer ,
Oct 25, 2020

Copy link to clipboard

Copied

I have even tried:

buttonCancel = dialog.add("button");  buttonCancel.text = "Cancel";
buttonCancel.preferredSize.width = 80;

doc = app.activeDocument;
currentStatus = doc.activeHistoryState;

buttonCancel.onClick = function(){
	currentStatus = doc.historyStates['rotate'];

	ref1 = new ActionReference();
	desc1 = new ActionDescriptor();
	ref1.putProperty(cTID('HstS'), cTID('CrnH'));
	desc1.putReference(cTID('null'), ref1);
	executeAction(cTID('Dlt '), desc1, DialogModes.NO);
	
	dialog.close();
}

to select a history state by name, delete it and then close the dialog box. No chances I am getting any result, as it does not want to listen at all.

Likes

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
Reply
Loading...
Oct 25, 2020 0
Explorer ,
Oct 25, 2020

Copy link to clipboard

Copied

I have obviously made a mistake in a part of the code, instead:

currentStatus = doc.historyStates['rotate'];

I have to replace it with:

doc.activeHistoryState = doc.historyStates['rotate'];

and it selects the first 'rotate' state, and it does not execute the deleting process.

 

What I clearly want to achieve is the cancel button to delete the last history state made by the sliders upon closing the dialog box.

Likes

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
Reply
Loading...
Oct 25, 2020 0
Explorer ,
Oct 25, 2020

Copy link to clipboard

Copied

I have found the solution to delete the history state after canceling the slider preview and here it is:

 

buttonOK.onClick = function(){
	ref1 = new ActionReference();
	desc1 = new ActionDescriptor();
	doc.activeHistoryState = doc.historyStates['rotate'];
	ref1.putProperty(charIDToTypeID('HstS'), charIDToTypeID('CrnH'));
	desc1.putReference(charIDToTypeID('null'), ref1);
	executeAction(charIDToTypeID('Dlt '), desc1, DialogModes.NO);

	app.activeDocument.suspendHistory ("rotated", "rotate()"); // it changes the history name to rotated upon OK

	dialog.close();
}

buttonCancel.onClick = function(){
	doc.activeHistoryState = doc.historyStates['rotate']; // it deletes only the history name found created while moving the sliders

	ref1 = new ActionReference();
	desc1 = new ActionDescriptor();
	ref1.putProperty(charIDToTypeID('HstS'), charIDToTypeID('CrnH'));
	desc1.putReference(charIDToTypeID('null'), ref1);
	executeAction(charIDToTypeID('Dlt '), desc1, DialogModes.NO);

	dialog.close();
}

Now, the only thing I need is to create a condition to delete either 'rotate' or 'scale' history name.

 

Likes

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
Reply
Loading...
Oct 25, 2020 0
Adobe Community Professional ,
Oct 25, 2020

Copy link to clipboard

Copied

Wrap the code in the onClick function with
try { ... } catch (e) {alert (e); }

If there is an alert, what does it say?
 

Likes

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
Reply
Loading...
Oct 25, 2020 0
Explorer ,
Oct 26, 2020

Copy link to clipboard

Copied

I've wrapped the code but getting no alert at all.

Likes

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
Reply
Loading...
Oct 26, 2020 0
Adobe Community Professional ,
Oct 26, 2020

Copy link to clipboard

Copied

So everything works. Insert some alerts before and after the "delete" command. What is happening in the history panel at this time? After all, something is deleted, and without errors.

You post only script fragments. It is not known what is going on there in your version of the code.
 
 

Likes

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
Reply
Loading...
Oct 26, 2020 0
Explorer ,
Oct 29, 2020

Copy link to clipboard

Copied

OK, here is the whole code copied from the link I provided in my first post with your solution.

 

The only thing is I have modified button1 to buttonOK and added buttonCancel variable and its function expression:

 

dialog = new Window("dialog");
dialog.text = "test"; 
group1 = dialog.add("group"); 
group1.orientation = "row"; 
group1.alignChildren = ["left","center"]; 
statictext1 = group1.add("statictext");  statictext1.text = "Scale:"; 
slider1 = group1.add("slider {minvalue: 0, maxvalue: 300, value: 100}"); 
slider1.preferredSize.width = 200; 
ts = group1.add("edittext"); ts.text = "100"; 
ts.preferredSize.width = 40; 
statictext2 = group1.add("statictext");statictext2.text = "%"; 
group2 = dialog.add("group"); 
statictext3 = group2.add("statictext"); statictext3.text = "Rotate:"; 
slider2 = group2.add("slider {minvalue: -180, maxvalue: 180, value: 0}"); 
slider2.preferredSize.width = 200; 
tr = group2.add("edittext"); tr.text = "0"; 
tr.preferredSize.width = 40; 
statictext4 = group2.add("statictext");    statictext4.text = "º"; 
buttonOK = dialog.add("button");  buttonOK.text = "OK"; // OK button
buttonOK.preferredSize.width = 80;
buttonCancel = dialog.add("button");  buttonCancel.text = "Cancel"; // Cancel button
buttonCancel.preferredSize.width = 80; 
var doc = app.activeDocument;  
var currentStatus = doc.activeHistoryState;  
slider1.onChange= function () {    
    ts.text = Math.round(slider1.value );  
    doc.activeHistoryState = currentStatus;  app.activeDocument.suspendHistory ("scale", "scale()"); 
    app.refresh();
}    
ts.onChange= function () {    
    slider1.value  = Number(ts.text);  
    doc.activeHistoryState = currentStatus;  app.activeDocument.suspendHistory ("scale", "scale()"); 
    app.refresh();
   }
slider2.onChange= function () {    
    tr.text = Math.round(slider2.value );  
    doc.activeHistoryState = currentStatus;  app.activeDocument.suspendHistory ("rotate", "rotate()"); 
    app.refresh();
   }    
tr.onChange= function () {    
    slider2.value  = Number(tr.text);  
    doc.activeHistoryState = currentStatus;  app.activeDocument.suspendHistory ("rotate", "rotate()"); 
    app.refresh();
   }
function scale(){    
    activeDocument.activeLayer.resize(slider1.value, slider1.value, AnchorPosition.MIDDLECENTER);  
    with (activeDocument.activeLayer) {rotate(slider2.value)};  
}   
function rotate(){    
    activeDocument.activeLayer.resize(slider1.value, slider1.value, AnchorPosition.MIDDLECENTER);  
    with (activeDocument.activeLayer) {rotate(slider2.value)};  
}  
buttonOK.onClick = function(){
    ref1 = new ActionReference();
    desc1 = new ActionDescriptor();
    doc.activeHistoryState = doc.historyStates['rotate'];
    ref1.putProperty(charIDToTypeID('HstS'), charIDToTypeID('CrnH'));
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('Dlt '), desc1, DialogModes.NO);
    app.activeDocument.suspendHistory ("rotated", "rotate()"); // it changes the history name to rotated upon OK
    dialog.close();
}

buttonCancel.onClick = function(){
    doc.activeHistoryState = doc.historyStates['rotate']; // it deletes only the history name found created while moving the sliders
    ref1 = new ActionReference();
    desc1 = new ActionDescriptor();
    ref1.putProperty(charIDToTypeID('HstS'), charIDToTypeID('CrnH'));
    desc1.putReference(charIDToTypeID('null'), ref1);
    executeAction(charIDToTypeID('Dlt '), desc1, DialogModes.NO);
    dialog.close();
}
dialog.show();

 

The script works perfectly but only for the rotate function. If I change the value of scale function last before hitting OK button it will overwrite the rotate history state with 'scale' name and thus the Cancel button will not delete that state because there is need of conditional statement.

 

My problem is figuring out how to add a conditional statement when hitting Cancel button to delete the last modification that is written into the history state made by the slider. The condition have to be for more than one function slider. How can that be done?

Thanks

Likes

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
Reply
Loading...
Oct 29, 2020 0
Adobe Community Professional ,
Oct 29, 2020

Copy link to clipboard

Copied

Try this version of your script. Manipulating history (deleting and other operations) is not the best option.
var undo = true;

app.activeDocument.suspendHistory ("script", "main()"); 

if (undo)
    {        
    executeAction(stringIDToTypeID("undoEvent"), undefined, DialogModes.NO);
    app.activeDocument.suspendHistory("Undo", ""); // Disable reverting via ctrl-Z
    executeAction(stringIDToTypeID("undoEvent"), undefined, DialogModes.NO);
    }        


function main()
{

dialog = new Window("dialog");
dialog.text = "test"; 
group1 = dialog.add("group"); 
group1.orientation = "row"; 
group1.alignChildren = ["left","center"]; 
statictext1 = group1.add("statictext");  statictext1.text = "Scale:"; 
slider1 = group1.add("slider {minvalue: 0, maxvalue: 300, value: 100}"); 
slider1.preferredSize.width = 200; 
ts = group1.add("edittext"); ts.text = "100"; 
ts.preferredSize.width = 40; 
statictext2 = group1.add("statictext");statictext2.text = "%"; 
group2 = dialog.add("group"); 
statictext3 = group2.add("statictext"); statictext3.text = "Rotate:"; 
slider2 = group2.add("slider {minvalue: -180, maxvalue: 180, value: 0}"); 
slider2.preferredSize.width = 200; 
tr = group2.add("edittext"); tr.text = "0"; 
tr.preferredSize.width = 40; 
statictext4 = group2.add("statictext");    statictext4.text = "º"; 
buttonOK = dialog.add("button");  buttonOK.text = "OK"; // OK button
buttonOK.preferredSize.width = 80;
buttonCancel = dialog.add("button");  buttonCancel.text = "Cancel"; // Cancel button
buttonCancel.preferredSize.width = 80; 
var doc = app.activeDocument;  
slider1.onChange= function () {    
    ts.text = Math.round(slider1.value );  
    scale(); 
    app.refresh();
}    
ts.onChange= function () {    
    slider1.value  = Number(ts.text);  
    scale(); 
    app.refresh();
   }
slider2.onChange= function () {    
    tr.text = Math.round(slider2.value );  
    rotate(); 
    app.refresh();
   }    
tr.onChange= function () {    
    slider2.value  = Number(tr.text);  
    rotate(); 
    app.refresh();
   }
function scale(){    
    activeDocument.activeLayer.resize(slider1.value, slider1.value, AnchorPosition.MIDDLECENTER);  
    with (activeDocument.activeLayer) {rotate(slider2.value)};  
}   
function rotate(){    
    activeDocument.activeLayer.resize(slider1.value, slider1.value, AnchorPosition.MIDDLECENTER);  
    with (activeDocument.activeLayer) {rotate(slider2.value)};  
}  
buttonOK.onClick = function(){
    undo = false;
    dialog.close();
}

buttonCancel.onClick = function(){
    dialog.close();
}
dialog.show();
}

Likes

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
Reply
Loading...
Oct 29, 2020 0
Explorer ,
Oct 29, 2020

Copy link to clipboard

Copied

Thanks r-bin, but the problem now is that the last value pointed with the slider in the preview state is applied to the canvas before hitting the OK button the edit becomes usless.

Likes

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
Reply
Loading...
Oct 29, 2020 0
Adobe Community Professional ,
Oct 30, 2020

Copy link to clipboard

Copied

Can you say it differently? I didn't understand anything, to be honest.
Including why "the edit becomes usless"?

Likes

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
Reply
Loading...
Oct 30, 2020 0
Explorer ,
Nov 02, 2020

Copy link to clipboard

Copied

You may try with your example by moving the 'scale' slider and see what I mean. In other words, when moving the slider to, let's say, 120% and after that returning it to 100%, it does not reset the image to its original scale, i.e. the 120% becomes its original scale. Try to scale it to 0% and than return it to 100%, it does not return it as 0% becomes its original scale.

 

Same goes with the rotation slider, rotate the image to 20deg and then return it to 0deg you will see it does not return to its original 0deg rotation.

 

I hope you can now understand the problem I've tried to  describe.

 

Thanks for your prescious time explaining.

Likes

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
Reply
Loading...
Nov 02, 2020 0
Explorer ,
Nov 02, 2020

Copy link to clipboard

Copied

In addition, what is up to the condition whether it is 'scale' or 'rotate' slider, it functions properly now.

Likes

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
Reply
Loading...
Nov 02, 2020 0
Adobe Community Professional ,
Nov 02, 2020

Copy link to clipboard

Copied

Yes, I realized that I messed up the functionality of your script.
Sorry.

What do you want to happen on the OK and Cancel buttons?

I noticed that the scale() and rotate() functions are exactly the same, but you create a step for them in history with different names.
Use the same name for the history step and then it will almost solve your problem with your original code. Don't use my code.
 

Likes

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
Reply
Loading...
Nov 02, 2020 1
Explorer ,
Nov 02, 2020

Copy link to clipboard

Copied

This is actually very good idea that I didn't thought of as it will shorten coding where otherwise conditionals would take up additional code lines. Keep it simple simple. I will take this answer as correct

Likes

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
Reply
Loading...
Nov 02, 2020 0