<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
<script:module xmlns:script="http://openoffice.org/2000/script" script:name="Tools" script:language="StarBasic">REM *****  BASIC  *****

&apos; Somes useful tools used in OOoLilyPondMusic

&apos;*********************************************************
&apos; Add a slash if necessary
Function AddSlash( sPath as String) as String
	If Right(sPath,1) = &quot;/&quot; then 
		AddSlash = sPath
	else
		AddSlash = sPath &amp; &quot;/&quot;
	end if
end Function

&apos;Executes the command sCommand in a bash and returnes when finished
&apos;The command must not include any single quote (&apos;)
sub BashCommand(sCommand)
	Shell(&quot;bash -c &apos;&quot; &amp; sCommand &amp; &quot;&apos;&quot;, 1, &quot;&quot;, True)
end sub


&apos;**********************************************************
&apos; Check the existance of the file...
Function CheckFile( sUrl as String, ErrorMsg as String) As Boolean
	&apos; Test the existance of the OOoLilyPond script ...
	if FileExists(sUrl) then
		CheckFile = False
	else
		if ErrorMsg = &quot;OOoLilyPond&quot; then _
				ErrorMsg = &quot;Cannot find &quot; &amp; sUrl &amp; chr(10) &amp; &quot;Check your installation...&quot; 
		Msgbox ErrorMsg
		CheckFile = True
	end if	
End Function

Function GetOSType()
	If GetPathSeparator() = &quot;\&quot; Then
		GetOSType=&quot;Windows&quot;
	Else
		GetOSType=&quot;Unix&quot;
	End If
End Function

sub InspectObject(vObj)
	vObj=ThisComponent()
	MsgBox vObj.getImplementationName
	&apos;MsgBox vObj.dbg_methods &apos;Methods for this object.
	&apos;MsgBox vObj.dbg_supportedInterfaces &apos;Interfaces for by this object.
	&apos;MsgBox vObj.dbg_properties &apos;Properties for this object.
end sub

&apos;***********************************************************
&apos; Import graphic from URL into the clipboard.
&apos; Inspired from OOoForums Danny&apos;s code 
Sub ImportGraphicIntoClipboard(cURL)

&apos;   MsgBox(cURL)

	oDispatcher = createUnoService( &quot;com.sun.star.frame.DispatchHelper&quot; )

	&apos; Import the graphic from URL into a new draw document.
	Dim arg1(0) As New com.sun.star.beans.PropertyValue
	 arg1(0).Name = &quot;Hidden&quot;
	 arg1(0).Value = true
	oDrawDoc = StarDesktop.loadComponentFromURL( cURL, &quot;_blank&quot;, 0, arg1() )
	oDrawDocCtrl = oDrawDoc.getCurrentController()
	
	&apos; Get the shape...
	oDrawPage = oDrawDoc.DrawPages(0)
    oImportShape = oDrawPage(0)
	
	&apos; Get the dimension of the image...
	oShapeSize = oImportShape.Size()
	
	&apos; Strange bug with the eps and emf format... correction of the size
	if sFormat = &quot;eps&quot; then oShapeSize.Width = oShapeSize.Width * 0.99
	if sFormat = &quot;eps&quot; then oShapeSize.Height = oShapeSize.Height * 0.91
	if sFormat = &quot;emf&quot; then oShapeSize.Width = oShapeSize.Width * 1.13
	if sFormat = &quot;emf&quot; then oShapeSize.Height = oShapeSize.Height * 1.1

	&apos; Copy the image to clipboard and close the draw document
	oDrawDocCtrl.select(oImportShape)
	Dim Array()
	oDispatcher.executeDispatch( oDrawDocCtrl.Frame, &quot;.uno:Copy&quot;, &quot;&quot;, 0, Array() )
	oDrawDoc.dispose()
End Sub

&apos;************************************************************
Sub PrintFile(sFile as String)
	if not FileExists(sTmpPath &amp; sFile) then
		Msgbox &quot;Error : the file &quot; &amp; TmpPath &amp; sFile &amp; &quot; doesn&apos;t exist...&quot;
		exit sub
	end if
	iNumber = Freefile
	Open sTmpPath &amp; sFile For Input As iNumber
 	While not eof(iNumber)
  		Line Input #iNumber, sLine
		&apos;if sLine &lt;&gt; &quot;&quot; then 
		sMsg = sMsg &amp; sLine &amp; chr(10)
	wend
	Close #iNumber
	Msgbox  sMsg
End sub

Sub SortStringArray(StringArray As Variant)
	Dim l, u as Integer
	l=LBound(StringArray())
	u=UBound(StringArray())

	Dim i, j As Integer
	Dim sTemp As String

	For i = l To (u - 1)
		For j = (i + 1) To u
			If StringArray(i) &gt; StringArray(j) Then
				sTemp = StringArray(i)
				StringArray(i) = StringArray(j)
				StringArray(j) = sTemp
			End If
		Next j
	Next i
End Sub

&apos; The Function returns the name of a file that does not already exist.
&apos; This prevents unintended overwriting of existing files.
Function TmpFileName(sPrefix , sSuffix As String) As String
	Do
		TmpFileName=sPrefix &amp; Int(Str(Rnd*1e6)) &amp; sSuffix
	Loop While FileExists(TmpFileName)
End Function

&apos; Did not achieve to run lilypond directly with the Shell command and the 
&apos; Output redirected to files.
&apos; I tried: Shell(&quot;cmd /c lilypond &gt;file1 2&gt;file2&quot;)
&apos; But this did not work :-(
&apos; Now I write down the command in a batch file and call it with Shell.
Sub WindowsCommand(sCommand as String)
	Dim sBatFile As String
	Dim iNumber As Integer

	sBatFile=TmpFileName(ConvertFromURL(sTmpPath) &amp; &quot;CommandCallFromOOo_&quot;,&quot;.bat&quot;)

	iNumber = Freefile
	Open sBatFile For Output As #iNumber
	Print #iNumber, sCommand
	Close #iNumber
	Shell(sBatFile,1,&quot;&quot;,True)
	Kill(sBatFile)
End Sub
</script:module>