Custom Open-Script for GNOME File Manager


Published: 2021-01-01
Updated: 2021-01-01
Web: https://fritzthecat-blog.blogspot.com/2021/01/custom-open-script-for-gnome-file.html


The GNOME default file manager is Nautilus. I use it when I want to open several video files at once. Then I select some videos, right-click for the context menu and choose "Open With Videos".

This never was very reliable concerning the sort order of the videos, sometimes the order (as visible in the file manager) was kept, but frequently, especially on first open, the order was messed up. I never found out whether Nautilus or Totem (the default video viewer) was responsible for that problem.

Now in Ubuntu 20.04.1 this became worse. Sometimes only the last selected file was played by Totem, sometimes all files, but in that case the sort order ALWAYS was messed up!

I use this procedure to define video cuts: I create a list of all video files in chronological order, then I open them and and write notes about the seconds to extract into that list. But when the videos are not shown in specified order, I make many mistakes during that process, and that costs time. So I had to fix this. But how?

How to intercept OPEN commands in Nautilus

You need to create a script (shell, Python, ...) in directory $HOME/.local/share/nautilus/scripts/ (create it when not existing):

$HOME
.local
share
nautilus
scripts
Play-Selected-Videos.sh

I named my script Play-Selected-Videos.sh. Don't forget to make it executable:

cd $HOME/.local/share/nautilus/scripts/
chmod 754 Play-Selected-Videos.sh

Any script in this folder will be displayed by Nautilus in its context menu:

So clicking the menu item will call my script with selected files. Unfortunately that menu item is shown for all types of files, not only videos. But I can live with that.

Shell Script that Sorts and Plays Videos

The Play-Selected-Videos.sh script sorts all videos passed by Nautilus, and then plays them one by one. (Passing the sorted list to Totem did not fix the problem!)

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for file in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
do
if [ -z "$list" ]
then
list=$file
else
list="$list
$file"
fi
done

for video in `echo "\$list" | sort`
do
totem $video
done

The environment variable NAUTILUS_SCRIPT_SELECTED_FILE_PATHS is evaluated by Nautilus with a space-separated list of selected files (absolute paths). The script builds a line list from these paths, and then sorts the line list. Finally a loop calls the video player for each video file in the sorted list.

Mind that $file in line 8 must be left aligned to not create spaces in the line list.
Mind further that directories with spaces in their pathes will be a problem, this would require complex string splitting.
Finally mind that "\$list" in line 12 needs to be enclosed in double quotes (") to maintain the line structure.

Conclusion

This is not a perfect solution, because I will not be able to browse backwards in the list of loaded videos. I would have to close all videos still in loop, and then start again. Hopefully this Totem bug will be fixed soon.





ɔ⃝ Fritz Ritzberger, 2021-01-01