How to add a full list of Autocorrect words

  1. faster: change variable filePath in @sokol92 example to your location :slight_smile:
filePath="/home/ubuntu/.config/libreoffice/4/user/uno_packages/cache/uno_packages/lu282754f7as.tmp_/with_acor_N_9_0.oxt/autocorr/acor_ru-RU.dat"
  1. better: get filePath from the information about extension (change NameOfExtension and acor_ru-RU.dat). It is functional also if you reinstalled the extension.
	dim oProvider as object
	oProvider=GetDefaultContext.getByName("/singletons/com.sun.star.deployment.PackageInformationProvider")
	filePath=oProvider.getPackageLocation("NameOfExtension") & "/autocorr/acor_ru-RU.dat"

For sure to see the names of extensions you can use

Sub showNamesOfExtensions
	dim oProvider as object, a, s$
	oProvider=GetDefaultContext.getByName("/singletons/com.sun.star.deployment.PackageInformationProvider")
	for each a in oProvider.ExtensionList
		s=s & a(0) & chr(13) 
	next
	msgbox s
End Sub
3 Likes

Awesome! It is working very well without any problem.

Is it possible to pick the incorrect > correct words from current open document? The format is something like this…

teest: test, tested
nermal: normal, abnormal

The incorrect word that is followed by colon should be replaced by the first one from comma separated list. In python:

mydic = dict()
for i in update.split('\n'):
    first = i.split(':')[0]
    mydic[first] = i.split(':')[1].split(',')[0].strip()

I have saved a lot of words in this format! :slight_smile:

There can be problem if you use Shift+Enter, Tab, multiple spaces etc. in your document, so there is initially Find&Replace for it (array aRepl)!
And set correctly constants cExtension and cAcor at start.

Sub addCurrentList
	REM !!! change this constants !!!
	const cExtension="NameOfExtension"
	const cAcor="acor_ru-RU.dat"
	
	Dim oPathSettings As Object, oPackage As Object, oDom As Object, oNode As Object
	Dim oPackageFolder As Object, oPackageStream As Object, oOutputStream As Object, oTempFile As Object
	Dim filePath As String, DocumentList As String
	dim oProvider as object, oDoc as object, oEnum as object, oPar as object, s$, s1$, s2$, i1&, i2&, oDesc as object, aRepl(), x

	oDoc=ThisComponent
	oProvider=GetDefaultContext.getByName("/singletons/com.sun.star.deployment.PackageInformationProvider")
	filePath=oProvider.getPackageLocation(cExtension) & "/autocorr/" & cAcor

	oPackage=createUnoService("com.sun.star.packages.Package")
	oPackage.initialize Array(filePath)

	DocumentList="DocumentList.xml"
	oPackageStream=oPackage.getByHierarchicalName(DocumentList)
	oDom=createUnoService("com.sun.star.xml.dom.DocumentBuilder").parse(oPackageStream.inputStream)

	rem replace some white characters
	aRepl=array( array("\n", "\n"), array("^$", ""), array("\t", ""), array("\s{2,}", "") )
	oDesc=oDoc.createReplaceDescriptor
	oDesc.SearchRegularExpression=true
	for each x in aRepl
		with oDesc
			.SearchString=x(0)
			.ReplaceString=x(1)
		end with
		oDoc.ReplaceAll(oDesc)
	next

	rem traverse paragraphs
	oEnum=oDoc.Text.createEnumeration()
	while oEnum.hasMoreElements()
		oPar=oEnum.nextElement()
		if oPar.supportsService("com.sun.star.text.Paragraph") then
			s=oPar.String
			if s<>"" then 'paragraph has text
				i1=InStr(s, ":")
				if i1>0 then ' : found
					i2=InStr(i1, s, ",")
					if i2>0 then ' , found
						s1=Trim(Left(s, i1-1))
						s2=Trim(Mid(s, i1+1, i2-i1-1))
						'msgbox s1 & chr(13) & s2
						oNode=oDom.documentElement.appendChild(oDom.createElement("block-list:block"))
						oNode.setAttribute("block-list:abbreviated-name", s1)
						oNode.setAttribute("block-list:name", s2)
					end if
				end if
			end if
		end if
	wend

	oTempFile=createUnoService("com.sun.star.io.TempFile")
	oDom.setOutputStream oTempFile.OutputStream
	oDom.start
	oTempFile.OutputStream.closeOutput

	oPackageFolder=oPackage.getByHierarchicalName("")
	oPackageStream=oPackage.createInstanceWithArguments(Array(False))
	oPackageStream.SetInputStream(oTempFile.InputStream)
	oPackageFolder.replaceByName(DocumentList, oPackageStream)

	oPackage.commitChanges
End Sub

The macro is working as expected. I really appreciate your help. Is it possible to count lines and show “34 entries added” message at the end?

Another issue is that when I tried the similar code to add entries to default English (US), I got an error “read only package”.

    Dim pathsubstitution
    pathsubstitution = createUnoService("com.sun.star.util.PathSubstitution")
    filePath = pathsubstitution.substituteVariables("$(inst)/share/autocorr", True ) & "/acor_en-US.dat"

Of course :slight_smile:

  1. faster: msgbox "34 entries added"
  2. better: with real count :slight_smile:
	dim iCount&
	'...
	if s<>"" then 'paragraph has text
		iCount=iCount+1
		'...
	end if

	msgbox(iCount & " entries added")

Use $(user) instead $(inst) in filePath. And copy acor_en-US.dat to your user profile if there isn’t.

filePath = pathsubstitution.substituteVariables("$(user)/share/autocorr", True ) & "/acor_en-US.dat"