Page 1 of 1

Undo a macro

Posted: 2009-09-01 12:02:35
by loulesko
Hi there,

I have a macro that does a lot of text changes and ends with a select all - copy - select end to copy the changes to the clipboard. I then want to undo the changes, but the follwoing commands are not working in the macro. Edit:Undo or Undo or Edit:Undo Macro- but it works fine when I choose Undo from the edit menu.

Thanks
Lou

Re: Undo a macro

Posted: 2009-09-01 13:41:17
by martin
Hi Lou- the "Undo" command inside a macro itself is treated differently than normal. NWP assumes you only want to undo the last step of the macro. For example, consider this macro:

Code: Select all

Bold
Italic
Undo
The "Undo" command only undoes the "Italic" command, and thus this macro only changes whether text is bold or not.

On the other hand, once a macro has been completed, all changes it made are grouped to be a single action. That is, choosing "Undo" from the menu will undo all changes made by the macro.

Currently there's no single command that would allow you to undo all changes made by the macro from inside the macro. There's two ways you might work around this. The first is to execute the "Undo" command in your macro as many times as you need, eg:

Code: Select all

Bold
Italic
Undo
Undo
If your macro does many things this may be error prone, but you might shorten it up like so:

Code: Select all

Bold
Italic

$undoCount = 2
While $undoCount > 0
    Undo
    $undoCount -= 1
End
That may not be terribly efficient. It might be better to work on a copy of the active document, like so:

Code: Select all

$doc = Document.active
$doc = $doc.copy
$doc.clearAndDisableUndoHistory # for efficiency, also allows closing doc at the end without any warning
# do whatever you want
$doc.close

Re: Undo a macro

Posted: 2009-09-01 16:19:51
by loulesko
That last solution creating a copy is brilliant and does exactly what I need.

Many thanks.
Lou