Skip to main content
M.Hasanin
Inspiring
September 23, 2022
Answered

Limiting Undo or Redo only for Script Functions , is it Possible?

  • September 23, 2022
  • 2 replies
  • 650 views

Dear Experts,

im using undo and redo in script ui buttons for  like the following :

//Undo One:
    //app.menuActions.itemByID(265).invoke(); // UNDO One
//Redo One:
   //app.menuActions.itemByID(266).invoke(); //REDO One

but my question is can i limit the undo or redo to be only related to script functions?,  thanks in advance

This topic has been closed for replies.
Correct answer Manan Joshi

What you can do is club your actions that you want to undo into a single script action so that you can undo all the changes done by that method with a single undo. So we will be using the undo modes as mentioned by @BarlaeDC . In the sample below I call a method as a single script action. This method adds a frame and adds contents two times to this frame. So in effect it does three operations on the document so should add three actions to the undo stack. However with us clubbing it into one, you can call undo just once and all the actions will be reversed. You also name your operations into a single stack operation as "testaction"

function testMethod(arg1, arg2){
	var doc = app.documents[0]
	var tf  = doc.textFrames.add()
	tf.geometricBounds = [0, 0, 100, 200]
	tf.contents += arg1
	tf.contents += arg2

}

app.doScript("testMethod(arguments[0], arguments[1])", ScriptLanguage.JAVASCRIPT, ["arg1", "arg2"], UndoModes.ENTIRE_SCRIPT, "testaction")
//Now if you use just a single undo, all the operations by the method are undone
//app.documents[0].undo()

-Manan

2 replies

Community Expert
September 23, 2022

Hi @M.Hasanin,

What @BarlaeDC shared is the feature that you can use but I would like to understand the problem you are facing and trying to solve here. It probably may be a problem that would require some setup in the code to fix rather than just applying a undo group.

So just describe the following with examples if possible

  • What do you do currently?
  • What is good or bad about what happens with your current code
  • What you want to change in the outcome of the current situation

-Manan

-Manan
M.Hasanin
M.HasaninAuthor
Inspiring
September 23, 2022

Hi @Manan Joshi , thanks for asking, actually i develop little utility to insert glyph like this :

So i was wondering if i click (Undo) it will only (Undoing) what script is making and not going too far to undo other than script, and thank you a lot

Mohammad Hasanin
Manan JoshiCommunity ExpertCorrect answer
Community Expert
September 23, 2022

What you can do is club your actions that you want to undo into a single script action so that you can undo all the changes done by that method with a single undo. So we will be using the undo modes as mentioned by @BarlaeDC . In the sample below I call a method as a single script action. This method adds a frame and adds contents two times to this frame. So in effect it does three operations on the document so should add three actions to the undo stack. However with us clubbing it into one, you can call undo just once and all the actions will be reversed. You also name your operations into a single stack operation as "testaction"

function testMethod(arg1, arg2){
	var doc = app.documents[0]
	var tf  = doc.textFrames.add()
	tf.geometricBounds = [0, 0, 100, 200]
	tf.contents += arg1
	tf.contents += arg2

}

app.doScript("testMethod(arguments[0], arguments[1])", ScriptLanguage.JAVASCRIPT, ["arg1", "arg2"], UndoModes.ENTIRE_SCRIPT, "testaction")
//Now if you use just a single undo, all the operations by the method are undone
//app.documents[0].undo()

-Manan

-Manan
BarlaeDC
Community Expert
Community Expert
September 23, 2022
M.Hasanin
M.HasaninAuthor
Inspiring
September 23, 2022

Thanks you @BarlaeDC  but i mean undo inside the script during executing,not after executing

Mohammad Hasanin