Selecting paragraph and formatting

Get help using and writing Nisus Writer Pro macros.
Post Reply
vwnisus
Posts: 58
Joined: 2016-04-24 00:13:31

Selecting paragraph and formatting

Post by vwnisus »

I realise that using the Nisus Writer Pro macro language is the preferred approach, but since I am first trying to learn AppleScript I would like to know if/how the following is possible:

1. In AppleScript select successive paragraphs (eg the first paragraph, the second paragraph); then

2. with Do Menu Macro with macro command format each paragraph and apply formatting in Nisus Writer Pro.

For example,

1. select first paragraph then format it; then
2. select second paragraph then format it

and so on.

I cannot work how to select a paragraph with an AppleScript command.

A search of the Nisus Macro forum produced:

Code: Select all

tell application "Nisus Writer Pro"
	set doctext to text of front document
	set para to paragraph 1 of doctext
end tell
but I dod not know how to then apply the Do Menu Macro with macro command to it.

Any help would be gratefully received.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Selecting paragraph and formatting

Post by phspaelti »

Just to be clear, it isn't just the choice of applescript that is going to make this clunky, it's the whole approach to doing things. If your goal is to format a document, I just wouldn't go through the document one paragraph at a time. This might work as a "demonstration object" (a proof of concept — "It works!"), but the resulting macro will be too slow for real work on any document of good size.

The problem is that select-format-repeat, ends up interleaving the work steps with access to the GUI. And the GUI has a large overhead. Using applescript is just likely to make the problem worse. The Nisus Way is to select large chunks, non-contiguously if necessary, and applying the formatting just once. Also selecting is best done with PowerFind.

But to answer the basic question, you can select the first paragraph using applescript like so:

Code: Select all

tell application "Nisus Writer Pro"
	Do Menu Macro with macro "Select Paragraph 1"
end tell
To do anything else in Nisus you can just keep repeating the Do Menu Macro with macro ad nauseum using commands from either the Nisus menus or the Nisus macro language. Alternately you can put the commands in a single text variable—with return separated lines—and give the text as an argument to the Do Menu Macro with macro command once. This is why in the end you are better off writing the commands into a Nisus macro and then having applescript call that macro (again with the Do Menu Macro with macro command), at which point the best strategy is to ditch the applescript altogether, and just use the Nisus macro directly.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Selecting paragraph and formatting

Post by phspaelti »

And one comment on this:
vwnisus wrote: 2018-11-12 09:24:44A search of the Nisus Macro forum produced:

Code: Select all

tell application "Nisus Writer Pro"
	set doctext to text of front document
	set para to paragraph 1 of doctext
end tell
This code will basically extract the text from the first paragraph of the document and put it into a container in applescript. This might be useful if you wanted to do further calculations based on that text, or if you wanted to pass the text to some other application. But this gives you no access to the actual paragraph in Nisus which will remain unchanged.

You could of course modify the text in applescript and then pass it back to Nisus which could insert it somewhere, anywhere, in the same document, or even another document, but I'm pretty sure you would lose the formatting on that bit of text.
philip
adryan
Posts: 561
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Selecting paragraph and formatting

Post by adryan »

G’day, vwnisus et al

I agree with what Philip says about practical utility, but the code fragment adduced by vwnisus, together with the accompanying query, raise the question of whether it’s possible to pass the value of an AppleScript variable to Nisus Writer, so that Nisus Writer itself can then work with it, either directly or via the Do Menu Macro command. This is what I take to be the point of the original query.

I combine AppleScript scripts and Nisus macros a fair bit, but the problem as thus enunciated has me stumped.

While NWP has quite good AppleScriptability, the one thing lacking (as far as I am aware) is some means of getting AppleScript to select something in a Nisus document that is referenced by an AppleScript variable. Without this ability, a Do Menu Macro command implicitly starts by working at a selection or at the insertion point, but these will already have been set by Nisus Writer and are not set by AppleScript itself. A quick survey of my own scripts shows that the called macros all work on entire documents: I haven’t needed to work with some fragment returned by an AppleScript script.

