Save /back up Basic Libraries with macro, rename Libraries

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
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Save /back up Basic Libraries with macro, rename Libraries

Post by JeJe »

Attached extension makes it easy to back up libraries. Someone recommended starting all your libraries with a couple of letters so they can be distinguished from other peoples - hence the feature of selecting libraries this way.
Second dialog to edit exported libraries
-rename them
-rename modules - includes replacing calls to the module eg in dialog controls
-replace text in files eg to change location=document to location = application
-open files in Writer

Use with caution - don't work on your only copy of a library
See module AAUser


Simple script to export/back up your Basic Libraries. (I've noticed that information in libraries can sometimes be lost when using the organiser and backing them up individually via the dialog is tedious)

Code: Select all


sub test()
SaveBasicLibraries ConvertToUrl("C:\tmp\libraries" & "2" ' format(date,"ddmmyy"))
end sub
Sub SaveBasicLibraries(URL as string)

   dim i as long,libnames,Blibs,Dlibs,Dlibnames

   Blibs = GlobalScope.BasicLibraries

   libnames= Blibs.getelementnames

   for i = 0 to ubound(libnames)
         Blibs.ExportLibrary libnames(i),URL,nothing
   next


   Dlibs = GlobalScope.DialogLibraries
   Dlibnames= Dlibs.getelementnames

  for i = 0 to ubound(Dlibnames)
         Dlibs.ExportLibrary Dlibnames(i),URL,nothing
   next


   Msgbox "Libraries Exported"


End Sub


EDIT: MY ORIGINAL POST DIDN'T EXPORT DIALOGS - AMENDED.
EDIT2: Rename library code posted further on in thread
EDIT3: Added a dialog to the extension to Edit exported libraries
Attachments
JeIDE.oxt
(12.46 KiB) Downloaded 323 times
Last edited by JeJe on Tue Dec 04, 2018 1:25 am, edited 7 times in total.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Save /back up Basic Libraries with macro

Post by JeJe »

UPDATED THREAD AS ORIGINAL VERSION JUST SAVED MODULES NOT DIALOGS.

Think okay now - but don't rely on for anything critical (eg creating your only copy of a library)
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Zizi64
Volunteer
Posts: 11352
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Save /back up Basic Libraries with macro

Post by Zizi64 »

A similar topic:
viewtopic.php?t=37459
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Save /back up Basic Libraries with macro

Post by JeJe »

Thanks, yeah. Another approach would be to save the User Profile.

This document from 2006 discusses implementing multiple library export:
... allowing multiple library export would cause considerable work as currently the library listbox at the “Organizer dialog/Libraries” tabpage does not support multi selection and none of the other functionality provided there (Edit, Password, Delete) supports it either. So the corresponding buttons would always have to be disabled in case of a multi selection. The bottom line is that the small advantage of multi library export does not seem to justify the additional work. So it will not be realised for now.
https://www.openoffice.org/specs/ide/Ba ... rtSpec.odt

The thing I like about OO is even though it doesn't have this feature - it allows access to the things necessary to implement it yourself if you want it - just with OOBasic. I don't know anything about the effort needed to code the above in the main program... but in Basic that dialog I wrote took very little time.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: Save /back up Basic Libraries with macro

Post by RoryOF »

The problem with saving the User Profile is that, on occasions, it is necessary to delete the User Profile; the ability to restore one's selection of macros to a new (uncorrupted) User Profile could be very useful.
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Save /back up Basic Libraries with macro

Post by JeJe »

Here is my rename code.

It exports a library, renames the folder,
then loads each document and does a find/replace for instances of the library name.
(NB: there may be some I've missed)
You can then import the modified library.

This is all tedious doing it manually. The code could also be modified to do a find/replace for location=document vs location=application

Again, take care if you use this code, examine the results and keep a backup of the original library just in case.

Code: Select all

sub test

	dim URL as string,pth as string, STS() AS STRING,libname as string,newlibname as string

	libname="JePickers"
	newlibname="JePickers2"

	oPathSettings = CreateUnoService( "com.sun.star.util.PathSettings" )
	URL= oPathSettings.UserConfig
	pth=convertfromURL( URL)

	CreateRenamedLibrary pth, libname,newlibname

	redim sts(5)
	sts(0) = "quot;" & libname 
	sts(1) = "quot;" & newlibname 
	sts(2) =  ":" & libname & "."
	sts(3) = ":" & newlibname & "."
	sts(4) = "library:name=" & chr(34) & libname
	sts(5) = "library:name=" & chr(34) & newlibname


	openFolderAndReplaceStrings pth,sts()
	Msgbox "Done"

end sub

sub CreateRenamedLibrary(pth as string, libname as string,newlibname as string)
	dim i as long,Blibs,Dlibs,URL as string

	URL = convertToURL(pth)
	Blibs = GlobalScope.BasicLibraries
	if Blibs.hasbyname(libname) then
		Blibs.ExportLibrary libname,URL,nothing
		gotlib = true
	end if
	Dlibs = GlobalScope.DialogLibraries

	if Dlibs.hasbyname(libname) then
		Dlibs.ExportLibrary libname,URL,nothing
		gotlib= true
	end if

	if gotlib = true then

		Name (pth & GetPathSeparator() & libname) as  (pth & GetPathSeparator() & newlibname)

		pth = (pth & GetPathSeparator() & newlibname &  GetPathSeparator())

	end if
end sub


sub openFolderAndReplaceStrings(pth as string,sts() as string)
	Dim NextFile As String,fpth as string

	NextFile = Dir(pth, 0)

	While NextFile  <> ""
		fpth = pth &  nextfile
		OpendocAndreplaceString pth & nextfile,STS()
		NextFile = Dir
		Wend
end sub


Sub OpendocAndreplaceString(pth as string,sts())

		Dim oDoc
		Dim oVal(2) As New com.sun.star.beans.PropertyValue
		dim URL as string
		Dim oRD as Object

		oVal(0).Name = "Hidden"
		oVal(0).Value = true
		oVal(1).Name = "FilterName"
		oVal(1).Value = "Text (encoded)"
		oVal(2).Name = "FilterOptions"
		oVal(2).Value = "UTF8,LF"

		oDoc = StarDesktop.loadComponentFromURL(convertToURL(pth), "_blank", 0, oVal())

		oRD = ODoc.createReplaceDescriptor
		oRD.SearchCaseSensitive = True

		for i = 0 to ubound(sts) step 2
			oRD.SearchString = sts(i)
			oRD.ReplaceString =sts(i+1)
			oDoc.replaceAll(oRD)
		next

		oDoc.store
		oDoc.close(TRUE)

End Sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Save /back up Basic Libraries with macro, rename Librari

Post by JeJe »

Added dialog to extension to edit exported dialogs.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply