ScriptForge.Basic service /text/sbasic/shared/03/sf_basic.xhp
Basic service

ScriptForge.Basic service

The ScriptForge.Basic service proposes a collection of %PRODUCTNAME Basic methods to be executed in a Python context. Basic service methods reproduce the exact syntax and behaviour of Basic builtin functions.
Typical example: svc.MsgBox('This has to be displayed in a message box') ScriptForge.Basic service is limited to Python scripts.

Service invocation

Before using the Basic service, import the CreateScriptService() method from the scriptforge module: from scriptforge import CreateScriptService svc = CreateScriptService("Basic")

Properties

Name ReadOnly Type Description MB_OK, MB_OKCANCEL, MB_RETRYCANCEL, MB_YESNO, MB_YESNOCANCEL Yes integer Values: 0, 1, 5, 4, 3 MB_ICONEXCLAMATION, MB_ICONINFORMATION, MB_ICONQUESTION, MB_ICONSTOP Yes integer Values: 48, 64, 32, 16
MB_ABORTRETRYIGNORE, MB_DEFBUTTON1, MB_DEFBUTTON2, MB_DEFBUTTON3 Yes integer Values: 2, 128, 256, 512 IDABORT, IDCANCEL, IDIGNORE, IDNO, IDOK, IDRETRY, IDYES Yes integer Values: 3, 2, 5, 7, 1, 4, 6
Constants indicating MsgBox selected button.
StarDesktop Yes UNO
object
StarDesktop object represents LibreOfficeDev Start Center.
List of Methods in the Basic Service ConvertFromUrl
ConvertToUrl
CreateUnoService
DateAdd
DateDiff
DatePart
DateValue
Format
GetDefaultContext
GetGuiType
GetPathSeparator
GetSystemTicks
InputBox
MsgBox
Now
RGB
Xray
ConvertFromUrl ------------------------------------------------------------------------- Basic service;ConvertFromUrl

ConvertFromUrl

Returns a system path file name for the given file: URL.

svc.ConvertFromUrl(url: str): str

url: An absolute file: URL.

A system path file name.

filename = svc.ConvertFromUrl( "file:///C:/Program%20Files%20(x86)/LibreOffice/News.txt") svc.MsgBox(filename)
ConvertToUrl --------------------------------------------------------------------------- Basic service;ConvertToUrl

ConvertToUrl

Returns a file: URL for the given system path.

svc.ConvertToUrl(systempath: str): str

systempath: A system file name as a string.

A file: URL as a string.

url = svc.ConvertToUrl( 'C:\Program Files(x86)\LibreOffice\News.txt') svc.MsgBox(url)
CreateUnoService ----------------------------------------------------------------------- Basic service;CreateUnoService

CreateUnoService

Instantiates a UNO service with the ProcessServiceManager.

svc.CreateUnoService(servicename: str): uno

servicename : A fully qualified service name such as "com.sun.star.ui.dialogs.FilePicker" or 'com.sun.star.sheet.FunctionAccess'.

dsk = svc.CreateUnoService('com.sun.star.frame.Desktop')
DateAdd -------------------------------------------------------------------------------- Basic service;DateAdd

DateAdd

Adds a date or time interval to a given date/time a number of times and returns the resulting date.

svc.DateAdd(interval: str, number: num, date: datetime): datetime

interval: A string expression from the following table, specifying the date or time interval. number: A numerical expression specifying how often the interval value will be added when positive or subtracted when negative. date: A given datetime.datetime value, the interval value will be added number times to this date/time value.

A datetime.datetime value.

dt = datetime.datetime(2004, 1, 31) dt = svc.DateAdd("m", 1, dt) print(dt)
DateDiff ------------------------------------------------------------------------------- Basic service;DateDiff

DateDiff

Returns the number of date or time intervals between two given date/time values.

svc.DateDiff(interval: str, date1: datetime, date2: datetime, firstdayofweek = 1, firstweekofyear = 1): int

interval: A string expression specifying the date interval, as detailed in above DateAdd method. date1, date2: The two datetime.datetime values to be compared.

A number.

date1 = datetime.datetime(2005,1, 1) date2 = datetime.datetime(2005,12,31) diffDays = svc.DateDiff('d', date1, date2) print(diffDays)
DatePart ------------------------------------------------------------------------------- Basic service;DatePart

DatePart

The DatePart function returns a specified part of a date.

svc.DatePart(interval: str, date: datetime, firstdayofweek = 1, firstweekofyear = 1): int