In the original script example, you could get AppleScript to place the contents of Paragraph 1 in the Clipboard whence it could be Pasted into a Nisus document or accessed by a Nisus macro (and hence the Do Menu Macro command), but things get out of hand very quickly if the Clipboard contents are not uniquely represented in the Nisus document. For example, the Clipboard doesn’t know that its contents represent the first paragraph, so the macro may not select the correct chunk of text for subsequent manipulation unless you inform it of this explicitly. Any workarounds are either convoluted or undermine the whole initial approach.

In his last posting Philip has in fact mentioned the lack of access Nisus Writer has to the result returned by a (possibly implicit) get statement in AppleScript, so I guess I’m just paraphrasing that to some extent.

I might just add that, while I find AppleScript indispensable in my daily Macintosh use, I also find it the most frustrating of the various computer languages I have dealt with over the years. Apple’s own documentation is sometimes helpful, sometimes not. Applications you would expect to be recordable are not. Error messages to the effect that something or other is not understood by an application, when you might reasonably expect it to be, are far too common. Errors that should be flagged at compile time are deferred until run time for your viewing pleasure. All too often I find myself floundering, trying all sorts of tactics before AppleScript at last condescends to do what I wanted it to do from the outset.

I offer these remarks, not to dissuade anyone from embarking on a journey with AppleScript, but more to extend such comfort as the knowledge that others have found AppleScript’s apparent user-friendliness somewhat deceptive may afford.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Ventura
Nisus Writer user since 1996
vwnisus
Posts: 58
Joined: 2016-04-24 00:13:31

Re: Selecting paragraph and formatting

Post by vwnisus »

Philip, Adrian,

Thank you for the detailed responses.

I did not realise it was possible to select paragraphs with a Nisus Writer Pro macro.

I have only follow-up question regarding AppleScript and the Do Menu Macro with macro command. This only permits the running of a successive Nisus Writer Pro commands but not a macro (that is it is not possible to run a .nvm file from AppleScript)?

Victor
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Selecting paragraph and formatting

Post by phspaelti »

You mean a ".nwm" file? A macro is just another "menu command" in Nisus. So, yes, of course you can run it from an applescript. In fact to do anything complex in Nisus, that would be my recommendation: write a Nisus macro, then run it from the applescript with a Do Menu Macro with macro command.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: Selecting paragraph and formatting

Post by phspaelti »

Since we're on the topic, I decided to try an experiment. I tried to use applescript to write a script to highlight every paragraph where the second letter was an "i". Not a particularly sensible task, but just something to test. I ran it on a list of words with about 1,500 paragraphs.

The applescript looked like this:

Code: Select all

tell application "Nisus Writer Pro"
	set docText to text of front document
	repeat with i from 1 to count of paragraphs in docText
		set para to paragraph i of docText
		if length of para is greater than 2 then
			if second character of para is equal to "i" then
				Do Menu Macro with macro "Select Paragraph " & i
				Do Menu Macro with macro "Highlight:Pink"
			end if
		end if
	end repeat
end tell
The result is quite a show, as you can watch Nisus scroll through the file highlighting stuff.

Then I tried the corresponding task in Nisus Macro language. Trying to write it in the same manner is actually quite ridiculous. So the following is an applescript that sets a variable to an (uneconomical) Nisus Writer macro and then runs it.

Code: Select all

tell application "Nisus Writer Pro"
	set NisusMacro to "Select Document Start
while Select Next Paragraph
$sel = TextSelection.active
if $sel.length > 2
if $sel.subtext.characterAtIndex(1) == 'i'
Highlight:Pink
end
end
end"
	Do Menu Macro with macro NisusMacro
end tell
While not a good way to do this task, this applescript beats the earlier one by quite a bit. After a short delay the action completes.

Note that here I wrote the Nisus macro into the applescript, but as mentioned earlier, one could just save the macro as a Nisus Macro in Nisus and then run it by calling by name from the Do Menu Macro with macro command. But as I said, I wouldn't do this task this way in Nisus.

Of course the much better way to accomplish this task is to use Find in Nisus.

Code: Select all

tell application "Nisus Writer Pro"
	Do Menu Macro with macro "Find '^.i.+\n', 'Ea'"
	Do Menu Macro with macro "Highlight:Pink"
end tell
This completes instantaneously.
philip
Post Reply