Macro Driven Undo Conundrum Solved

Get help using and writing Nisus Writer Pro macros.
Post Reply
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Macro Driven Undo Conundrum Solved

Post by loulesko »

I have a macro that converts the document to (simple) markdown for blog posting. Ideally I want to convert the text, put it on the clipboard, then revert the document to its original state. But calling the Undo menu never worked. I thought it was because the Undo menu would rename to Undo Macro "Convert to Markdown." So I tried to put the full title in the macro's menu command to no avail.

I came upon a solution.

Use the sleep command for 1 second. It works fabulously and the tiny delay is hardly noticeable.

Code: Select all

#Place the converted text on the clipboard
Write Clipboard $doc.text
Sleep 1
$menu = Menu.menuAtPath(":Edit:Undo")
$menu.activate
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Macro Driven Undo Conundrum Solved

Post by adryan »

G'day, loulesko et al

I'm not sure I would rely on an Undo command (in any application) to revert a file to its previous condition after a major operation. I think it's safer to work with a duplicate of the file.

In relation to your use of the "Sleep" command, judicious use of the equivalent "delay" command in AppleScript will often resolve problems in the latter environment. In fact, my impression is that this expedient needs to be resorted to more frequently in recent times. My naïve understanding of this is that the operating system is less capable of multitasking than it would like to think it is, with the result that it trips over itself in its headlong rush to get to its destination. Perhaps dropping the baton in a relay race might be a better analogy.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Macro Driven Undo Conundrum Solved

Post by loulesko »

Hi Adrian

Thanks for your input. My needs for this macro were dead simple. A quick copy so I can paste in the blog, and then return to its original state for maybe another edit. Anything more complicated, I would definitely work on a duplicate copy.

Wonderful to hear from you.

Lou
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Macro Driven Undo Conundrum Solved

Post by adryan »

G'day, Lou et al

As is often the case, my comments are often directed at the assembled throng, rather than to the original poster.

Just amplifying my previous comment, again for the less experienced: It’s a good idea to incorporate some sort of debugging device in your code at strategic points, so you can monitor whether something you want to happen does in fact happen. If it doesn’t, and your programming logic is flawless (and whose isn’t?), insertion of a pause can often rectify things. (The fact that in my experience a pause of as long as 5 seconds may be necessary lends weight to the hypothesis that the operating system is not what it used to be.) Regard the pause as giving the operating system time to reflect on its wayward behavior and chart a more prudent course towards enlightenment.

This is perhaps another area where Lou's Clipboard Macros could be pressed into service. Of course, any such debugging code can be commented out or deleted entirely once it’s no longer needed.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Macro Driven Undo Conundrum Solved

Post by martin »

Undo in macros should work to undo the last action inside the macro. For example consider this macro:

Code: Select all

Display as Small Caps
Bold
Undo
Italic
After running the macro, your selected text should appear in italic and small caps, but not bold.

But generally I agree with Adrian's suggestion: if your macro is converting a document it's probably better to work on a throwaway copy instead of the original. Here's the most efficient template for that:

Code: Select all

$doc = Document.active
$doc = $doc.copy
$doc.markAsDiscardable # disable undo, autosave, etc
# process the document/text however desired

$doc.close
User avatar
loulesko
Posts: 124
Joined: 2008-01-20 11:08:35
Location: California
Contact:

Re: Macro Driven Undo Conundrum Solved

Post by loulesko »

Adrian and Martin

Thank you for the advice. I have indeed adopted your approach.

Best
Lou
Post Reply