Cross-platform "show file in folder" ?

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Cross-platform "show file in folder" ?

Post by kenmo »

This is a snippet I use to show a file/folder pre-selected in the OS's explorer/finder.

Can anyone:
(1) confirm the MacOS snippet works? I can only test on Windows right now
(2) recommend a way to implement this in Linux? Hopefully it's a simple one-liner like the other two OS

Code: Select all

FileToShow$ = ProgramFilename()

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  RunProgram("explorer.exe", "/select," + #DQUOTE$ + FileToShow$ + #DQUOTE$, "")
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
  RunProgram("open", "-R " + #DQUOTE$ + FileToShow$ + #DQUOTE$, "")
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
  ; ...
CompilerEndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 5389
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Cross-platform "show file in folder" ?

Post by mk-soft »

macOS is Ok :wink:
On ubuntu work this ... Parameter -w for new window

Code: Select all

FileToShow$ = ProgramFilename()

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  RunProgram("explorer.exe", "/select," + #DQUOTE$ + FileToShow$ + #DQUOTE$, "")
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
  RunProgram("open", "-R " + #DQUOTE$ + FileToShow$ + #DQUOTE$, "")
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
  RunProgram("nautilus", "-w " + #DQUOTE$ + FileToShow$ + #DQUOTE$, "")
CompilerEndIf
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Shardik
Addict
Addict
Posts: 1989
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Cross-platform "show file in folder" ?

Post by Shardik »

mk-soft's Linux solution only works with file manager Nautilus. But many Linux distributions utilize other file managers, so mk-soft's solution doesn't work on my Linux Mint 19.3 with Cinnamon where the default file manager is Nemo. I therefore try to find out the name of the default file manager. Unfortunately my method is not fool proof but it should work with more distributions and file managers than mk-soft's approach.

Code: Select all

FileToShow$ = ProgramFilename()

CompilerIf #PB_Compiler_OS = #PB_OS_Windows
  RunProgram("explorer.exe", "/select," + #DQUOTE$ + FileToShow$ + #DQUOTE$, "")
CompilerElseIf #PB_Compiler_OS = #PB_OS_MacOS
  RunProgram("open", "-R " + #DQUOTE$ + FileToShow$ + #DQUOTE$, "")
CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
  ProgramID = RunProgram("xdg-mime", "query default inode/directory", "",
    #PB_Program_Open | #PB_Program_Read)

  If ProgramID = 0
    MessageRequester("Error",
      "Search for default file manager failed!")
  Else
    FileManager$ = ReadProgramString(ProgramID)
    CloseProgram(ProgramID)
    RunProgram("gtk-launch", FileManager$ + " " + FileToShow$, "")
  EndIf
CompilerEndIf
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Cross-platform "show file in folder" ?

Post by kenmo »

Thank you both... I've implemented it as a "Show in folder" feature to the PB IDE, but as I said I've only tested on Windows. Not submitted to the team yet.

If this ends up in the official IDE, it should work on as many Linux systems as possible!


EDIT: Here's the context...
viewtopic.php?f=3&t=75131
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Cross-platform "show file in folder" ?

Post by freak »

This functionality already exists in the IDE (it is used in the project panel):

https://github.com/fantaisie-software/p ... ns.pb#L428
https://github.com/fantaisie-software/p ... ns.pb#L374
https://github.com/fantaisie-software/p ... ns.pb#L161

The Linux version may need to be updated to know about newer window managers and file explorers, but the basics are there.
quidquid Latine dictum sit altum videtur
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Cross-platform "show file in folder" ?

Post by kenmo »

I didn't think about / see that... that's 90% of the functionality right there :lol:

The subtle difference is: Those just open a folder. What I want (and I assume Kiffi) is to jump right to the specific file. Notepad++ does this, and I've seen it elsewhere.

Windows method:
https://stackoverflow.com/questions/136 ... e-selected
Mac method:
https://scriptingosx.com/2017/02/the-ma ... n-command/ (see -R param)

(Also: Is there a reason you use ShellExecute_() on Windows and not RunProgram?)
freak
PureBasic Team
PureBasic Team
Posts: 5929
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Cross-platform "show file in folder" ?

Post by freak »

RunProgram() uses the default verb in ShellExecute. What that does exactly is system dependent. I used the "explore" verb here which makes more sense for opening a directory. I am not just running "explorer.exe" because this way it is up to the user's settings what program should handle this.

Also RunProgram() first tries to launch a new process from the input parameters and then tries ShellExecute after which is just unneeded work here.
quidquid Latine dictum sit altum videtur
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Cross-platform "show file in folder" ?

Post by kenmo »

Got it, thanks for the explanation.
Post Reply