Are there AutoCorrect or AutoComplete services?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Are there AutoCorrect or AutoComplete services?

Post by JeJe »

There are services for the Spellchecker, Thesaurus and AutoText. Is there any similar services for the AutoCorrect or AutoComplete entries? Perhaps undocumented?

I've looked long and hard. I'd like to add the functionality of all these to textboxes in dialogue. I have workarounds - I can load the AutoCorrect file and parse it myself - but that's not simple. And create my own code for AutoComplete in textboxes - but it wouldn't be the same list as in the main document.

Any proper access to these please?
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: Are there AutoCorrect or AutoComplete services?

Post by RoryOF »

Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Are there AutoCorrect or AutoComplete services?

Post by JeJe »

Thanks. That gets me the path to the folder with the autocorrect file

oPathSettings = CreateUnoService( "com.sun.star.util.PathSettings" )
msgbox oPathSettings.AutoCorrect

I don't know OO very well. I know I can find the file and write code myself to parse it and so on. What I'm wondering is if there's a service to get/set Autocorrect entries in that file?
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: Are there AutoCorrect or AutoComplete services?

Post by JeJe »

One of my autocorrect entries is "abotu" whic is corrected to "about". Is there a way of querying the word "abotu" to see whether its an autocorrect entry? Without writing code to examine the autocorrect file myself - I know I can do that but its complicated.
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: Are there AutoCorrect or AutoComplete services?

Post by JeJe »

I've thought of creating a hidden docment and entering the word - then a space - but autocorrect isn't invoked then. Just when the space is entered manually.

oCursor = thisComponent.getText.createTextCursor
oText = thisComponent.getText
otext.string ="abotu"
ocursor.collapseToEnd(false)
ocursor.string =" "

Doesn't invoke autocorrect. Sendkeys might work but that's just Windows and anyway this wouldn't be efficient even if it worked.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
echo8hink
Posts: 12
Joined: Wed Mar 02, 2016 7:59 pm

Re: Are there AutoCorrect or AutoComplete services?

Post by echo8hink »

There is a sample database with the LibreOffice 5 Base documentation that has an example of AutoComplete macro in formatted text boxes. I would guess it is similar to OpenOffice. I don't have the link handy, but a quick search of their site should get you there.
LibreOffice 5.4.5.1 on Linux Mint 18.3
John_Ha
Volunteer
Posts: 9583
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: Are there AutoCorrect or AutoComplete services?

Post by John_Ha »

See [Tutorial] Automatic functions in Writer - enable / disable.

AutoHotKey is an extremely powerful tool which recognises text the user types and replaces it with pre-defined text or commands. AutoHotKey typed the above line for me when I typed zztutaut, my shortcut for zz_tut[orial]_aut[omatic functions]. zzsol gets ...

If this solves the problem, please click the Edit button on your original post and add [Solved] in front of your subject.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Are there AutoCorrect or AutoComplete services?

Post by JeJe »

Thanks for the two new replies/tries. All I could find for AutoComplete macros in LibreOffice is...
"LibreOffice has an AutoComplete function which activates itself in some text and list boxes. For example, enter ~/a into the URL field and the AutoComplete function displays the first file or first directory found in your home folder that starts with the letter "a"."
https://help.libreoffice.org/Common/Gen ... ut_Keys_in

What I'll probably have to settle for is

Thesausus/Spelling/find and replace - I can access all those features in code fine.
Autocomplete - do a seperate implementation of that for textboxes. The word list just won't be the same as for documents.
Autotext - instead of trying to use that, do a similar but separate text-clips thing for textboxes
Autocorrect - I can read in the autocorrect list from the text file and use that without trying to make any changes to it.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
echo8hink
Posts: 12
Joined: Wed Mar 02, 2016 7:59 pm

Re: Are there AutoCorrect or AutoComplete services?

Post by echo8hink »

I don't know if "Autotext" is exactly what you are looking for. The example I referred to earlier is for LO5 and is in a base file named: Example_Autotext_Searchmark_Spelling.odb.

It is included in a zip file https://wiki.documentfoundation.org/ima ... abases.zip The code snippet below works with a textbox:

Code: Select all

SUB Autotext(oEvent AS OBJECT)	'Properties: TextBox → Events → Text modified
	DIM arText()
	DIM stText AS STRING
	DIM oSelection AS NEW com.sun.star.awt.Selection
	DIM oField AS OBJECT
	DIM oAutotexts AS OBJECT
	DIM oRange AS OBJECT
	DIM oCursor AS OBJECT
	oField = oEvent.Source
	IF oField.text <> "" THEN
		arText = Split(oField.text)
		stText = arText(Ubound(arText))
		oAutotexts = createunoservice("com.sun.star.text.AutoTextContainer")
		oRange = oAutotexts.getByName("standard") 'template, standard, crdbus50, mytexts - Autotext will be started in Writer by shortcut and F3.
		IF oRange.hasByName(stText) THEN
			oAutotext = oRange.getByName(stText)  'Example: DMS is the shortcut for "Dear Madam or Sir" (Writer → Edit → AutoText)
			oCursor = oField.Model.createTextCursor()
			oCursor.gotoEnd(true)
			oCursor.goLeft(len(stText),true)
			oField.Model.insertString(oCursor,oAutotext.String,true)
			oSelection.Min = len(oField.Text)
			oSelection.Max = len(oField.Text)
			oField.setFocus
			oField.Selection = oSelection
		END IF
	END IF
END SUB
LibreOffice 5.4.5.1 on Linux Mint 18.3
JeJe
Volunteer
Posts: 2763
Joined: Wed Mar 09, 2016 2:40 pm

Re: Are there AutoCorrect or AutoComplete services?

Post by JeJe »

Brilliant thanks... that helps with the Autotext bit. I've used a little of that to write autotext for a dialog textbox which will work on the F3 keypress.

Code: Select all

sub keyPress(oEvt)'call this from dialog textbox keyPress event
	dim sel as new com.sun.star.awt.Selection
	Dim aLocale As New com.sun.star.lang.Locale
	dim a as new com.sun.star.i18n.Boundary
	dim testwd as string,c as object,BI
	dim oAutotexts,oAutotext,AutoTextGroup
	
	
	if oEvt.keycode = 770 then 'f3

		c=oEvt.source
		sel =c.getSelection()
		
		'Set the locale - would do something less ad hoc for a final version.
		aLocale.Language = "en" 'Use the English language
		aLocale.Country = "US" 'Use the United States as the country

		'We could search backwards for a breakchar ourselves but xBreakIterator will get the word boundary for the locale			
		BI = CreateUnoService("com.sun.star.i18n.BreakIterator") 
		a =BI.getwordboundary(c.gettext,sel.max,aLocale,0,false)

		testwd=  mid(c.gettext,a.startPos+1,a.endpos - a.startpos)
		
		oAutotexts = createunoservice("com.sun.star.text.AutoTextContainer")
		
		AutoTextGroup = oAutotexts.getByName("standard") 'template, standard, crdbus50, mytexts - Autotext will be started in Writer by shortcut and F3.
		
		IF AutoTextGroup.hasByName(testwd) THEN
			oAutotext = AutoTextGroup.getByName(testwd)  'Example: DMS is the shortcut for "Dear Madam or Sir" (Writer → Edit → AutoText)
			sel.min = a.startpos
			c.insertText sel,oAutotext.string
		end if

	end if

end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Post Reply