From 35a43adc47edcaeecaae1db790edefb3f181d948 Mon Sep 17 00:00:00 2001 From: flywire Date: Tue, 30 Nov 2021 04:50:48 +0200 Subject: Consistent ScriptForge Examples Variable Names - ui for UI service - bas for Basic service Change-Id: I0eb4fbdd021db1149985bb37ba8b29a57297a6eb Reviewed-on: https://gerrit.libreoffice.org/c/help/+/126092 Tested-by: Jenkins Reviewed-by: Rafael Lima (cherry picked from commit 4156bb3b6cfcecb9ccce1ef5c7656d67df93b5c9) Reviewed-on: https://gerrit.libreoffice.org/c/help/+/126934 Reviewed-by: Olivier Hallot --- source/text/sbasic/shared/03/sf_basic.xhp | 76 ++++++++++++------------- source/text/sbasic/shared/03/sf_calc.xhp | 28 ++++----- source/text/sbasic/shared/03/sf_database.xhp | 20 +++---- source/text/sbasic/shared/03/sf_form.xhp | 10 ++-- source/text/sbasic/shared/03/sf_formcontrol.xhp | 15 +++-- source/text/sbasic/shared/03/sf_ui.xhp | 44 +++++++------- source/text/sbasic/shared/03/sf_writer.xhp | 6 +- 7 files changed, 97 insertions(+), 102 deletions(-) diff --git a/source/text/sbasic/shared/03/sf_basic.xhp b/source/text/sbasic/shared/03/sf_basic.xhp index 01acfac5ca..a5d6352022 100644 --- a/source/text/sbasic/shared/03/sf_basic.xhp +++ b/source/text/sbasic/shared/03/sf_basic.xhp @@ -24,18 +24,17 @@

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') + bas.MsgBox('Display this text in a message box from a Python script') 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") + bas = CreateScriptService("Basic")

Properties

@@ -177,9 +176,9 @@ - d = svc.CDate(1000.25) - svc.MsgBox(str(d)) # 1902-09-26 06:00:00 - svc.MsgBox(d.year) # 1902 + d = bas.CDate(1000.25) + bas.MsgBox(str(d)) # 1902-09-26 06:00:00 + bas.MsgBox(d.year) # 1902 @@ -204,8 +203,8 @@ uno_date.Year = 1983uno_date.Month = 2uno_date.Day = 23 - new_date = svc.CDateFromUnoDateTime(uno_date) - svc.MsgBox(str(new_date)) # 1983-02-23 00:00:00 + new_date = bas.CDateFromUnoDateTime(uno_date) + bas.MsgBox(str(new_date)) # 1983-02-23 00:00:00 @@ -226,8 +225,8 @@ from datetime import datetime current_datetime = datetime.now() - uno_date = svc.CDateToUnoDateTime(current_datetime) - svc.MsgBox(str(uno_date.Year) + "-" + str(uno_date.Month) + "-" + str(uno_date.Day)) + uno_date = bas.CDateToUnoDateTime(current_datetime) + bas.MsgBox(str(uno_date.Year) + "-" + str(uno_date.Month) + "-" + str(uno_date.Day)) @@ -246,8 +245,8 @@ A system path file name. - filename = svc.ConvertFromUrl( "file:///C:/Program%20Files%20(x86)/LibreOffice/News.txt") - svc.MsgBox(filename) + filename = bas.ConvertFromUrl( "file:///C:/Program%20Files%20(x86)/LibreOffice/News.txt") + bas.MsgBox(filename) @@ -266,8 +265,8 @@ A file: URL as a string. - url = svc.ConvertToUrl( 'C:\Program Files(x86)\LibreOffice\News.txt') - svc.MsgBox(url) + url = bas.ConvertToUrl( 'C:\Program Files(x86)\LibreOffice\News.txt') + bas.MsgBox(url) @@ -284,7 +283,7 @@ 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') + dsk = bas.CreateUnoService('com.sun.star.frame.Desktop') @@ -307,7 +306,7 @@ dt = datetime.datetime(2004, 1, 31) - dt = svc.DateAdd("m", 1, dt) + dt = bas.DateAdd("m", 1, dt) print(dt) @@ -331,7 +330,7 @@ date1 = datetime.datetime(2005,1, 1) date2 = datetime.datetime(2005,12,31) - diffDays = svc.DateDiff('d', date1, date2) + diffDays = bas.DateDiff('d', date1, date2) print(diffDays) @@ -353,8 +352,8 @@ 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) + print(bas.DatePart("ww", datetime.datetime(2005,12,31) + print(bas.DatePart('q', datetime.datetime(1999,12,30) @@ -373,7 +372,7 @@ The computed date. - dt = svc.DateValue("23-02-2011") + dt = bas.DateValue("23-02-2011") print(dt) @@ -395,7 +394,7 @@ - txt = svc.Format(6328.2, '##.##0.00') + txt = bas.Format(6328.2, '##.##0.00') print(txt) @@ -414,7 +413,7 @@ 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() + ctx = bas.GetDefaultContext() @@ -430,7 +429,7 @@ svc.GetGuiType(): int - n = svc.GetGuiType() + n = bas.GetGuiType() @@ -444,12 +443,9 @@ Use os.pathsep from os Python module to identify the path separator.svc.GetPathSeparator(): str - - svc.GetPathSeparator(): str - - sep = svc.GetPathSeparator() + sep = bas.GetPathSeparator() @@ -464,10 +460,10 @@ svc.GetSystemTicks(): int - ticks_ini = svc.GetSystemTicks() + ticks_ini = bas.GetSystemTicks() time.sleep(1) - ticks_end = svc.GetSystemTicks() - svc.MsgBox("{} - {} = {}".format(ticks_end, ticks_ini,ticks_end - ticks_ini)) + ticks_end = bas.GetSystemTicks() + bas.MsgBox("{} - {} = {}".format(ticks_end, ticks_ini,ticks_end - ticks_ini)) @@ -488,7 +484,7 @@ The following example loads the Gimmicks Basic library if it has not been loaded yet. - libs = svc.GlobalScope.BasicLibraries() + libs = bas.GlobalScope.BasicLibraries() if not libs.isLibraryLoaded("Gimmicks"): libs.loadLibrary("Gimmicks") @@ -511,9 +507,9 @@ The following example shows a message box with the names of all available dialog libraries. - dlg_libs = svc.GlobalScope.DialogLibraries() + dlg_libs = bas.GlobalScope.DialogLibraries() lib_names = dlg_libs.getElementNames() - svc.MsgBox("\n".join(lib_names)) + bas.MsgBox("\n".join(lib_names)) @@ -547,7 +543,7 @@

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: int], [title: str])[: int] + bas.MsgBox(prompt: str, [buttons: int], [title: str])[: int] @@ -567,7 +563,7 @@ svc.Now(): datetime - svc.MsgBox(svc.Now(), svc.MB_OK, "Now") + bas.MsgBox(bas.Now(), bas.MB_OK, "Now") @@ -586,7 +582,7 @@ Integer - YELLOW = svc.RGB(255,255,0) + YELLOW = bas.RGB(255,255,0) @@ -604,8 +600,8 @@ - comp = svc.ThisComponent - svc.MsgBox("\n".join(comp.getSupportedServiceNames())) + comp = bas.ThisComponent + bas.MsgBox("\n".join(comp.getSupportedServiceNames())) @@ -623,7 +619,7 @@ - db_doc = svc.ThisDatabaseDocument + db_doc = bas.ThisDatabaseDocument table_names = db_doc.DataSource.getTables().getElementNames() bas.MsgBox("\n".join(table_names)) @@ -643,7 +639,7 @@ obj: A variable or UNO object. - svc.Xray(svc.StarDesktop) + bas.Xray(bas.StarDesktop) diff --git a/source/text/sbasic/shared/03/sf_calc.xhp b/source/text/sbasic/shared/03/sf_calc.xhp index 68a20ff429..56b4590229 100644 --- a/source/text/sbasic/shared/03/sf_calc.xhp +++ b/source/text/sbasic/shared/03/sf_calc.xhp @@ -75,11 +75,11 @@ myDoc = CreateScriptService("Calc") - svcUI = CreateScriptService("UI") - myDoc = svcUI.CreateDocument("Calc") + ui = CreateScriptService("UI") + myDoc = ui.CreateDocument("Calc") - myDoc = svcUI.OpenDocument(r"C:\Documents\MyFile.ods") + myDoc = ui.OpenDocument(r"C:\Documents\MyFile.ods") myDoc = CreateScriptService("SFDocuments.Calc", "MyFile.ods") @@ -109,8 +109,8 @@ - docA = svcUI.OpenDocument(r"C:\Documents\FileA.ods", hidden = True, readonly = True) - docB = svcUI.OpenDocument(r"C:\Documents\FileB.ods") + docA = ui.OpenDocument(r"C:\Documents\FileA.ods", hidden = True, readonly = True) + docB = ui.OpenDocument(r"C:\Documents\FileB.ods") docB.CopyToRange(docA.Range("SheetX.D4:F8"), "D2:F6") @@ -616,8 +616,8 @@ - svcUI = CreateScriptService("UI") - myDoc = svcUI.GetDocument(svcUI.ActiveWindow) + ui = CreateScriptService("UI") + myDoc = ui.GetDocument(ui.ActiveWindow) myDoc.Activate("Sheet4") Activating a sheet makes sense only if it is performed on a Calc document. To make sure you have a Calc document at hand you can use the isCalc property of the document object, which returns True if it is a Calc document and False otherwise. @@ -779,8 +779,8 @@ myDoc.CopySheet("SheetX", "SheetY") - docA = svcUI.OpenDocument(r"C:\Documents\FileA.ods", hidden = True, readonly = True) - docB = svcUI.OpenDocument(r"C:\Documents\FileB.ods") + docA = ui.OpenDocument(r"C:\Documents\FileA.ods", hidden = True, readonly = True) + docB = ui.OpenDocument(r"C:\Documents\FileB.ods") docB.CopySheet(docA.Sheet("SheetX"), "SheetY") To copy sheets between open documents, use CopySheet. To copy sheets from documents that are closed, use CopySheetFromFile. @@ -850,7 +850,7 @@ - docSource = svcUI.OpenDocument(r"C:\Documents\SourceFile.ods", hidden = True, readonly = True) + docSource = ui.OpenDocument(r"C:\Documents\SourceFile.ods", hidden = True, readonly = True) docDestination = CreateScriptService("Calc") docDestination.CopyToCell(docSource.Range("Sheet1.C2:C4"), "SheetT.A5") docSource.CloseDocument() @@ -900,8 +900,8 @@ doc.CopyToRange("SheetX.A1:F10", "SheetY.C5:J5") - docA = svcUI.OpenDocument(r"C:\Documents\FileA.ods", hidden = True, readonly = True) - docB = svcUI.OpenDocument(r"C:\Documents\FileB.ods") + docA = ui.OpenDocument(r"C:\Documents\FileA.ods", hidden = True, readonly = True) + docB = ui.OpenDocument(r"C:\Documents\FileB.ods") docB.CopyToRange(docA.Range("SheetX.A1:F10"), "SheetY.C5:J5") @@ -1043,8 +1043,8 @@ - sBasic = CreateScriptService("Basic") - sBasic.MsgBox(myDoc.GetColumnName(3)) + bas = CreateScriptService("Basic") + bas.MsgBox(myDoc.GetColumnName(3)) The maximum number of columns allowed on a Calc sheet is 1024. diff --git a/source/text/sbasic/shared/03/sf_database.xhp b/source/text/sbasic/shared/03/sf_database.xhp index 890b42a3df..f788aa48d3 100644 --- a/source/text/sbasic/shared/03/sf_database.xhp +++ b/source/text/sbasic/shared/03/sf_database.xhp @@ -287,12 +287,12 @@ myDB = CreateScriptService("Database", "/home/user/Databases/Employees.odb") - svc = CreateScriptService("Basic") - svc.MsgBox(myDB.DCount("[ID]", "EmployeeData")) - svc.MsgBox(myDB.DSum("[Salary]", "EmployeeData")) - svc.MsgBox(myDB.DCount("[ID]", "EmployeeData", "[Position] = 'Manager'")) - svc.MsgBox(myDB.DCount("[ID]", "EmployeeData", "[Position] = 'Sales' AND [City] = 'Chicago'")) - svc.MsgBox(myDB.DCount("[ID]", "EmployeeData", "[FirstName] LIKE 'Paul%'")) + bas = CreateScriptService("Basic") + bas.MsgBox(myDB.DCount("[ID]", "EmployeeData")) + bas.MsgBox(myDB.DSum("[Salary]", "EmployeeData")) + bas.MsgBox(myDB.DCount("[ID]", "EmployeeData", "[Position] = 'Manager'")) + bas.MsgBox(myDB.DCount("[ID]", "EmployeeData", "[Position] = 'Sales' AND [City] = 'Chicago'")) + bas.MsgBox(myDB.DCount("[ID]", "EmployeeData", "[FirstName] LIKE 'Paul%'")) @@ -322,10 +322,10 @@ - svc = CreateScriptService("Basic") - svc.MsgBox(myDB.DLookup("[FirstName]", "EmployeeData", criteria = "[LastName] LIKE 'Smith'", orderclause = "[FirstName] DESC")) - svc.MsgBox(myDB.DLookup("[Salary]", "EmployeeData", criteria = "[ID] = '3'")) - svc.MsgBox(myDB.DLookup("[Quantity] * [Value]", "Sales", criteria = "[SaleID] = '5014'")) + bas = CreateScriptService("Basic") + bas.MsgBox(myDB.DLookup("[FirstName]", "EmployeeData", criteria = "[LastName] LIKE 'Smith'", orderclause = "[FirstName] DESC")) + bas.MsgBox(myDB.DLookup("[Salary]", "EmployeeData", criteria = "[ID] = '3'")) + bas.MsgBox(myDB.DLookup("[Quantity] * [Value]", "Sales", criteria = "[SaleID] = '5014'")) diff --git a/source/text/sbasic/shared/03/sf_form.xhp b/source/text/sbasic/shared/03/sf_form.xhp index 9d0a88eacc..f797f3bf01 100644 --- a/source/text/sbasic/shared/03/sf_form.xhp +++ b/source/text/sbasic/shared/03/sf_form.xhp @@ -73,8 +73,8 @@ from scriptforge import CreateScriptService - svc = CreateScriptService('UI') - doc = svc.OpenDocument('/home/user/Documents/MyForm.odt') + ui = CreateScriptService('UI') + doc = ui.OpenDocument('/home/user/Documents/MyForm.odt') my_form = doc.Forms('Form1') Forms can be accessed by their names or by their indices, as shown below: @@ -96,8 +96,8 @@ This is achieved identically using Python: - svc = CreateScriptService('UI') - doc = svc.OpenDocument('/home/user/Documents/MyForms.ods') + ui = CreateScriptService('UI') + doc = ui.OpenDocument('/home/user/Documents/MyForms.ods') my_form = doc.Forms('Sheet1', 'Form1')

