Passing an argument from Filemanager to a Gambas-App

Post your Gambas programming questions here.
Post Reply
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

Passing an argument from Filemanager to a Gambas-App

Post 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?
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

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

Post 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.
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

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

Post 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.
Attachments
args.jpg
args.jpg (215.02 KiB) Viewed 3403 times
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

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

Post 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]]
User avatar
stevedee
Posts: 518
Joined: Monday 20th March 2017 6:06pm

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

Post 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.
01McAc
Posts: 75
Joined: Sunday 24th January 2021 8:25pm

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

Post 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.
Post Reply