Recent

Author Topic: How to open folder in Platform file explorer?  (Read 580 times)

LeoBruno

  • New Member
  • *
  • Posts: 30
How to open folder in Platform file explorer?
« on: August 04, 2022, 10:54:54 pm »
Hi:

I need to call the platform´s file explorer (ex: windows explorer on Windows) and open some pre-determined folder.

Similar to what can be done by calling WinApi ShellExecute passing the Open Parameter.

My targets are:

Windows, MacOs and Linux.

Is there a cenralized way to do this?

If not, how can this be done on MacOS and Linux?

Thank's

HeavyUser

  • Sr. Member
  • ****
  • Posts: 364
Re: How to open folder in Platform file explorer?
« Reply #1 on: August 04, 2022, 11:31:56 pm »
in linux you call the default file manager in my suse installation that would be dolphin and I call it like this
Code: [Select]
dolphin --select "Filename" where filename is the file you want to be selected in the manager when it opens. use
Code: [Select]
dolphin --help for more info.

Wallaby

  • New Member
  • *
  • Posts: 47
Re: How to open folder in Platform file explorer?
« Reply #2 on: August 05, 2022, 03:37:54 am »
Try OpenDocument from LCLIntf it should work on all platforms.

If you need to open a folder and select a specific file, it would be like this:

Code: Pascal  [Select][+][-]
  1. procedure OpenLocation(const FileName: string);
  2. var
  3.   Process: TProcess;
  4. begin
  5.   Process := TProcess.Create(nil);
  6.   try
  7.     {$IFDEF WINDOWS}
  8.     Process.Executable := 'explorer.exe';
  9.     Process.Parameters.Add('/select,' + FileName.QuotedString('"'));
  10.     {$ELSE}
  11.     //This is for macOS, on Linux it will likely depend on WM.
  12.     Process.Executable := 'open';
  13.     Process.Parameters.Add('-R');
  14.     Process.Parameters.Add(FileName);
  15.     {$ENDIF}
  16.     Process.Execute;
  17.   finally
  18.     Process.Free;
  19.   end;
  20. end;

Ally

  • New Member
  • *
  • Posts: 39
Re: How to open folder in Platform file explorer?
« Reply #3 on: August 05, 2022, 09:20:49 am »
Under Windows also works:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var Dir: String;
  3. begin
  4.   Dir := 'C:\';
  5.   {$IFDEF WINDOWS}
  6.   ExecuteProcess('explorer.exe', Dir, []);
  7.   {$ENDIF}
  8. end;

MarkMLl

  • Hero Member
  • *****
  • Posts: 4679
Re: How to open folder in Platform file explorer?
« Reply #4 on: August 05, 2022, 09:51:20 am »
Try OpenDocument from LCLIntf it should work on all platforms.

If that doesn't work then there isn't an existing cross-platform solution (in the scope of the LCL etc.).

On at least most Linux distreaux, and probably most unix derivatives, something like

Code: Text  [Select][+][-]
  1. $ xdg-open $HOME
  2.  

should work (I've just checked that on Debian).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlexTP

  • Hero Member
  • *****
  • Posts: 1838
    • UVviewsoft
Re: How to open folder in Platform file explorer?
« Reply #5 on: August 05, 2022, 10:10:29 am »
@MarkMLl
OpenDocument handles the 'xdg-open'
Code: Pascal  [Select][+][-]
  1. function OpenDocument(APath: String): Boolean;
  2. ....
  3.   lApp:=FindFilenameOfCmd('xdg-open'); // Portland OSDL/FreeDesktop standard on Linux
  4.   if lApp='' then
  5.     lApp:=FindFilenameOfCmd('kfmclient'); // KDE command
  6.   if lApp='' then
  7.     lApp:=FindFilenameOfCmd('gnome-open'); // GNOME command
  8.   if lApp='' then
  9.     Exit(False);    
   

Now I think that calling of 'dolphin' may be added here?

MarkMLl

  • Hero Member
  • *****
  • Posts: 4679
Re: How to open folder in Platform file explorer?
« Reply #6 on: August 05, 2022, 10:32:30 am »
@MarkMLl
OpenDocument handles the 'xdg-open'

I know, but I was making the specific point that I'd tested it for a directory ("folder") so it wasn't just handling files of preconfigured types.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

LeoBruno

  • New Member
  • *
  • Posts: 30
Re: How to open folder in Platform file explorer?
« Reply #7 on: August 05, 2022, 06:56:22 pm »
Thank's to all.

This is the chosen sollution

Try OpenDocument from LCLIntf it should work on all platforms.

If you need to open a folder and select a specific file, it would be like this:

Code: Pascal  [Select][+][-]
  1. procedure OpenLocation(const FileName: string);
  2. var
  3.   Process: TProcess;
  4. begin
  5.   Process := TProcess.Create(nil);
  6.   try
  7.     {$IFDEF WINDOWS}
  8.     Process.Executable := 'explorer.exe';
  9.     Process.Parameters.Add('/select,' + FileName.QuotedString('"'));
  10.     {$ELSE}
  11.     //This is for macOS, on Linux it will likely depend on WM.
  12.     Process.Executable := 'open';
  13.     Process.Parameters.Add('-R');
  14.     Process.Parameters.Add(FileName);
  15.     {$ENDIF}
  16.     Process.Execute;
  17.   finally
  18.     Process.Free;
  19.   end;
  20. end;

 

TinyPortal © 2005-2018