interval: A string expression specifying the date interval, as detailed in above DateAdd method. date: The date/time from which the result is calculated. firstdayofweek, firstweekofyear: optional parameters that respectively specify the starting day of a week and the starting week of a year, as detailed in above DateDiff method.

The extracted part for the given date/time.

print(svc.DatePart("ww", datetime.datetime(2005,12,31) print(svc.DatePart('q', datetime.datetime(1999,12,30)
DateValue ------------------------------------------------------------------------------ Basic service;DateValue

DateValue

Computes a date value from a date string.

svc.DateValue(date: str): datetime

The computed date.

dt = svc.DateValue("23-02-2011") print(dt)
Format --------------------------------------------------------------------------------- Basic service;Format

Format

Converts a number to a string, and then formats it according to the format that you specify.

svc.Format(expression: any, format = ''): str

Formatting Codes

Predefined Formats

txt = svc.Format(6328.2, '##.##0.00') print(txt)
GetDefaultContext ---------------------------------------------------------------------- Basic service;GetDefaultContext

GetDefaultContext

Returns the default context of the process service factory, if existent, else returns a null reference. GetDefaultContext is an alternative to the getComponentContext() method available from XSCRIPTCONTEXT global variable or from uno.py module.

svc.GetDefaultContext(): uno

The default component context is used, when instantiating services via XMultiServiceFactory. See the Professional UNO chapter in the Developer's Guide on api.libreoffice.org for more information.

ctx = svc.GetDefaultContext()
GetGuiType ----------------------------------------------------------------------------- Basic service;GetGuiType

GetGuiType

Returns a numerical value that specifies the graphical user interface. This function is only provided for backward compatibility with previous versions. Refer to system() method from platform Python module to identify the operating system.

svc.GetGuiType(): int

n = svc.GetGuiType()
GetPathSeparator ---------------------------------------------------------------------- Basic service;GetPathSeparator

GetPathSeparator

Returns the operating system-dependent directory separator used to specify file paths. Use os.pathsep from os Python module to identify the path separator.

svc.GetPathSeparator(): str svc.GetPathSeparator(): str

sep = svc.GetPathSeparator()
GetSystemTicks ------------------------------------------------------------------------- Basic service;GetSystemTicks

GetSystemTicks

Returns the number of system ticks provided by the operating system. You can use this function to optimize certain processes. Use this method to estimate time in milliseconds:

svc.GetSystemTicks(): int

ticks_ini = svc.GetSystemTicks() time.sleep(1) ticks_end = svc.GetSystemTicks() svc.MsgBox("{} - {} = {}".format(ticks_end, ticks_ini,ticks_end - ticks_ini))
InputBox ------------------------------------------------------------------------------- Basic service;InputBox

InputBox

svc.InputBox(prompt: str, [title: str], [default: str], [xpostwips: int, ypostwips: int]): str

string

txt = s.InputBox('Please enter a phrase:', "Dear user") s.MsgBox(txt, MB_ICONINFORMATION, "Confirmation of phrase") For in-depth information please refer to Input/Output to Screen with Python on the Wiki.
MsgBox -------------------------------------------------------------------------------- Basic service;MsgBox

MsgBox

Displays a dialog box containing a message and returns an optional value.
MB_xx constants help specify the dialog type, the number and type of buttons to display, plus the icon type. By adding their respective values they form bit patterns, that define the MsgBox dialog appearance.

svc.MsgBox(prompt: str, buttons = svc.MB_OK, [title: str])[: int]

An optional integer as detailed in above IDxx properties.

Now ------------------------------------------------------------------------------------ Basic service;Now

Now

Returns the current system date and time as a date/time value.

svc.Now(): datetime

svc.MsgBox(svc.Now(), svc.MB_OK, "Now")
RGB ------------------------------------------------------------------------------------ Basic service;RGB

RGB

Returns an integer color value consisting of red, green, and blue components.

svc.RGB(red:int, green: int, blue: int): int

integer

YELLOW = svc.RGB(255,255,0)
Xray ----------------------------------------------------------------------------------- Basic service;Xray

Xray

Inspect Uno objects or variables.

svc.Xray(obj: any)

obj: A variable or Uno object.

svc.Xray(svc.StarDesktop)
uno.fileUrlToSystemPath() method uno.systemPathToFileUrl() method Input/Output to Screen with Python on the wiki XSCRIPTCONTEXT.getComponentContext() method uno.getComponentContext() method platform.system() method os.pathsep() method