How to move the insertion point into a header?

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

How to move the insertion point into a header?

Post by Þorvarður »

Is there a way to move the insertion point into a header or a footer with a macro?

Obviously, this is supposed to be a part of a longer macro where something is pasted or automatically typed into the header.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: How to move the insertion point into a header?

Post by phspaelti »

Hello Þorvarður,
If your goal is to edit a header or footer, then I wouldn't bother "moving the insertion point". You can add/remove stuff using the macro language. I'd love to give you a simple command, but it"s actually going to require some work to accomplish this. Basically you will have to loop through Document.allTexts and check their .documentContentType to find the header/footer(s). If you have different sections with different header/footer(s) this could get tricky to know which is associate with which section (I think. Haven't tried this in a while).

If what you want to insert is an automatic number, then you might need to move the insertion point after all, since that will be a menu command that operates on the selection. For that Find would be the quickest, provided you have a way of unambiguously finding the header/footer. Alternately you could use the above procedure to find the relevant text object and then create a selection object.

There may be easier methods that I am overlooking.
Yes, of course. There is the menu command "Go To Header" in the "View" menu.

Feature Request: I really wish that Nisus Macro language had a Section Object. This object would have properties that point to various useful properties of the section: number, and numbering sections, header/footer settings and pointers to header and footer text objects, margins, etc. Then the document object should have an .allSections command to returen the section objects.
Last edited by phspaelti on 2021-05-13 21:39:38, edited 1 time in total.
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: How to move the insertion point into a header?

Post by phspaelti »

Using a defined command would be a good approach:

Code: Select all

Define Command GetHeaderSelection($doc)
  foreach $text in $doc.allTexts
    if $text.documentContentType == "header"
      $sel = TextSelection.new $text, Range.new 0, $text.length
      return $sel
    end
  end
  return @undefined
end

$doc = Document.active
$sel = GetHeaderSelection($doc)
#View:Page View
$doc.setSelection $sel
Type Text "Howdy"
NB: This code will only find the "first" header. (Not sure what the "first" one is :P )
philip
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: How to move the insertion point into a header?

Post by phspaelti »

Actually once you have moved the selection to the header, you will be able to ask which section this header is attached to. So with a bit more work one could loop through all headers and determine which section they belong to and select the appropriate one.

I spoke too soon. One cannot ask which section a header belongs to. NML returns 'undefined'
philip
credneb
Posts: 187
Joined: 2007-03-28 07:30:34

Re: How to move the insertion point into a header?

Post by credneb »

Mostly just curious, but since identifying/selecting a specific header seems to be difficult in the macro language, wouldn't it be easier to manually select the desired header and then run the macro? Whatever text is to be pasted would have already been copied/generated anyway for the macro to work even if the desired header could be easily selected by the macro.

This, of course, requires not working in draft view.
Perhaps that is an obstacle?
Þorvarður
Posts: 410
Joined: 2012-12-19 05:02:52

Re: How to move the insertion point into a header?

Post by Þorvarður »

Hello Philip,
Yes, of course. There is the menu command "Go To Header" in the "View" menu.
I overlooked that too! This menu command is the right thing for my purpose; and with the defined command you show us once again that there are few things one can not do with Nisus macros. :–)

The macro was actually intended for someone else, someone who has just begun to use Nisus, and he missed a feature from Mellel called "Mentions." The aim was to create a running header by bookmarking a heading and then insert a cross-reference into the header.

My purpose was to encourage him to use the menu commands of Nisus Writer Pro to automate repetitive tasks with very simple, yet effective macros, macros that everybody should be able to write. This takes away the fear of novices of having to be a programmer to write macros.

I realize now that the idea of placing something automatically into the header with a macro is perhaps not very useful, because it clearly amounts to take a sledgehammer to crack a nut.

Anyway, it's good to know that there is a menu command "Go To Header" available from the "View" menu.

Thanks Philip.
Þorvarður
Posts: 410
Joined: 2012-12-19 05:02:52

Re: How to move the insertion point into a header?

Post by Þorvarður »

Hello credneb,
credneb wrote: 2021-05-14 16:00:06 wouldn't it be easier to manually select the desired header and then run the macro? Whatever text is to be pasted would have already been copied/generated anyway for the macro to work even if the desired header could be easily selected by the macro.
You are quite right. As I explained in my answer to Philip, this macro was just meant to be an example for a Nisus beginner of how to write a simple, yet effective macro in Nisus. I'm aware that I didn't choose a good macro example. Now I would just do this manually; but my purpose was to encourage the new user and try to make him understand that if Nisus doesn't have an inbuilt feature to do something, it doesn't mean that it can't be done with a macro.
User avatar
phspaelti
Posts: 1313
Joined: 2007-02-07 00:58:12
Location: Japan

Re: How to move the insertion point into a header?

Post by phspaelti »

Hello Þorvarður,
Þorvarður wrote: 2021-05-16 17:45:01 The macro was actually intended for someone else, someone who has just begun to use Nisus, and he missed a feature from Mellel called "Mentions." The aim was to create a running header by bookmarking a heading and then insert a cross-reference into the header.
I understood that that was what you were trying to do.

I was thinking it should be possible to create a macro that might find all your chapter headings, make sure they start a new section and then copy a cross-reference into the header.

BUT, the stumbling block is that there is no way to control whether a given header has "different headers", which means that if the section had the same header you would end up overwriting the previous running header. So in practice you will need to insert or at least check the sections yourself.
philip
Post Reply