Page 1 of 1
Cross-platform "show file in folder" ?
Posted: Fri Oct 30, 2020 4:43 am
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
Re: Cross-platform "show file in folder" ?
Posted: Fri Oct 30, 2020 4:46 pm
by mk-soft
macOS is Ok
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
Re: Cross-platform "show file in folder" ?
Posted: Fri Oct 30, 2020 7:42 pm
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
Re: Cross-platform "show file in folder" ?
Posted: Sat Oct 31, 2020 3:33 am
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
Re: Cross-platform "show file in folder" ?
Posted: Sat Oct 31, 2020 11:57 am
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.
Re: Cross-platform "show file in folder" ?
Posted: Sat Oct 31, 2020 1:34 pm
by kenmo
I didn't think about / see that... that's 90% of the functionality right there
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?)
Re: Cross-platform "show file in folder" ?
Posted: Sat Oct 31, 2020 1:50 pm
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.
Re: Cross-platform "show file in folder" ?
Posted: Sat Oct 31, 2020 8:23 pm
by kenmo
Got it, thanks for the explanation.