LibO/OOo Macro Code Snippets

In questa pagina ho raccolto alcune macro per OpenOffice/LibreOffice/NeoOffice che spero possano essere utili a qualcuno, e che sicuramente sono state utili a me nella realizzazione di Writer2ePub.

[box title=”Informazioni” color=”#b0b0b0″]

Queste macro consentono di ottenere informazioni su diversi parametri di OpenOffice:
[spoiler title=”GetOOoVersion()” open=”0″ style=”2″]

Questa funzione ritorna la versione di OpenOffice/LibreOffice in uso:

Function GetOOoVersion() As String
	'Retrives the running OOO version
	Dim aSettings, aConfigProvider
	Dim aParams2(0) As new com.sun.star.beans.PropertyValue
	Dim sProvider$, sAccess$
	sProvider = "com.sun.star.configuration.ConfigurationProvider"
	sAccess   = "com.sun.star.configuration.ConfigurationAccess"
	aConfigProvider = createUnoService(sProvider)
	aParams2(0).Name = "nodepath"
	aParams2(0).Value = "/org.openoffice.Setup/Product"
	aSettings = aConfigProvider.createInstanceWithArguments(sAccess, aParams2())
	GetOOoVersion=aSettings.getbyname("ooSetupVersion")
End Function[/spoiler]

[spoiler title="GetOsType()" open="0" style="2"]

Questa funzione ritorna il sistema operativo utilizzato in forma di stringa:
"Windows" "Mac" "OSX" "Linux"
Function GetOsType()
	Select Case getGUIType
	case 1
		GetOsType="Windows"
	case 3
		GetOsType="Mac" 'Only for NeoOffice, Libo and OOo doesn't run on old Mac"
	case 4
		GetOsType=iif(instr(environ("PATH"),"openoffice")=0,"OSX","Linux")
	Case Else
		GetOsType="Other"
	End Select
End Function
[/spoiler] [spoiler title="GetDocumentLanguage()" open="0" style="2"] Questa funzione ritorna il codice della lingua utilizzata nel documento.

Function GetDocumentLanguage() 
	Dim OOLang As string	
 	OOLang = ThisComponent.CharLocale.Language
	GetDocumentLanguage() = OOLang	
End Function
[/spoiler] [spoiler title=”GetInterfaceLanguage()” open=”0″ style=”2″]

Questa funzione ritorna il codice della lingua dell’interfaccia.

Function GetInterfaceLanguage() 
	Dim OOLang As string	
 	Dim aSettings, aConfigProvider
 	Dim aParams2(0) As new com.sun.star.beans.PropertyValue
	aParams2(0).Name = "nodepath"
 	aParams2(0).Value = "/org.openoffice.Setup/L10N"
 	aConfigProvider = createUnoService("com.sun.star.configuration.ConfigurationProvider" )
 	aSettings = aConfigProvider.createInstanceWithArguments("com.sun.star.configuration.ConfigurationAccess", aParams2() )
 	OOLang = aSettings.getbyname("ooLocale")
	GetInterfaceLanguage() = OOLang
End Function
[/spoiler] [spoiler title=”GetExtensionLocation()” open=”0″ style=”2″]

Se avete creato un’estensione, potrebbe tornarvi utile sapere dove si trova la vostra estensione, per far riferimento ad immagini, icone o altri oggetti che avete incorporato nell’estensione stessa. Questa funzione ritorna il percorso a cui si trova la vostra estensione. Al posto di “vnd.myextension” va ovviamente inserito l’identificatore univoco della vostra estensione:

Function GetExtensionLocation()
On Error Resume Next
' this routine returns the path of your extension, useful to embed and access files 	
	Dim pip As Object
	Const ExtensionIdentifier as String = "vnd.myextension" 'your extension unique identifier
	pip = GetDefaultContext.getByName("/singletons/com.sun.star.deployment.PackageInformationProvider")
	GetExtensionLocation = pip.getPackageLocation(extensionIdentifier)
End Function
[/spoiler] [/box] [box title=”Altre Macro” color=”#b0b0b0″] [spoiler title=”UUIDv4()” open=”0″ style=”2″]

Un UUID è un codice identificativo univoco che viene generato casualmente. Vista la notevole lunghezza di questo codice, la probabilità che vengano generati due codici identici è di 338

'______________________________________________________________________________
 '
 ' Version 4 UUID generator by Luca Calcinai
 '
 ' This library is free software; you can redistribute it and/or
 ' modify it under the terms of the GNU Lesser General Public
 ' License As published by the Free Software Foundation.
 '
 ' This library is distributed in the hope that it will be useful,
 ' but WITHOUT ANY WARRANTY; without even the implied warranty of
 ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 ' Lesser General Public License for more details.
 '
 ' Please email me with any bug reports, questions or comments:
 ' writer2epub@gmail.com
 '______________________________________________________________________________
 '
 ' (definition from http://en.wikipedia.org/wiki/UUID)
 ' Version 4 UUIDs use a scheme relying only on random numbers.
 ' This algorithm sets the version number As well As two reserved bits.
 ' All other bits are set using a random or pseudorandom data source.
 ' Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
 ' with hexadecimal digits x and hexadecimal digits 8, 9, A, or B for y.
 ' e.g. f47ac10b-58cc-4372-a567-0e02b2c3d479.
 '
 '______________________________________________________________________________
 '
 '
 ' Declarations:
 Option Explicit
Sub UUIDtest
	msgbox "this is a random generated UUID:" & chr(13) & UUIDv4()
End Sub
Function UUIDv4() As String
	Dim i As integer	
	UUIDv4 = ""
	for i=1 to 8
		UUIDv4 = UUIDv4 & hex(int(rnd()*16))
	next i
	UUIDv4 = UUIDv4 & "-"
	for i=1 to 4
		UUIDv4 = UUIDv4 & hex(int(rnd()*16))
	next i
	UUIDv4 = UUIDv4 & "-4"
	for i=1 to 3
		UUIDv4 = UUIDv4 & hex(int(rnd()*16))
	next i
	UUIDv4 = UUIDv4 & "-"
	UUIDv4 = UUIDv4 & hex((int(rnd()*4))+8)
	for i=1 to 3
		UUIDv4 = UUIDv4 & hex(int(rnd()*16))
	next i
	UUIDv4 = UUIDv4 & "-"
	for i=1 to 12
		UUIDv4 = UUIDv4 & hex(int(rnd()*16))
	next i
End Function
[/spoiler] [/box]

Lascia un commento