Page 1 of 1

Passing an argument from Filemanager to a Gambas-App

Posted: Thursday 23rd September 2021 12:04pm
by 01McAc
I know the class gb.args and how command line arguments will be passed to a Gambas application. It's probably me but I have no idea how to proceed with this use case:
In a filemanager (i.e. KDE/Dolphin) I want to right-click an image. The context menue appears and I want to start my Gambas fileviewer-app to open and view the selected file. The filemanager opens my coded Gambas-fileviewer-app in the background passing just a single argument (path/filename as string) to the app.
Unfortunately, Gambas arguments require two dashes "--" upfront and that's why

Code: Select all

Application.Args[1-n]
is null in my fileviewer-app. Any idea how to pass a single argument to the app without two dashes?

Re: Passing an argument from Filemanager to a Gambas-App

Posted: Thursday 23rd September 2021 1:40pm
by stevedee
I don't really understand your question or why you are talking about gb.Args, I think you just need the standard Application class.

I would have thought you could just start your Gambas program with something like: myImageViewer filename

...and then read the filename from Application.Args[0] in your code.

Re: Passing an argument from Filemanager to a Gambas-App

Posted: Thursday 23rd September 2021 2:23pm
by 01McAc
sorry for the misunderstanding. I want the filemanager Dolphin to start my app (see attachment). Application.Args[0] shows only the path and name of the app but not the filename I clicked in Dolphin. I would expect the filename L1007019.jpg in Application.Args[1] but it's empty.

Re: Passing an argument from Filemanager to a Gambas-App

Posted: Thursday 23rd September 2021 2:32pm
by cogier
There is no need for gb.args. The basic one works fine for what you are looking for. The 'Arguments' are passed to Gambas as an array. The 1st argument, [0], in the array is the name of your program so what you need is the 2nd argument, [1]. Your file manager will pass the full path to Gambas so, try this code: -
PictureBox1.Picture = Picture[Args[1]]

Re: Passing an argument from Filemanager to a Gambas-App

Posted: Thursday 23rd September 2021 3:29pm
by stevedee
Yes sorry, its argument [1] as Charlie said, not [0] which is always the name of the program.

I modified some code to test it:-
  If Application.Args[1] <> "" Then 
    strThumbNail = LibRAW.ExtractSingleThumbnail(Application.Args[1])
    Me.Text = "Arg found:" & Application.Args[1]
  Else 
    strThumbNail = LibRAW.ExtractSingleThumbnail(RAW_FILE_NAME)
  Endif
  
  pBox.Image = Image.Load(strThumbNail)
  pBox.Stretch = True
I also modified "Open With..." properties in the file manager so it always loads my program if I double click on a RAW file type ...but you may want to chose each time.

Re: Passing an argument from Filemanager to a Gambas-App

Posted: Thursday 23rd September 2021 3:55pm
by 01McAc
Thanks. Seems to be a user problem. This is my original example:

Code: Select all

Public Sub Form_Open()
  Dim myFile As Variant
  
  If IsNull(Application.Args[1]) Then
     Message("No Argument")
     Quit 
  Else
     myFile = Val(Application.Args[1])
     
  Endif '  Application.Args 

  message("arg0: " & Application.Args[0] & gb.NewLine & "arg1: " & myFile & gb.NewLine & "arg2: " & Application.Args[2])
End
I found out that arg[1] is empty because the function val() destroys obviously the string. When I replace

Code: Select all

myFile = Val(Application.Args[1])

with

Code: Select all

myFile = Application.Args[1]
it works! Thanks for your time.