Change script so that input file gets already open when selected in Dolphin

I have this script (from the internet) to cut media files:

#!/bin/bash

INPUT=$(kdialog --getopenfilename '*.m4a *.ogg *.mp3 *.mp4 *.avi *.aac *.flac *.avi *.mkv *.mp4')

eval $(yad --width=400 --form --field=start --field=end --field=output:SFL "00:00:00" "00:00:00" "${INPUT/%.*}-out.${INPUT##*.}" | awk -F'|' '{printf "START=%s\nEND=%s\nOUTPUT=\"%s\"\n", $1, $2, $3}')
[[ -z $START || -z $END || -z $OUTPUT ]] && exit 1

DIFF=$(($(date +%s --date="$END")-$(date +%s --date="$START")))
OFFSET=""$(($DIFF / 3600)):$(($DIFF / 60 % 60)):$(($DIFF % 60))

$(ffmpeg -ss "$START" -t "$OFFSET" -i "$INPUT" -c copy "$OUTPUT")
(for i in $(seq 0 3 100); do echo "$i"; sleep 0.1; done) | yad --progress --width=400 --auto-close

if [ $? -eq 0 ]; then
kdialog --msgbox "Process completed successfully!"
  else
kdialog --msgbox "SOMETHING WENT WRONG!"
fi

The main part is based on yad, but the first (selection) and last one (finish messages) are using kdialog because I’m in Plasma.

When run, the script is meant to open a file selection dialog. My intention though is to skip the first part by running the script as a Dolphin service menu or from a launcher. Thus, I can simply select the file in Dolphin:

But, run in this way, the script does not select the file, although the --getopenfilename option and arguments allow kdialog to already open the location of the file and restrict the files listed to media files. Although I have already selected the file in Dolphin, kdialog is in this way asking me to select again:

and only after selecting again I get the yad window I would like to see directly:

image

Can I modify the script so that INPUT= sends the file to the yad process directly, without the need for the kdialog selection, so that when I select the file in Dolphin the window in last image is immediately available?

If you’re running the script from a service menu, then I assume it would have something like Exec=[script goes here] %u, which is a placeholder for a single URL (or you could use %f for a single file).

On that basis, you should be able to change the INPUT= line to be INPUT=$1, where the $1 should get the first argument to the script, which should be the filename.

Having said that, I haven’t actually tested the above, so you’ll need to try it out and see if it works.

It works fine!

  • Only, now I realize the part of the script about the progress window is meaningless: the progress starts after the real process is finished ! :slight_smile: