[Solved] Way to make a macro increment the number in section

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
SteveH_66
Posts: 38
Joined: Sun Apr 13, 2008 4:42 pm

[Solved] Way to make a macro increment the number in section

Post by SteveH_66 »

Hi all. I was just wondering, is there a way to get a macro to automatically increment the number of a section when you put it in by macro instead of manually? When you are doing sections manually, Writer automatically increments the number of the next section to the next number, so the code is in Writer itself somewhere. Like if you are doing a document and you already have Section1, Section2, Section3 -- then when you add a new section, in the New Section Dialog box or whatever, it will automatically have the name of your new section in there and incremented to "Section4". Just wondering if there is a method or function or anything which could be implemented in a macro so that it automatically incremented the Section number like Writer does automatically when you are doing them manually. I need this for my hidden section macros I talk about in this post viewtopic.php?f=20&t=103195&p=499409#p499409 which work great for the most part.

I can live without this functionality, if I have to, it would just make my macros about perfect if I could automatically increment the Section number in the macro itself, or by doing a 3rd macro that did this for me. It would pretty much fully automate the process -- as it is now, I am having to go in and add the numbers to the Sections manually, either each time I do the 'hiddenSection' macro or after I have put in a few sections and then go in and assign them all the correct number. Hope someone knows a way to do this, it would be great. Thanks
Last edited by SteveH_66 on Fri Oct 09, 2020 6:27 am, edited 1 time in total.
OS: Windows 10 X86_64 (build 19045); UI render: Skia/Raster; Libre Office: Version: 25.8.4.2 (X86_64)
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
FJCC
Moderator
Posts: 9644
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Way to make a macro increment the number in section?

Post by FJCC »

Here is some minimal code to insert a text section with a name that increments with the number of sections, that can be either visible or hidden, and that is located at the current location of the cursor. I am not sure exactly what you want to do but I hope this will get you started.

Code: Select all

Sub TextSec
TextSection = ThisComponent.createInstance("com.sun.star.text.TextSection")

SectionCount = ThisComponent.TextSections.Count

