Skip to main content
Tomas Sinkunas
Legend
August 1, 2015
Question

Undo group of actions

  • August 1, 2015
  • 1 reply
  • 1581 views

Hi guys.

There's a fictitious situation where I have UI with 2 buttons:

Button A creates a new layer and set's it position to XY, Button B Scales that layer by 10%


buttonA.onClick = function() {  

    createLayer();

}

buttonB.onClick = function() {  

    scaleLayer();

}

function createLayer(){

var myLayer = myComp.layers.addSolid([0, 0,0], "Layer", 100, 100, 1);

myLayer.property("ADBE Transform Group").property("ADBE Position").setValue([X, Y])

}

function scaleLayer(){

myLayer.property("ADBE Transform Group").property("ADBE Scale").setValue([WHATEVER])

}

Lets say user click ButtonA, Button B, Button B, Button B. While undoing, each step will undo every step (Button B, Button B, Button B, Button A). However, I was wondering if it's possible to "collet actions" and do a "bundle" undo at once, so it undos all button actions?

Cheers.

This topic has been closed for replies.

1 reply

Inspiring
August 6, 2015

You can use this:

app.beginUndoGroup('someName');

app.endUndoGroup();

app.executeCommand(16);

app.beginUndoGroup:

Marks the beginning of an undo group, which allows a script to logically group all of its actions as a single
undoable action (for use with the Edit > Undo/Redo menu items). Use the endUndoGroup() method to mark
the end of the group.

beginUndoGroup() and endUndoGroup() pairs can be nested. Groups within groups become part of the
larger group, and will undo correctly. In this case, the names of inner groups are ignored.

undoString - String, The text that will appear for the Undo command in the Edit menu (that is, “Undo <undoString>”)


app.endUndoGroup:

Marks the end of an undo group begun with the app.beginUndoGroup() method. You can use this method to
place an end to an undo group in the middle of a script, should you wish to use more than one undo group for
a single script.

If you are using only a single undo group for a given script, you do not need to use this method; in its absence
at the end of a script, the system will close the undo group automatically.

Calling this method without having set a beginUndoGroup() method yields an error.

Mathias Moehl
Community Expert
Community Expert
August 7, 2015

What does the

app.executeCommand(16);

do in your solution?

I think the problem that Tomas has is that when the individual actions within the undo group are not all performed immediately (because they are connected to a button), the big question is when and how to close the undo group.

The only thing that comes to my mind is that you need to implement your own undo logic manually and store the actions that already have been performed. I.e.

1) user clicks A and the script does

beginUndoGroup

doA

endUndoGroup

2) say next the user clicks B script performs

undoA

beginUndoGroup

doA

doB

endUndoGroup

This way your last undo group always contains all the actions. But still, you have a lot of strange undo steps before that last one. And still it is not so clear what happens when the user does other modifications to the project in between the button clicks.

I think this last point is the major problem: If you link several actions to buttons and want to undo them all at once, what should happen if the user does something else in between (like clicking undo or deleting the layer you created).

Mathias Möhl - Developer of tools like BeatEdit and Automation Blocks for Premiere Pro and After Effects
Inspiring
August 7, 2015

Sorry, forgot to mention:

app.executeCommand(16); execute the 'Undo' command.


You can call the 'beginUndoGroup' when your code is launch, and when the user click undo, you will call the 'endUndoGroup' and then execute the 'Undo' command