Associate a file with an application made on Lazarus on Linux

Asked

Viewed 752 times

4

I developed an application on Lazarus on the linux platform for printing reports, now I need to click on the file with any extension, ex arquivo.gsa , open my application Lazarus on linux. In windows, it was not so complicated I used this code:

function RegisterLink(Ext, FType, FriendlyName, Cmd: PChar): Boolean;
//
// Tenta associar um tipo de arquivo a um aplicativo
//
// Ext: Extensão a ser registrada.
// FType: Categoria do arquivo
// FriendlyName: Tipo de arquivo
// Cmd: linha de comando para abrí-lo
// RegisterLink('.Bsd','ArqInutil','Arquivo inútil','C:\Windows\Notepad.exe "%1"')
//
var
Key: HKey;
SZEntry: Array[0..255] of Char;
SZSize: LongInt;
begin
Result := True;
if RegOpenKey(HKEY_CLASSES_ROOT,Ext,Key) = ERROR_SUCCESS then
    begin
   SZSize := SizeOf(SZEntry);
   RegQueryValue(Key,'',SZEntry,SZSize);
   StrCat(SZEntry,'\Shell\Open\Command');
   if RegOpenKey(HKEY_CLASSES_ROOT,SZEntry,Key) = ERROR_SUCCESS then
      begin
      SZSize := SizeOf(SZEntry);
      RegQueryValue(Key,'',SZEntry,SZSize);
      if (StrIComp(SZEntry,Cmd) = 0) then // and (MessageDlg('A extensão "'+StrPas(Ext)+ '" já está associada para '+copy(StrPas(SZEntry),1,22)+#13+'Você deseja substituir a associação atual por esta?', mtConfirmation, [mbYes,mbNo],0) <> IDYES)  then
         begin
         Result := False;
         Exit;
         end;
      end;
   end;
RegCreateKey(HKEY_CLASSES_ROOT,Ext,Key);
RegSetValue(Key,'',REG_SZ,FType,StrLen(FType));
RegCreateKey(HKEY_CLASSES_ROOT,FType,Key);
RegSetValue(Key,'',REG_SZ,FriendlyName,StrLen(FriendlyName));
StrCat(StrCopy(SZEntry,FType),'\Shell\Open\Command');
RegCreateKey(HKEY_CLASSES_ROOT,SZEntry,Key);
RegSetValue(Key,'',REG_SZ,Cmd,StrLen(Cmd));
end;

then I pass this information to the function:

 RegisterLink('.gsa','ArqGestor','Archivos del Informe',pchar(application.exename+' "%1"'))

Now the problem is how to do this in linux?

  • There must be some way to associate a file containing text to open in a Lazarus application. In the name of this file contains some information from the page that will be printed. Even if I use the " open with ", I need to get the file name in the execution to pass the parameters to the printer by Lazarus.

  • Depends it will help me associate my file . gsa containing text and move to my application developed in Lazarus?? I need to click on this file and open my program already passing the text of this file to a Tmemo, explain to me better what this pkge does. Thanks for the help

  • Would you have any tutorial on this? some example? if possible. Thank you

  • thank you, wait

1 answer

2


I believe you can use the xdg-utils to do this in a Linux environment.

First, you will need to register the icon for the type MIME through the command:

xdg-icon-resource install --context mimetypes --size 48 myicon-file-type.png x-application-mytype

You also need to create a configuration file(freedesktop Shared MIME Documentation):

<?xml version="1.0"?>
 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
   <mime-type type="application/x-mytype">  
   <comment>Algum comentario</comment>
   <comment xml:lang="en">Some comment</comment>
   <glob pattern="*.gsa"/>
  </mime-type>
 </mime-info>

Install the configuration file:

xdg-mime install mytype-mime.xml

This makes your file recognized and associated with an icon.

For more information see here and here.


Updating

Make sure the code below works for you.

procedure TMainForm.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  Memo1.Lines.Add(ParamStr(0)); // executavel.exe
  for I := 1 to ParamCount do
    Memo1.Lines.Add(Format('Param %d : %s', [I, ParamStr(I)]));

  Memo1.Lines.Add(GetCommandLine);
end; 
  • This would solve the invocation of my application, now on the internal side of the Lazarus application how would I get the name of this file and the content? to feed a Tmemo and print the text with the title information? Ex: User Click ==> filename.gsa ==> application.executable => feeds Memo ==> Print

  • 1

    Thanks for your attention @DBX8, I will be very grateful for this help.

  • I’ll be performing the tests, Thank you, Soon return confirming if everything went well

Browser other questions tagged

You are not signed in. Login or sign up in order to post.