Automatically setting bookmarks with strictly formatted names; also concerning the "open-to-last-position-issue"

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Automatically setting bookmarks with strictly formatted names; also concerning the "open-to-last-position-issue"

Post by Lupp »

There seem to be re-occurring issues with the feature of Writer to set again the "last cursor position" when opening a document from file.
In addition a user (identified by his profile) or different users (with different profiles) may want to be able to find back to one of the positions they recently worked at. This can be done with the help of bookmarks, but in many cases arbitrary names won't be the best choice.
I would suggest to use for this purpose bookmarks with names showing all the relevant information: The user who set it, and a date-time-stamp telling when it was set, both parts in a strict but short format being alphabetically sortable without semantic confusion.
The well tested code below can do that. Efficient usage will require to assign it to some caller, most likely a keyboard shortcut. If a click on a toolbar area is used as the trigger or the routine is called due to a FormControl event, some additional lines of code will allow branching depending on passed information.
The example contains in addition a function usable as an event handler for onGoingToBeClosed(). If used it will automate the setting of a bookmark for the "last position".

As always: If seriously interested in the feature provided here, please also download the example file. This way I can estimate the "impact".
autoBookmarkDemo.odt
(28.82 KiB) Downloaded 258 times

Code: Select all

Sub createUserNamedDTstampedSinglePositionBookmark(Optional pEventInfo)
REM Currently pEventInfo isn't evaluated. You may change that if you want to
REM modify the action dending on what mofiers wer used with a toolbar area
REM or any info provided by a FormControl used for the call.
cDoc       = ThisComponent
If NOT cDoc.supportsService("com.sun.star.text.TextDocument") Then Exit Sub
REM hard-coded names
Const dataNode      = "/org.openoffice.UserProfile/Data"
Const useAsID       = "initials"
REM format strings
Const longDTformat  = "YYYY-MM-DD_HH:MM:SS"
Const shortDTformat = "YYYYMMDDHHMMSS" REM preferred by my current camera, e.g. 
REM Assumed preferences
Const dtDelim       = "_" REM inserted between user ID and dat.
Const bookmarkStart = False
cCtrl      = cDoc.CurrentController
cSel       = cCtrl.Selection
If cSel.Count>1 Then Exit Sub
REM Bookmark shall point to a single position. User should define it unambiguosly.
insPos     = IIf(bookmarkStart, cSel(0).Start, cSel(0).End)
newBm      = ThisComponent.createInstance("com.sun.star.text.Bookmark")
newBmN     = simplePropertyValueFromRegistryModificationsXcu(dataNode, useAsID)
newBmN     = newBmN & dtDelim & Format(Now(), longDTformat)
newBm.Name = newBmN 
cText      = insPos.Text
insCur     = cText.createTextCursorByRange(insPos)
cText.insertTextContent(insCur, newBm, False)
End Sub

REM Thanks to Andrew Pitonyak I could easily code this needed helper: 
'
Function simplePropertyValueFromRegistryModificationsXcu( _
           pNodePath As String, pPropertyName As String) As Variant
simplePropertyValueFromRegistryModificationsXcu = ":err:"
On Local Error Goto fail
Dim structNode, providerSrv
Dim arguments(0) As New com.sun.star.beans.PropertyValue
Const providerSrvN = "com.sun.star.configuration.ConfigurationProvider"
Const structNodeN  = "com.sun.star.configuration.ConfigurationAccess"
providerSrv        = createUnoService(providerSrvN)
arguments(0).Name  = "nodepath"
arguments(0).Value = pNodePath
structNode         = providerSrv.createInstanceWithArguments(structNodeN, arguments())
simplePropertyValueFromRegistryModificationsXcu _
                   = structNode.getPropertyValue(pPropertyName)
fail:
End Function
Last edited by Lupp on Sun Jan 28, 2024 2:47 pm, edited 2 times in total.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Automatically setting bookmarks with strictly formatted names

Post by Hagar Delest »

Thanks for that.
Could the macro delete all previous bookmarks from the current user so that only the last one remains? Of course you could just go to the last one. But at some point, you'll need some house cleaning in all the bookmarks.
Any way to make Writer go to the last user's bookmark automatically at loading (if existing)?
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Automatically setting bookmarks with strictly formatted names

Post by Lupp »

A macro designed to directly be called with a keyboard shortcut can't be modifiable.
I can, however, write a modifiable version for which you can set a defaultMode, or a caller setting the wanted mode basded on some information passed to it.
WHAT modifications would you judge to be useful?
- Delete all autoformatted bookmarks by the same user in advance? (How to decide 'autoformatted'?)
- Interpret the dates and delete all such bookmarks older than 9 days?
- ...
The ID used to distinguish users in case of documents edited by different persons - with different profiles here- is -for the current version- taken from the user profile (say "owner"). Would you prefer a version asking for an "author's name" to be able to distinguish different authors using the same profile?

There are lots of open questions, and there won't be a solution satisfying all the possible preferences.
And after all the software can't know the "CurrentTypist" without being told.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Automatically setting bookmarks with strictly formatted names

Post by Hagar Delest »

Well, first don't get me wrong, I'm not really asking for such feature. The bottom line being: I still hope that this feature will be improved in the future. I'm stuck with LO versions that do mostly work the way I want (meaning it opens at the latest edit position, which is broken in some versions).

I could do my homework and tweak the code also. But not a priority so far. I may not use the macro at all, it's just my 2 cents for how I would see it ideally from my POV.

I would keep it simple: the macro could get the user's name/initials, check if there is any bookmark of a defined formatting (the ones you have assigned) and delete them all. Then create a new one at the cursor position and that's all. I'm for keeping the macro as transparent as possible for the user. In most cases, there may be a single user. Few chances that there are 2 users with the same credentials that edit a document and use the macro.

Maybe there should be also a button that goes to the bookmark of the current user. It may be possible to automate this since it's the current behavior of Writer but may require to go deeper in the code itself rather than with a macro, not sure at all. I know that you can use the Navigator to get to the bookmark but mine is locked on the headings and I'm not ready to lose this sleek view to add all the other sections to find the last bookmark.

Again, that's just ideas, other inputs are welcomed to see if there is really a need to work further on this.
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
JeJe
Volunteer
Posts: 2785
Joined: Wed Mar 09, 2016 2:40 pm

Re: Automatically setting bookmarks with strictly formatted names

Post by JeJe »

The cursor position looks to be saved in OO if you've edited the document - but if you just open it and read it then the new position is lost.

Reminds me of this old thread which you helped with behind the scenes:

https://forum.openoffice.org/en/forum/v ... p?t=101246

A different way to do it would be a little text file in the user profile saved to on exit and the position retrieved on document load using that method. Then you could just read the document and get the closing position saved and loaded again without affecting the document/needing to save it.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Automatically setting bookmarks with strictly formatted names

Post by Lupp »

This topic was split off from a "code-snippet-topic" I had started. Now I'm somehow unhappy about the way the discussion took, and want to state that I was well aware of the use-case to get bookmarked "the last cursor position" for probably more than one user in the same document, but mainly wanted to provide the basis for time-stamped bookmarks depending on "this or that" in a flexible way.

Concerning the "last position per profile owner" I should have emphasied the suggestion to include and customize as event handler the following piece of code:

Code: Select all

Function onGoingToBeClosed(Optional pEvent) As Boolean
createUserNamedDTstampedSinglePositionBookmark()
onGoingToBeClosed = True
End Function
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Hagar Delest
Moderator
Posts: 32670
Joined: Sun Oct 07, 2007 9:07 pm
Location: France

Re: Automatically setting bookmarks with strictly formatted names

Post by Hagar Delest »

My bad.
I've split the topic for the last cursor position issue: Set and retrieve last cursor position (with a bookmark).
LibreOffice 7.6.2.1 on Xubuntu 23.10 and 7.6.4.1 portable on Windows 10
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Automatically setting bookmarks with strictly formatted names

Post by Lupp »

No bad. Just life. I originally came from an ask.libreoffic.org topic where the background clearly were re-occurring issues with the last-position-feature in different versions of LibO. I never had a problem of the kind, but was interested in a different perspective. That was the origin of my snippet...
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply