kdialog-open-files.lua and zenity-open-files.lua are two projects that use KDE KDialog and Gnome's zenity respectively to open files in mpv. I wish to do the same using fdfind and fzf.
As illustrated in this stackoverflow answer the following works and will be our starting point:
fdfind . /path/to/Music | fzf -m | xargs -d "\n" mpv
We can use the above command to select some files and play them using mpv. However we want a dialog to be created for our file selection. To do this, we create a bash script menu_fzf.sh with the following contents:
#!/bin/bash
urxvt -name fzf_menu -e bash -c "fzf -m $* < /proc/$$/fd/0 > /proc/$$/fd/1"
(urxvt is only an example and any other suitable terminal emulator can be used in the above).
Now if we run:
fdfind . /path/to/Music | menu_fzf.sh
then a new terminal opens that lets you pick multiple files from /path/to/Music with fzf and will return the selection as output to the calling terminal. So in fact right now we could run the following
fdfind . /path/to/Music | menu_fzf.sh | xargs -d "\n" mpv
and it will play the selected files in mpv. So far so good!
The tricky part now is how to do all the above using lua scripts from within mpv. By looking at the kdialog-open-files.lua or zenity-open-files.lua it seems I need to write something like this:
utils = require 'mp.utils'
-- TODO: make the following work so that file_select correctly stores
-- the files selected using menu_fzf.sh:
file_select = utils.subprocess({
args = {'command', 'argument1', 'argument2', 'and so on'},
cancellable = false,
})
-- and then the rest of the code from kdialog-open-files.lua
However I am finding the official manual for utils.subprocess to be terse and inadequate. I have creatively tried picking command, argument1, argument2, and so on in the above, but nothing seems to work!
So I have two questions:
- Can you (please) complete the above code template into a working solution?
- Do you know where I can find more information on
utils.subprocess? Is there a more detailed manual somewhere? Where is the source-code?
Remark: As I have never programmed in lua before I am struggling with solving question-1 above and will probably continue to struggle even if I had the source code for utils.subprocess. So really help with question-1 is of higher priority. But any help with question-2 is also great!
My failed attempts so far:
The following will append all files in current directory to playlist:
file_select = utils.subprocess({
args = {'fdfind', '.', directory},
cancellable = false,
})
The following fails:
file_select = utils.subprocess({
args = {'fdfind', '.', directory, '|', 'fzf'},
cancellable = false,
})
with error message:
[open_files] [fd error]: Search path '|' is not a directory.
[open_files] [fd error]: Search path 'fzf' is not a directory.
The following allows selecting multiple files from $HOME directory but (as expected) does not return the selection:
file_select = utils.subprocess({
args = {'urxvt','-e','fzf', '-m'},
cancellable = false,
})
The following doesn't work:
file_select = utils.subprocess({
args = {'urxvt','-e','fzf', '-m', '>', '/proc/$$/fd/1'},
cancellable = false,
})
The following opens up a new dialog with fzf but has nothing to select from:
file_select = utils.subprocess({
args = {'menu_fzf.sh'},
cancellable = false,
})