Jump to content

Macros/ScriptForge/UseConsoleQuickwin

From The Document Foundation Wiki


How to use the console

Authored by Jean-Pierre Ledure.

These are short snippets for easy insertion in user's code of powerful functions.

  • The loading of the ScriptForge Basic library is presumed done elsewhere.
  • All used variables are declared explicitly.
  • The concerned code is presented inside a BASIC function and/or a Python def. They may contain alternative syntaxes or options.
  • Copy/paste what is useful to you.

The loading of the ScriptForge Basic library is present for completeness. However it can be done elsewhere. Remember, in Basic, all used variables are declared explicitly. The concerned code is presented inside a Basic Sub or a Python def.

Run that piece of code and consider the result.

Appropiate use of terminal (console).
Use of console (terminal) in ScriptForge.

Code

REM		How to use the console ?
REM		Minimal required version: LibreOffice 7.6
REM 	Used service(s)	Exception, Timer

Sub activateconsole()
Dim exc As Object
Dim timer As Object		'	To measure durations
Dim i As Integer

	GlobalScope.BasicLibraries.loadLibrary("ScriptForge")
	exc = CreateScriptService("Exception")
	timer = CreateScriptService("Timer", True)	'	True = start immediately

	With exc
		'	A non-modal console is great to debug events
		.Console(modal := False)

		For i = 1 To 10
			Wait 1000	'	Milliseconds
			.DebugPrint("i =", i, timer.Duration, Now(), timer)	'	Any variable type may be put as argument
		Next i

		If MsgBox("Do you want to clean the console ?", buttons := MB_YESNO, title := "Console") = IDYES Then
			.ConsoleClear(keep := 0)	'	You may specify a number of lines to be kept. Default = 0
		End If
	End With

	'	Housekeeping
	timer.Terminate()

End Sub
# coding: utf-8
from __future__ import unicode_literals

from scriptforge import CreateScriptService
basic = CreateScriptService('Basic')

###		How to use the console ?
###		Minimal required version: LibreOffice 7.6
### 	Used service(s)	Exception, Timer

def activateconsole():

	import time

	exc = CreateScriptService('Exception')
	timer = CreateScriptService('Timer', True)	#	True = start immediately
	
	#	A non-modal console is great to debug events
	exc.Console(modal = False)
	
	for i in range(1, 11):
		time.sleep(1)	#	Seconds
		exc.DebugPrint('i =', i, timer.Duration, basic.Now(), timer)	#	Any variable type may be put as argument
	
	if basic.MsgBox('Do you want to clean the console ?', buttons = basic.MB_YESNO, title = 'Console') == basic.IDYES:
		exc.ConsoleClear(keep = 0)	#	You may specify a number of lines to be kept. Default = 0
	
	#	Housekeeping
	timer.Terminate()


g_exportedScripts = (activateconsole,)

if __name__ == "__main__":
	activateconsole()

See also