Print specific pages

Everything related to our flagship word processor.
johseb
Posts: 27
Joined: 2016-02-13 10:01:29

Print specific pages

Post by johseb » 2019-08-24 04:45:38

Maybe I'm missing something obvious here but how can I print e.g. pages 1 to 3 plus page 9 and page 12?

Word 2016 print dialog has a dedicated setting for this but the option is not available when printing from NWP3.

Screen Shot 2019-08-23 at 22.48.37.png
Screen Shot 2019-08-23 at 22.48.37.png (89.97 KiB) Viewed 3635 times

The obvious workaround is to create a PDF and then print only selected pages; a time waste especially considering that during long docs preparation such partial printing could be frequent.

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

Re: Print specific pages

Post by adryan » 2019-08-24 05:16:45

G'day, johseb et al

I currently use a Brother MFC-J5730DW Printer. I looked at the Print dialog box for documents in several applications: in none of them was there a Page Range option such as that depicted in your screenshot — just "All" and From… to".

So it would appear that that Page Range option is specific to Word (which I don't have).

You could probably devise a Nisus Writer macro (or an AppleScript script) that presented a dialog box for input of a page range that was then printed. There would be no need to convert a document to PDF beforehand.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.6
Nisus Writer user since 1996

johseb
Posts: 27
Joined: 2016-02-13 10:01:29

Re: Print specific pages

Post by johseb » 2019-08-24 07:34:55

Hi Adrian,
sorry for not being clear in my post; yes, that option is supplied by Word and it will not be available in NWP unless it is included as a new feature.

As for the macro, I didn't find any existing macro for a print task to take as a starting point and, honestly, starting from scratch is above my pay grade.

So it seems that this thread boils down to a feature request for NWP3 to include the described print option.
Unless some good fellow has a macro in his/her drawer :P

xiamenese
Posts: 440
Joined: 2006-12-08 00:46:44
Location: London or Exeter, UK

Re: Print specific pages

Post by xiamenese » 2019-08-24 08:17:51

Have you tried entering something like 3–5, 11–15 or whatever the ranges you need are into that box? I’m on my iPad at the moment with a Samsung printer that doesn’t support air print … so there’s no way I can test it out. That used to be how you set complex page ranges.

Mark

johseb
Posts: 27
Joined: 2016-02-13 10:01:29

Re: Print specific pages

Post by johseb » 2019-08-24 08:41:17

Good suggestion, thank you. The print dialog only has the "From" and "To" fields available for user input and both of them only accept a number (no commas or hyphens).

P.S. Reading again your post maybe you interpreted my request as the need to print a range es 12-15. This can be done. What I'm missing is the ability print specific non adjacent pages.

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

Re: Print specific pages

Post by adryan » 2019-08-24 22:58:20

G’day, johseb et al

I thought I’d look into this a bit more: we need a script of some sort.

We can’t use a macro that relies only on commands available in the NWP hierarchical menu structure because you can’t set the page numbers in the Print dialog box other than manually. As far as I could determine, the NWP Macro Language offers no Print commands at all, let alone with parameter setting. The only way to get a NWP macro to do the job is to get it to call a script from another scripting language.

So I looked at AppleScript. The good news is that the AppleScript Dictionary for NWP contains commands for setting the print parameters we want. The bad news is that I haven’t got my script to work.

Code: Select all

-- *** For want of a better way to get a reference to the frontmost Nisus Writer Pro document, we appeal to System Events.
tell application "System Events"
	set this_document to document of window 1 of application "Nisus Writer Pro"
	set this_file to path of this_document
end tell

-- Now we do the main business.
tell application "Nisus Writer Pro"
	
	activate
	
	set savedDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {","}
	
	set dialog_title to "Printing Non-contiguous Pages"
	set dialog_text to "Enter page numbers of pages to be printed.

Separate page numbers by a hyphen or an en dash if they are contiguous, or by a comma if they are non-contiguous."
	display dialog dialog_text with title dialog_title default answer ""
	
	set string_of_pages to (the text returned of the result)
	
	set page_string to ""
	
	repeat with character_number from 1 to length of string_of_pages
		set this_character to character character_number of string_of_pages
		if this_character is "-" then set this_character to "–"
		if this_character is not " " then set page_string to page_string & this_character
	end repeat
	
	set page_string to page_string as list
	
	repeat with substring in page_string
		if substring does not contain "–" then
			set first_page_to_print to substring as integer
			set last_page_to_print to first_page_to_print as integer
		else
			set AppleScript's text item delimiters to {"–"}
			set substring to substring as list
			set first_page_to_print to (item 1 of substring) as integer
			set last_page_to_print to (item 2 of substring) as integer
			set AppleScript's text item delimiters to {","}
		end if
		
		-- We should now have all we need to start printing.
		-- *** But the following line has problems.
		print file this_file print dialog yes with properties {starting page:first_page_to_print, ending page:last_page_to_print, copies:1, target printer:"Brother MFC-J5730DW"}
	end repeat
	
	set AppleScript's text item delimiters to savedDelimiters
end tell
I draw your attention to the two comments that include asterisks. We want to call the script when we have a NWP document open, but I have found it very difficult to get the required reference to the document automatically. Hence the resort to System Events. But the reference may still not be quite what the print command farther down requires. I’ve encountered various errors along the way, including messages to the effect that the NWP document (whose path the error message gave correctly) did not exist, when it clearly did exist right there in the Finder where it should be!

I’m happy with the manipulation of the user input: I’ve checked it at every step. But there’s something wrong in that print command line. Even when the Print dialog box opens, the user-derived parameters are not entered in the appropriate fields. I don’t know whether I’ve got something wrong or whether there is a bug in NWP’s AppleScript implementation. I hope someone out there can help.

A couple of closing remarks….

My script allows the entry of spaces, whether or not this is done systematically and consistently. It also allows either hyphens or en dashes to be used in designating page ranges. Apart from this, the script does no error-checking. In particular, if a page number appears more than once in the user input (explicitly or implicitly), it will get printed more than once; but this could accord with the user’s intention.

Since pages are dealt with in the order they appear in the user’s input, rather than in the order they appear in the document, it would be possible to manipulate the printing order without having to alter the document. This might be useful on occasions.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.6
Nisus Writer user since 1996

johseb
Posts: 27
Joined: 2016-02-13 10:01:29

Re: Print specific pages

Post by johseb » 2019-08-25 00:27:32

Adrian,
modifying a couple of lines the script executes correctly up to the print panel.

Code: Select all

-- *** For want of a better way to get a reference to the frontmost Nisus Writer Pro document, we appeal to System Events.
tell application "System Events"
	set this_document to document of window 1 of application "Nisus Writer Pro"
	set this_file to path of this_document
end tell

-- Now we do the main business.
tell application "Nisus Writer Pro"
	
	activate
	
	set savedDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {","}
	
	set dialog_title to "Printing Non-contiguous Pages"
	set dialog_text to "Enter page numbers of pages to be printed.

Separate page numbers by a hyphen or an en dash if they are contiguous, or by a comma if they are non-contiguous."
	display dialog dialog_text with title dialog_title default answer ""
	
	set string_of_pages to (the text returned of the result)
	
	set page_string to ""
	
	repeat with character_number from 1 to length of string_of_pages
		set this_character to character character_number of string_of_pages
		if this_character is "-" then set this_character to "–"
		if this_character is not " " then set page_string to page_string & this_character
	end repeat
	
	set page_string_list to every text item of page_string
	
	repeat with substring in page_string_list
		if substring does not contain "–" then
			set first_page_to_print to substring as integer
			set last_page_to_print to first_page_to_print as integer
		else
			set AppleScript's text item delimiters to {"–"}
			set substring_list to every text item of substring
			set first_page_to_print to (item 1 of substring_list) as integer
			set last_page_to_print to (item 2 of substring_list) as integer
			set AppleScript's text item delimiters to {","}
		end if
		
		-- We should now have all we need to start printing.
		-- *** But the following line has problems.
		print this_file print dialog yes with properties {starting page:first_page_to_print, ending page:last_page_to_print, copies:1, target printer:"Brother MFC-J5730DW"}
	end repeat
	
	set AppleScript's text item delimiters to savedDelimiters
end tell
I modified the creation of the list of items from user's input and page range substring

Code: Select all

set page_string_list to every text item of page_string
…
…
set substring_list to every text item of substring
and used

Code: Select all

print this_file
instead of

Code: Select all

print file this_file
So now the print dialog opens but doesn't pick up the correct values form the print settings list (it is set to print All pages)

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

Re: Print specific pages

Post by adryan » 2019-08-25 02:33:11

G'day, johseb et al

Thanks for your input on this, johseb.

I am assuming you would have altered the target printer parameter in the code to reflect your own printer. Not that it matters much, if the dialog box is opening but other parameters are not entered correctly. It would just need to be changed in order for printing to proceed eventually. (I'm sure you know this, but I'm aware that others who follow these discussions may try things without spotting such potential problems. At this stage, I'm only interested in getting all the syntax right.)

You didn't seem to disagree with the rest of the code in that print line, so it looks as though there could be a problem with the NWP AppleScript implementation.

There is a moral to all this, too. I am annoyed that I neglected the First Law of Programming; viz, Attack the critical bits first. (The Zeroth Law is: Get someone else to do it!) It's all too easy to get seduced into doing the fun stuff, but it counts for nothing if everything comes to a grinding halt at some critical juncture. In retrospect, I should have tested that print hook first, as it was a readily identifiable potential point of failure.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.6
Nisus Writer user since 1996

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

Re: Print specific pages

Post by adryan » 2019-08-25 04:31:38

G'day, johseb et al

It's a problem with AppleScript, at least under Mojave. The AppleScript Dictionary for BBEdit has similar commands to NWP, but attempting to print closes all open documents and the editing window, with no dialog box in sight.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.6
Nisus Writer user since 1996

martin
Official Nisus Person
Posts: 4610
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Print specific pages

Post by martin » 2019-08-26 10:33:36

The print dialog options to print a range of pages using the "from" and "to" fields is standard. It's something provided by the system. The extended options in Word are indeed a custom feature provided by Microsoft.

I was going to offer another potential way to come at this problem from the AppleScript side of things, but I'm not sure it will work. I was going to say that instead of repeatedly printing a different page range of the document — which is likely to be slow, problematic, and potentially confusing — maybe save a PDF of the whole document, post-process it down to just the pages you want, and then print that instead.

I expected Apple's Preview app to have AppleScript commands for basic PDF processing. The idea was to use Preview to isolate just the PDF pages you want, and then print that synthesized PDF in one go. That appears to be a non-starter; I see no AppleScript commands for PDF processing in Preview.

There do seem to be Automator commands that could help (eg: Split PDF). So maybe you could save the whole PDF from Nisus Writer and process it using Automator. It could ask for the desired pages, generate the selective PDF, and print it?

johseb
Posts: 27
Joined: 2016-02-13 10:01:29

Re: Print specific pages

Post by johseb » 2019-08-26 13:19:56

I "solved" the problem with a Keybboard Maestro macro (as it often happens).
I put solved in quotes because it is simply a macro faking the user interactions in the NWP3 print dialog (you know, flashing panels, cursor magically jumping here and there; a kludge).
It somehow works but it's not nice.
I'm not proud of it but I'm attaching here a copy in case it is useful to someone.

EDIT: Keyboard Maestro macro deleted. Superseded by Martin's native NWP macro (see below).
Last edited by johseb on 2019-08-28 16:23:23, edited 1 time in total.

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

Re: Print specific pages

Post by adryan » 2019-08-26 16:54:58

G'day, johseb, Martin et al

I'm not a fan of relying on Apple's PDF system if it can be avoided. I think I've found another solution to johseb's original problem, but I'm not quite sure how to implement it.

The CUPS environment offers a command for selecting a range (or several comma-separated ranges) of pages for printing.

https://www.cups.org/doc/options.html#pageranges

I note that the Brother printer installer has already installed a number of files with "CUPS" in the filename in my main computer Library.

So (with much hand-waving!) it should be possible to have an AppleScript script employ the "do shell script" command to run a Terminal-type script that invokes the required CUPS command.

That's the general idea, anyway. One advantage of this method is that it would work for a variety of applications (NWP, Preview, etc).

Exactly how to access the CUPS environment, create the Terminal script, and get the AppleScript script to feed user input to the Terminal script, I don't know. Maybe all the processing of user input is better done in Terminal anyway.

Whaddyareckon?

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.6
Nisus Writer user since 1996

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

Re: Print specific pages

Post by adryan » 2019-08-28 02:17:07

G'day, all

It occurred to me that, since I had CUPS-related files for my Brother printer installed in one of my Libraries, perhaps I could, without any preamble at all, simply use CUPS commands in Terminal to print files. And so it proved!

All I had to do was connect the printer, open Terminal, enter the lp command for selecting a range of pages (see link in my previous posting), drag the file from the Finder and drop it into Terminal, then execute. It worked, even for printing a couple of non-consecutive pages.

Unfortunately, .rtf was deemed to be an "unsupported document-format", but .txt and .pdf files were successfully printed.

So the method I outlined earlier is feasible. Unless there is overwhelming demand from the assembled throng, though, I won't pursue this any further, as for me the restriction on RTF files limits the utility too much. But it's probably not too difficult to create a more user-friendly front end, with dialog boxes and such, that would allow a Nisus Print Page Range macro to access the CUPS environment via a combination of AppleScript and Terminal.

I'll just add that it would obviously be possible to convert an RTF file to a PDF one before proceeding, but the Macintosh PDF system can't even handle Mail or Safari files properly, so I would be most reluctant to employ a method that used PDF as an invisible intermediary for printing. It might be OK if you start with a nice PDF document, but not if you start with some other format and expect the conversion to PDF to produce something akin to a screenshot. Still, it's worth keeping this method in the back of your mind in case you need to print non-contiguous ranges of pages from a PDF document you've already checked.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.6
Nisus Writer user since 1996

martin
Official Nisus Person
Posts: 4610
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Print specific pages

Post by martin » 2019-08-28 08:38:38

adryan wrote:
2019-08-28 02:17:07
All I had to do was connect the printer, open Terminal, enter the lp command for selecting a range of pages (see link in my previous posting), drag the file from the Finder and drop it into Terminal, then execute. It worked, even for printing a couple of non-consecutive pages.
Ah that's great! Thanks for digging up the "lp" command. A macro can employ it. I'm attaching a macro that gathers the desired page numbers (and/or page ranges) and sends it all off to Perl and the lp command for printing. It should do exactly what's desired.
Print Page Ranges.nwm
(6.69 KiB) Downloaded 82 times
The only potential snag is sandboxing. The macro works by first saving a PDF to a temporary file, which is picked up by Perl. It's possible sandboxing could prevent Perl from accessing the temporary file. That all depends on macOS. Luckily it doesn't seem to be a problem. I had no trouble while I was testing the macro.
I would be most reluctant to employ a method that used PDF as an invisible intermediary for printing
I think your reluctance to use PDF is misguided. Generating a PDF on macOS is essentially the same as making a printout. For any modern app the process involved uses exactly the same pagination and drawing code. It's just that the data is captured as a PDF file instead of being sent to the printer immediately.

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

Re: Print specific pages

Post by adryan » 2019-08-28 14:23:04

G'day, Martin et al

Thanks for another great addition to the macro collection, Martin.

With respect to my distrust of the Mac's PDF system, it's born of long experience — including yet again yesterday, as a matter of fact. I think there are two factors involved. One is that line wrapping appears differently from the original, although this is probably understandable when dealing with Mail and Web browsers. The other is that paper size is set inconsistently in the Print dialog (which is something you never get to check if you use the Export as PDF command, so I tend to avoid it). What seems to happen is that, even if a printer preset configuration specifies a particular paper size, a different paper size can appear specified elsewhere in the labyrinth of the Print dialog. Having said all this, I don't think I've experienced such problems when converting NWP documents to PDF files, so my scepticism is more in the nature of a default position, I guess.

Cheers,
Adrian
MacBook Pro (mid-2014)
macOS Mojave 10.14.6
Nisus Writer user since 1996

Post Reply