Posts tagged with “dolphin”

KDE Shenanigans: Playing a random video from Dolphin

Dolphin, the KDE file manager, is great, and has grown to become my favorite file manager of all time. It's super customizable, and a joy to use, which is more than I can say for the Windows equivalent. I do a fair amount of file management, so having a good tool for this is important, and when it's extensible like Dolphin, that's when it really starts to shine.

I recently got the idea to make a script that will play a random video file from a given directory tree. Some possible use cases for this is to play a random episode of a TV show, or a random home recording stored on your computer. Making the script itself was fairly straight-forward, but I don't want to open up the terminal to launch my script every time I want to use it, and I have enough keyboard shortcuts for things already (the most important one being Meta+Z, which plays a rimshot sound effect, much to the amusement of everyone I know).

Naturally, I started looking into integrating this into Dolphin. Initially, I wanted to make a custom toolbar button, but it turns out that isn't possible. What you can do however, is make a KDE Service Menu! These live in the context menu that pops up whenever your right-click things. They are really easy to create as well, you just pop a suitable .desktop file in the right directory, make it executable, and presto! You got yourself a context menu item! Let's see how to accomplish this.

Making the script

First of all, let's make the script itself. There are many ways to go about this, and I just went with the most straight-forward way I could think of; recursively reading the files of the current directory, filtering them on extension, and picking a random one out of the list.

playrandomvideo.sh:

#!/bin/bash
shopt -s nullglob nocasematch
matches=$(find . -print | grep -i -E "\.(webm|mkv|ogv|mov|avi|qt|ts|wmv|mp4|m4v|mpg|mp2|mpeg|mpe|mpv|flv)$" --color=none)
IFS=$'\n'
read -r -d '' -a matchedFiles <<< "$matches"
numFiles=${#matchedFiles[@]}

if [[ "$numFiles" -gt "0" ]] ; then
    rand=$((0 + $RANDOM % $numFiles))
    randFile=${matchedFiles[${rand}]}
    xdg-open "$randFile"
    exit 0
else
    kdialog --sorry "No videos found in the current directory tree."
    exit 1
fi

Note that if you use some esoteric video format that is not included in the regex pattern on line 3 of the script, you can just add it. You can also replace the list of file extensions entirely if you want to adapt the script to opening a different type of content; why not live life on the cutting edge and replace it with ppt|pptx|odp, so the next time you have a presentation at work, you won't know what you're presenting until you start it? Way to keep yourself on your toes.

Place it somewhere safe, like /opt/scripts, and make it executable with chmod +x playrandomvideo.sh.

Making the service menu

Prior to doing this, I didn't know how to create service menus, but KDE has great documentation on how to do that.

First, find the location of service menus on your system, and cd into it. Create playrandomvideo.desktop, and make it executable.

$ qtpaths --locate-dirs GenericDataLocation kio/servicemenus
/usr/share/kio/servicemenus
$ cd /usr/share/kio/servicemenus
$ sudo touch playrandomvideo.desktop
$ sudo chmod +x playrandomvideo.desktop

Note that if your path is in your home directory, you do not need to use sudo to touch and chmod the file.

Now open the file in your favourite text editor, and populate it with the following:

playrandomvideo.desktop:

[Desktop Entry]
Type=Service
MimeType=inode/directory;
Actions=playRandomVideoFromHere
X-KDE-Priority=TopLevel

[Desktop Action playRandomVideoFromHere]
Name=Play random video from here
Icon=media-playback-start
Exec=cd "%u"; /opt/scripts/playrandomvideo.sh

Change the contents of the last line to match where you placed the script we made earlier.

The line X-KDE-Priority=TopLevel is optional. If you keep it, the context menu entry will appear at the top level of the context menu, like so:

If you omit the line, the context menu item will live under a submenu named "Actions":

Done!

Now you can right click any folder, or any empty area of the current folder, and click "Play random video from here" to do just that. The video will open in your system default handler for its respective file type (using xdg-open). If no videos are found, you'll be notified via a dialog box.