Set default extension for plain text files?

I like this resource for its tolerance and willingness to help anyone who asks. StackOverflow blocked the question in 20 minutes - it seemed to some snob that the issues of convenient use of LibreOffice were not interesting to the community. Therefore, I duplicate this question here and give one of the solutions. Perhaps some of you colleagues will suggest another way.

Is it possible to change the behavior of LibreOffice so that when I save a non-plain (.odt or .docs) file as plain text via File->Save As..., it always sets the filename extension to .plain instead of .txt, without me having to change the extension manually afterwards?

Why not? Store simple macro to My Macros/Standard:

Sub SaveAsPlainText()
Const FILE_EXTENTION = ".plain"
Dim oStoreDialog As Object
Dim ListAny(0) As Long
Dim args(0) As New com.sun.star.beans.PropertyValue
Dim sFileName As String 
	GlobalScope.BasicLibraries.loadLibrary("Tools")
	ListAny(0) = com.sun.star.ui.dialogs.TemplateDescription.FILESAVE_AUTOEXTENSION_PASSWORD
	oStoreDialog = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
	oStoreDialog.Initialize(ListAny)
	oStoreDialog.AppendFilter("Plain text (" & FILE_EXTENTION & ")", FILE_EXTENTION)
	sFileName = ThisComponent.getURL()
	If sFileName <> "" Then oStoreDialog.SetDefaultName(GetFileNameWithoutExtension(ThisComponent.getURL(),"/"))
	oStoreDialog.setValue(com.sun.star.ui.dialogs.ExtendedFilePickerElementIds.CHECKBOX_AUTOEXTENSION,0, true)
	If oStoreDialog.Execute() <> 1 Then Exit Sub 
	oStoreDialog.dispose()
	args(0).Name = "FilterName"
	args(0).Value = "Text"
	ThisComponent.storeAsURL(oStoreDialog.Files(0),args)
End Sub

Now just go to menu Tools - Customize - Toolbars tab and set this macro as button on your Standard panel

2 Likes

Thank you very much! As a LibreOffice newbie who is completely new to macros, I would not call this solution ‘simple’! One nitpick: your macro saved the plaintext file in my home directory; to save it in the same directory as the original file, I replaced ‘ThisComponent.getURL(),"/"’ with ‘ThisComponent.getLocation()’.

My code is missing the oStoreDialog.SetDisplayDirectory("<directory>") line - you can set this value to your liking. For example, using the FileNameOutOfPath() function

Uncheck “automatic file name extension” and enter the full name with trailing .plain.
Glitch: Next time you should check this option again.