[Solved] Clicking Close or Cancel on dialogs.FilePicker

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
oonk
Posts: 23
Joined: Fri Oct 04, 2019 8:03 am
Location: Pathum Thani, THAILAND

[Solved] Clicking Close or Cancel on dialogs.FilePicker

Post by oonk »

Clicking 'Open' button without selecting a file, there is no problem for the dialog remains unchanged.
But clicking 'Close' button or 'Cancel' button, there is an error.
What is the way to handle these 2 button?
Attachments
Screenshot_20191102_102203.jpg
Screenshot_20191102_102423.jpg
Last edited by oonk on Sun Nov 03, 2019 2:14 am, edited 1 time in total.
| Fedora 31 Workstation Cinnamon of Fedora from Spins | LibreOffice 6.2.8.2-2 | Base with embedded Firebird |
User avatar
Zizi64
Volunteer
Posts: 11361
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Post by Zizi64 »

Please upload a real ODF type document file (together the embedded macro code) here, instead of the pictures.
Tibor Kovacs, Hungary; LO7.5.8 /Win7-10 x64Prof.
PortableApps/winPenPack: LO3.3.0-7.6.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Post by Villeroy »

Your variable s is declared as string. Declare it as variant and test which type of value is assigned to that variable.
 Edit: getFiles is deprecated. For now it returns an empty array or an array with one string element. 
https://api.libreoffice.org/docs/idl/re ... d93556a60d

P.P.S.
With LO I would get a single selected file like this

Code: Select all

x = oDialog.execute()
if x = 0 then
REM Cancel
else
s = oDialog.getSelectedFiles()(0)
endif
or like this:

Code: Select all

oDialog.execute()
a() = oDialog.getSelectedFiles()
if uBound(a())=-1 then
REM Cancel
elseif uBound(a())=0 then
s = a(0)
else
REM multi-selection
endif
If the code needs to be compatible with AOO, you can use the deprecated getFiles() in the exact same ways as long as LO supports this method and as long as you don't need the result of a multiple file selection. In this case AOO returns one element more than the selected files. First element is the path URL, subsequent elements are the file names without path.
Last edited by Villeroy on Sat Nov 02, 2019 12:06 pm, edited 1 time in total.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
User avatar
Lupp
Volunteer
Posts: 3553
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Post by Lupp »

This works for me in LibO and in AOO 4.1.5 as well:

Code: Select all

Function pickedCsvUrl()
pickedCsvUrl = ""
filePicker = createUnoService("com.sun.star.ui.dialogs.FilePicker")
With filePicker
  .MultiSelectionMode = False
  .AppendFilter("CSV files (.csv)", "*.csv")
  returned = .Execute
  If returned=1 Then
    pickedUrl = .Files(0)
    splitUrl = Split(pickedUrl, ".")
    u = Ubound(splitUrl)
    extension = splitUrl(u)
    If extension="csv" Then pickedCsvUrl = url
   End If
End With
End Function
See also https://xkcd.com/2116/
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Event of clicking Close or Cancel on dialogs.FilePicker

Post by Villeroy »

c.s.s.ui.dialogs.FilePicker supports all the options that are availlable in the complex file picker that appears when you hit Ctrl+O, i.e. extra check boxes and grouped file type filters.

This is a simplified file picker for one file with a simple pattern filter:

Code: Select all

Function pickFile(sTitle$, sInit$, sFilterLabel$, sPattern$)
REM dialog starts at office default directory if sInit = ""
Dim oPicker, x()
	oPicker = CreateUnoService("com.sun.star.ui.dialogs.FilePicker")
	oPicker.setTitle(sTitle)
	oPicker.setDisplayDirectory(sInit)
	oPicker.setMultiSelectionMode(False)
	oPicker.appendFilter(sFilterLabel, sPattern)
	if oPicker.execute()<>0 then
		x = oPicker.getFiles()
		pickFile = x(0)
	endif
End Function
Takes the window title, the start path, the displayed filter name and one or more file name patterns separated by semicolon.
It is compatible with both office suites as long as LibreOffice supports getFiles(). With LO only you should use the new method getSelectedFiles().

Example call:

Code: Select all

f = pickFile("TEST", "", "Blah Text", "*.txt;*.csv"
Window title: TEST
If the init path is "", the picker starts in the default document directory.
Name of file type: "Blah Text"
Displayed files: *.txt and *.csv
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply