Skip to main content
dublove
Legend
October 28, 2025
Answered

Is there a way to instantly revert any script to its original state?

  • October 28, 2025
  • 2 replies
  • 162 views

Some scripts can be undone with a single click before execution.
But for others, you have to roll back step by step.
Some scripts perform multiple operations, making it cumbersome to revert to the state before execution.

Is there a way to undo these multi-step scripts with one click, returning them to their origina

Correct answer m1b

Hi @dublove, yes in most cases it is possible. Just make sure you call your script using app.doScript and pass it UndoModes.ENTIRE_SCRIPT.

 

For example, this is my code on a question you posted previously:

function main() {

    var doc = app.activeDocument;
    var items = doc.selection;

    // must update `items` with new item references
    items = setItemStackingOrder(doc, items, sortByLeftTop);

    // just for testing final order, print the names
    for (var i = 0; i < items.length; i++) {
        $.writeln(i + ': item ' + items[i].name);
    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Stacking Order');

 

In fact, you you will see this on every script I have ever posted for you lol 🙂

- Mark

 

EDIT (for extra info): What we are doing is calling the `main` function using the app.doScript call. So any code you put in the `main` function will execute, even if it just calls another function. You don't have to use `main`—name it whatever you want. You can still use other functions as normal, so long as they are in scope (visible to the `main` function, for example).

2 replies

dublove
dubloveAuthor
Legend
March 22, 2026

@m1b 

I tried wrapping all my code inside a `main(){}`, but that’s prone to errors.
So I decided to wrap the subfunctions instead, and that works.
However,
what if my function needs to take parameters?
Wrapping the function again seems to work, but it feels a bit complicated.

 

The following approach works:

        for (var j = 0; j < sel.length; j++) {
movePBSel(sel[j], doc)
function fft() {
frameFit(sel[j]);
}
app.doScript(fft, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Stacking Order')
}

 

But I want to keep it simple and just call it directly; if I pass parameters, it throws an error.

        for (var j = 0; j < sel.length; j++) {
movePBSel(sel[j], doc)

frameFit(sel[j]);

app.doScript(frameFit(sel[j]), ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Stacking Order')
}

 

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
October 28, 2025

Hi @dublove, yes in most cases it is possible. Just make sure you call your script using app.doScript and pass it UndoModes.ENTIRE_SCRIPT.

 

For example, this is my code on a question you posted previously:

function main() {

    var doc = app.activeDocument;
    var items = doc.selection;

    // must update `items` with new item references
    items = setItemStackingOrder(doc, items, sortByLeftTop);

    // just for testing final order, print the names
    for (var i = 0; i < items.length; i++) {
        $.writeln(i + ': item ' + items[i].name);
    }

};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Set Stacking Order');

 

In fact, you you will see this on every script I have ever posted for you lol 🙂

- Mark

 

EDIT (for extra info): What we are doing is calling the `main` function using the app.doScript call. So any code you put in the `main` function will execute, even if it just calls another function. You don't have to use `main`—name it whatever you want. You can still use other functions as normal, so long as they are in scope (visible to the `main` function, for example).

dublove
dubloveAuthor
Legend
October 28, 2025

What? Oh my gosh.
I almost deleted all the app.doScript( ) files.
I've always wondered why it was such a hassle...
Turns out it has a clever use after all.