In Base documents

@@ -784,7 +784,7 @@

Requery

Reloads the current data from the database and refreshes the form. The cursor is positioned on the first record. Returns True if successful. - svc.Requery(): bool + svc.Requery(): bool myForm.Requery() ' Basic diff --git a/source/text/sbasic/shared/03/sf_formcontrol.xhp b/source/text/sbasic/shared/03/sf_formcontrol.xhp index 2fcca6b292..19a79df9fb 100644 --- a/source/text/sbasic/shared/03/sf_formcontrol.xhp +++ b/source/text/sbasic/shared/03/sf_formcontrol.xhp @@ -41,7 +41,6 @@ All controls have a Value property. However, its contents will vary according to the control type. For more information, read The Value Property below. It is also possible to format the controls via the XControlModel and XControlView properties. The SFDocuments.FormControl service is closely related to the SFDocuments.Form service. - API;awt.XControl API;awt.XControlModel @@ -1105,8 +1104,8 @@ - ui = CreateScriptService('ScriptForge.Basic') - doc = CreateScriptService('SFDocuments.Document', ui.ThisComponent) + bas = CreateScriptService('ScriptForge.Basic') + doc = CreateScriptService('SFDocuments.Document', bas.ThisComponent) form = doc.Forms(0) control = form.Controls('thisControl') # SFDocuments.FormControl control.SetFocus() @@ -1126,8 +1125,8 @@ - ui = CreateScriptService('ScriptForge.Basic') # Basic-like methods - doc = CreateScriptService('SFDocuments.Document', ui.ThisDatabaseDocument) + bas = CreateScriptService('ScriptForge.Basic') # Basic-like methods + doc = CreateScriptService('SFDocuments.Document', bas.ThisDatabaseDocument) form = doc.Forms('formDocumentName', 'formName') lbl_city = form.Controls('labelCity') combo_city = form.Controls('comboboxCity') @@ -1150,14 +1149,14 @@ - ui = CreateScriptService('ScriptForge.Basic') # Basic-like methods - doc = CreateScriptService('SFDocuments.Document', ui.ThisDatabaseDocument) + bas = CreateScriptService('ScriptForge.Basic') # Basic-like methods + doc = CreateScriptService('SFDocuments.Document', bas.ThisDatabaseDocument) form = doc.Forms('formDocumentName', 'formName') radio_buttons = ['optA', 'optB', 'optC'] for name in radio_buttons: control = form.controls(name) if control.Value == True: - ui.MsgBox('Selected option: ' + control.Caption) + bas.MsgBox('Selected option: ' + control.Caption) break diff --git a/source/text/sbasic/shared/03/sf_ui.xhp b/source/text/sbasic/shared/03/sf_ui.xhp index b3c1443f8e..03593c7f57 100644 --- a/source/text/sbasic/shared/03/sf_ui.xhp +++ b/source/text/sbasic/shared/03/sf_ui.xhp @@ -87,7 +87,7 @@ from scriptforge import CreateScriptService - svcUI = CreateScriptService("UI") + ui = CreateScriptService("UI")

