How to use the command 'Scan Document' in a longer macro

Get help using and writing Nisus Writer Pro macros.
Post Reply
Þorvarður
Posts: 410
Joined: 2012-12-19 05:02:52

How to use the command 'Scan Document' in a longer macro

Post by Þorvarður »

I use Bookends as a reference manager. I would like to be able to run a macro which looks like this:

Scan Document
Clean up
Do some other stuff
...

'Clean up' is the name of a macro that tweaks the formatting, deletes gremlins, etc.
The problem now is that the macro stops after the document has been scanned and never goes further.
I suppose this requires some simple internal tweaks in Nisus or Bookends. Could this be fixed?
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: How to use the command 'Scan Document' in a longer macro

Post by phspaelti »

The fundamental problem that you will have here is that your Mac is multi-tasking. "Scan Document" may look like a Nisus command to you, but Nisus just hands off control to Bookends, and then continues to execute the macro without waiting for Bookends to complete.

I thought Nisus macro language had some kind of "Following Macro" capability, but I can't seem to find it, so maybe i was dreaming.
Another approach might be to use Applescript to control execution, though that doesn't seem to work so well since the introduction of the sandbox.
philip
Þorvarður
Posts: 410
Joined: 2012-12-19 05:02:52

Re: How to use the command 'Scan Document' in a longer macro

Post by Þorvarður »

Hello Philip,
phspaelti wrote: 2021-02-13 11:23:09 "Scan Document" may look like a Nisus command to you, but Nisus just hands off control to Bookends
Yes, that's exactly what I though. I imagine we are watching toss juggling: two jugglers with clubs face each other, one tosses a club, the other responds and tosses back another club and … now, who would have expected that, the game is suddenly over, before it even really started!

Another approach might be to use Applescript to control execution
Yes, I also tried AppleScript with the "Do Menu Macro with macro"-command.

tell application "Nisus Writer Pro"
Activate
get text of document 1
Do Menu Macro with macro "Scan Doc"
Do Menu Macro with macro "Clean up"
end tell

The second macro came half way through and cleaned up the Nisus document, but Bookends froze and had to be restarted and the computer became unstable.

I just don't know yet who of the two jugglers, Bookends or Nisus, is in charge here and can fix this. I guess Martin can tell us. If it's Bookends, then I'll contact Sonny Software.
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: How to use the command 'Scan Document' in a longer macro

Post by adryan »

G'day, Þorvarður, Philip et al

It's sometimes helpful in an AppleScript script to interpose a "delay" command (with an appropriate duration) to ensure that execution of code pertaining to one application is completed before code pertaining to another application is dealt with.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
Þorvarður
Posts: 410
Joined: 2012-12-19 05:02:52

Re: How to use the command 'Scan Document' in a longer macro

Post by Þorvarður »

adryan wrote: 2021-02-13 23:12:47 It's sometimes helpful in an AppleScript script to interpose a "delay" command
Thank you Adrian for reminding me of this. After adding a delay command now, Bookends does not crash, but the second macro is not carried out, just like before.

Let's wait and see what Martin has to say about this. Not being able to run a macro in combination with 'Scan Document' exists as long as I can remember. It's not version or system related.
User avatar
martin
Official Nisus Person
Posts: 5227
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: How to use the command 'Scan Document' in a longer macro

Post by martin »

This scenario is definitely a juggling act, and one that unfortunately won't be easily solved. Adding a delay to the macro may happen to work, but it's brittle. A delay assumes the time required to scan and process a document will always be uniform– that the time will always be under some threshold. That can't ever be guaranteed, especially if there are user interactions like prompting about missing citations, etc.

To reliably run a macro after the scan you'd need some way to know when Nisus Writer had successfully received the data from Bookends and processed it. I don't think there's any such mechanism for you to hook into, sorry.

That said, the usual way to hack a solution in situations like this is to use "polling". Your macro would continually check to see if the scan had finished. That's a problem in itself, since there's no way for the macro to directly ask "has the scan finished?" But there's a variety of proxy conditions you might consider as good enough. For example, if your Bookends scans always replace the document contents (instead of opening a new document window) then you can monitor the document text. Here's a macro that does just that:

Code: Select all

# copy the document's current text
$doc = Document.active
$docText = $doc.text
$oldText = $docText.substringInRange(Range.new(0,$docText.length))

# scan in Bookends
Menu "Tools:Bibliography:Scan Document"

# wait for the document's text to change, which we assume means that Bookends is done
$newText = $oldText
While $newText == $oldText
	Sleep 1
	$docText = $doc.text
	$newText = $docText.substringInRange(Range.new(0,$docText.length))
End

# post-processing after the scan
Prompt "Looks like the Bookends scan is done."
There are a few minor problems with this approach:
1. If you cancel the scan, or anything otherwise disrupts it, the macro doesn't know and will wait forever. You'd have to manually cancel the macro.
2. The macro doesn't care how the change occurs. If you accidentally type a single character while waiting for the scan to finish, the macro assumes Bookends is done.
Þorvarður
Posts: 410
Joined: 2012-12-19 05:02:52

Re: How to use the command 'Scan Document' in a longer macro

Post by Þorvarður »

This is the solution! Wonderful! I'm floating on cloud nine… :–)
Thank you so much, Martin.
Post Reply