TextSection.setName("SectionNumber" & SectionCount + 1
TextSection.isVisible =  TRUE

oVC = ThisComponent.CurrentController.ViewCursor

oText = ThisComponent.Text
oText.insertTextContent(oVC.Start, TextSection, FALSE

End Sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
SteveH_66
Posts: 38
Joined: Sun Apr 13, 2008 4:42 pm

Re: Way to make a macro increment the number in section?

Post by SteveH_66 »

FJCC wrote:Here is some minimal code to insert a text section with a name that increments with the number of sections, that can be either visible or hidden, and that is located at the current location of the cursor. I am not sure exactly what you want to do but I hope this will get you started.

Code: Select all

Sub TextSec
TextSection = ThisComponent.createInstance("com.sun.star.text.TextSection")

SectionCount = ThisComponent.TextSections.Count

TextSection.setName("SectionNumber" & SectionCount + 1
TextSection.isVisible =  TRUE

oVC = ThisComponent.CurrentController.ViewCursor

oText = ThisComponent.Text
oText.insertTextContent(oVC.Start, TextSection, FALSE

End Sub
Hi FJCC, your code will be a huge help to me, once I figure out how to integrate the function of my macros into your code, or how to take pieces of your code and integrate them into my macros. My macros are recorded macros, done as I was setting up the 2 actions I need to be able to do, as most macros by novices are recorded macros as you know -- since the learning curve is so steep for non-programmer. So of course my macro code looks a lot different than yours -- also I was using LO ver 4.2.3.3 for this, don't know if that makes any difference.

Just in case you were not interested enough to read the post I linked to where I described how I do the 2 macros I use and what they are for that I linked to in my OP, but are interested enough to know what I will be trying to do with your code, here is what I do with them. By the way, some of this is my original work, some of it was gotten from elsewhere, I couldn't find where I got it -- so when my system crashed and I lost them, I had to recreate them on my own and finally did -- with a lot of help from some other members/gurus here, many thanks to those folks :D . But I can't give credit to whoever the people were who helped me set this up many years ago unfortunately :( And that post I linked to in my OP in this thread tells how I do it, because I use it for outlining and I think others who won't/can't use Navigator for outlining might find it very useful as I have over the years I have been using it. All you have to do is assign shortcuts to the macros and you can very quickly add hidden sections :D

I have one macro, called "hideText", here is the code :

Code: Select all

sub hideText
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(5) as new com.sun.star.beans.PropertyValue
args1(0).Name = "Type"
args1(0).Value = 8
args1(1).Name = "SubType"
args1(1).Value = 2
args1(2).Name = "Name"
args1(2).Value = "hide"
args1(3).Name = "Content"
args1(3).Value = "1"
args1(4).Name = "Format"
args1(4).Value = 10000
args1(5).Name = "Separator"
args1(5).Value = " "

dispatcher.executeDispatch(document, ".uno:InsertField", "", 0, args1())


end sub
I have another macro named "hiddenSection" and here is the macro code for it :

Code: Select all

sub hiddenSection
rem ----------------------------------------------------------------------
rem define variables
dim document   as object
dim dispatcher as object
rem ----------------------------------------------------------------------
rem get access to the document
document   = ThisComponent.CurrentController.Frame
dispatcher = createUnoService("com.sun.star.frame.DispatchHelper")

rem ----------------------------------------------------------------------
dim args1(6) as new com.sun.star.beans.PropertyValue
args1(0).Name = "RegionName"
args1(0).Value = "Section"
args1(1).Name = "RegionCondition"
args1(1).Value = "hide==1"
args1(2).Name = "RegionHidden"
args1(2).Value = true
args1(3).Name = "RegionProtect"
args1(3).Value = false
args1(4).Name = "LinkName"
args1(4).Value = ""
args1(5).Name = "FilterName"
args1(5).Value = ""
args1(6).Name = "SubRegion"
args1(6).Value = ""

dispatcher.executeDispatch(document, ".uno:InsertSection", "", 0, args1())


end sub
'hideText' makes a Set variable with a value of 1. You place that at the end of whatever line on the numbered list or whatever you are using for your outline, and by changing the value from 1 to 0 (or any other number but 1) it toggles the section from hidden to visible.

'hiddenSection' makes a section with the "Region Condition" Value "hide==1", so the section will be hidden if 'hideText' variable field is set to 1 in the document. As you may notice by looking at the code, the 'hiddenSection' "Region Name" value is "Section" -- when it was originally created "Region Name" value was "Section1" because that was the name the Section was originally created as -- I just deleted the 1, so all the hidden Sections would be named "Section" to cut down on confusion and possible macro problems. Then, I was having to change the Sections from Section to "Section1" etc -- either at the time I did the macro or go in after a few macros and modify them all so they had the right Section numbers.

So anyway, how the 2 macros work is that you put the cursor at the end of a line where you want a hidden section when outlining (because you want longer detailed text in there - say character descriptions or whatever) and run 'hideText' and it places that Variable field there with it set to 1. Then, you put a "Unnumbered Entry" under that line, and run 'hiddenSection' macro and it puts in a Section which you can hide/reveal just by changing the 'hideText' variable number from 1 to any other, because it's Hide Value was set to 'hide==1' when you recorded the macro. So it would look like this :

Code: Select all

[list=]
1. Chapter 1
     1. Paragraph 1
           1. Scene 1 [0]
                This is the hidden Section, with text for scene 1.
           2. Scene 2
     2. Paragraph 2
[/list]
Like that. So what you did was a huge help to me. Now all I have to do is figure out how to change your code around so I can have it hide/reveal with that "hide==1" value, so 'hideText' can hide or reveal the sections, or figure out what to take out of your code and integrate into my macro so that it gives me that incrementing Section name function your code gives. Or hope that you or someone else will see this post and take some pity on me and help me with some advice on how to modify one or the other -- your code or mine. Anyway, thanks for the help you have given FJCC, at least now I know that incrementing numbers for Section name can be done, even though as a novice I don't fully understand what you did or how it could be taken and integrated into my macro, or how to build a fancy macro like you did but which has the section Hide Value set as 'hide==1' :D
OS: Windows 10 X86_64 (build 19045); UI render: Skia/Raster; Libre Office: Version: 25.8.4.2 (X86_64)
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
FJCC
Moderator
Posts: 9644
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Way to make a macro increment the number in section?

Post by FJCC »

Here is a slightly modified version of my code that sets the section to be hidden when the variable named Var1 is set to 1. It seems that the section is visible when first inserted even if Var1 is 1. I had to toggle the variable to get the effect to work.

Code: Select all

TextSection = ThisComponent.createInstance("com.sun.star.text.TextSection")

SectionCount = ThisComponent.TextSections.Count

TextSection.setName("SectionNumber" & SectionCount + 1
TextSection.isVisible =  FALSE
TextSection.Condition = "Var1==1"

oVC = ThisComponent.CurrentController.ViewCursor

oText = ThisComponent.Text
oText.insertTextContent(oVC.Start, TextSection, FALSE
end sub
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
SteveH_66
Posts: 38
Joined: Sun Apr 13, 2008 4:42 pm

Re: Way to make a macro increment the number in section?

Post by SteveH_66 »

FJCC, your macro is working for my needs beautifully! Thank you so very much for modifying your code to help me out like that, it is such an awesome improvement to my outlining technique. :D

On the thing with the Section being visible even with Var1 set to 1 when it is first inserted, I noticed the same thing with my Set variable and my 'hiddenSection' macro. It must be some kind of bug or oddity in the Writer code. It is a very small hiccup to have, in return for the outlining functionality this method gives one. Again, thank you very much :D

P.S. Do you think this would be very hard for the developers to implement as a feature somewhere in Writer? If it would not be and you have any influence with the developers maybe you could suggest they do it. You would make a lot of people who use outlining but "don't like/can't learn" outlining with Navigator and want a simple method of outlining very happy. I have followed discussions for years on bugs and such filed asking for pretty much something like this for many years. Just a thought.
OS: Windows 10 X86_64 (build 19045); UI render: Skia/Raster; Libre Office: Version: 25.8.4.2 (X86_64)
Build ID: 290daaa01b999472f0c7a3890eb6a550fd74c6df; Apache OpenOffice: AOO4116m3(Build:9816) - Rev. 277251aa7e
Locked