Properties

@@ -200,11 +200,11 @@ - svcUI = CreateScriptService("UI") - sBasic = CreateScriptService("Basic") - openDocs = svcUI.Documents() + ui = CreateScriptService("UI") + bas = CreateScriptService("Basic") + openDocs = ui.Documents() strDocs = "\n".join(openDocs) - sBasic.MsgBox(strDocs) + bas.MsgBox(strDocs)
@@ -254,7 +254,7 @@ - svcUI.Activate(r"C:\Documents\My file.odt") + ui.Activate(r"C:\Documents\My file.odt") @@ -284,8 +284,8 @@ - myBase = svcUI.CreateBaseDocument(r"C:\Databases\MyBaseFile.odb", "FIREBIRD") - myCalcBase = svcUI.CreateBaseDocument(r"C:\Databases\MyCalcBaseFile.odb", \ + myBase = ui.CreateBaseDocument(r"C:\Databases\MyBaseFile.odb", "FIREBIRD") + myCalcBase = ui.CreateBaseDocument(r"C:\Databases\MyCalcBaseFile.odb", \ "CALC", calcfilename = r"C:\Databases\MyCalcFile.ods") @@ -316,9 +316,9 @@ - myDoc1 = svcUI.CreateDocument("Calc") + myDoc1 = ui.CreateDocument("Calc") FSO = CreateScriptService("FileSystem") - myDoc2 = svcUI.CreateDocument(templatefile = FSO.BuildPath(FSO.TemplatesFolder, "personal/CV.ott")) + myDoc2 = ui.CreateDocument(templatefile = FSO.BuildPath(FSO.TemplatesFolder, "personal/CV.ott")) @@ -344,10 +344,10 @@ - myDoc = svcUI.GetDocument(r"C:\Documents\My file.odt") + myDoc = ui.GetDocument(r"C:\Documents\My file.odt") from scriptforge import CreateScriptService bridge = CreateScriptService("Basic") - myDoc = svcui.GetDocument(bridge.ThisComponent) + myDoc = ui.GetDocument(bridge.ThisComponent) To access the name of the currently active window, refer to the ActiveWindow property. @@ -372,7 +372,7 @@ - svcUI.Maximize("Untitled 1") + ui.Maximize("Untitled 1") @@ -396,7 +396,7 @@ - svcUI.Minimize() + ui.Minimize() @@ -423,7 +423,7 @@ - svcUI.OpenBaseDocument(r"C:\Documents\myDB.odb", macroexecution = svcUI.MACROEXECALWAYS) + ui.OpenBaseDocument(r"C:\Documents\myDB.odb", macroexecution = ui.MACROEXECALWAYS) To improve code readability you can use predefined constants for the macroexecution argument, as in the examples above. @@ -455,7 +455,7 @@ - svcUI.OpenDocument(r"C:\Documents\myFile.odt", readonly = True) + ui.OpenDocument(r"C:\Documents\myFile.odt", readonly = True) @@ -481,7 +481,7 @@ - svcUI.Resize(width = 500, height = 500) + ui.Resize(width = 500, height = 500) To resize a window that is not active, first activate it using the Activate method. @@ -515,9 +515,9 @@ from time import sleep for i in range(101): - svcUI.SetStatusbar("Test:", i) + ui.SetStatusbar("Test:", i) sleep(0.05) - svcUI.SetStatusbar() + ui.SetStatusbar() @@ -551,10 +551,10 @@ from time import sleep for i in range(101): - svcUI.ShowProgressBar("Window Title", "Progress ... " + str(i) + "/100", i) + ui.ShowProgressBar("Window Title", "Progress ... " + str(i) + "/100", i) sleep(0.05) # Closes the Progress Bar window - svcUI.ShowProgressBar() + ui.ShowProgressBar() @@ -579,7 +579,7 @@ - if svcUI.WindowExists(r"C:\Document\My file.odt"): + if ui.WindowExists(r"C:\Document\My file.odt"): # ... diff --git a/source/text/sbasic/shared/03/sf_writer.xhp b/source/text/sbasic/shared/03/sf_writer.xhp index 5b61087f6b..0f7804132d 100644 --- a/source/text/sbasic/shared/03/sf_writer.xhp +++ b/source/text/sbasic/shared/03/sf_writer.xhp @@ -67,11 +67,11 @@ myDoc = CreateScriptService("Writer") ' Default = ActiveWindow - svcUI = CreateScriptService("UI") - myDoc = svcUI.CreateDocument("Writer") + ui = CreateScriptService("UI") + myDoc = ui.CreateDocument("Writer") - myDoc = svcUI.OpenDocument(r"C:\Documents\MyFile.odt") + myDoc = ui.OpenDocument(r"C:\Documents\MyFile.odt") myDoc = CreateScriptService("SFDocuments.Writer", "MyFile.odt") -- cgit