Create executable bash script that accepts drag & drop

Try:

#!/bin/bash
mplayer "$1"

The file path of the dropped file will be passed to the script file as the 1th command line argument.


In openned terminal

By using mate-terminal, gnome-terminal or konsole, you could use drag'n drop into oppened window.

This will send URL as tipped, with a space added, but without endline.

For this, run mplayer, I wrote this little loop function:

while IFS=$' \t\r\n' read -d '' -p "Wait for URL to play: " -rsn 1 str &&
    [ "$str" ];do
    while IFS= read -d '' -rsn 1 -t .02 char
    do str+="$char"
    done
    if [ "$str" ] ;then
        read -a req <<<"$str"
        echo $req
        mplayer $req
    fi
  done
  • First read will determine if something is comming or else end loop.
  • Second loop with very short timeout will read dropped URL as a single string
  • read -a req will split string to consider only 1st part

You can access arguments passed to the script with $1 (for the first argument). And also you should make a .desktop file so Nautilus (or your desktop manager) know what to do and use %u to pass the dropped path to the script.

For example you can create a file named DropOverMe.desktop:

[Desktop Entry]
Encoding=UTF-8
Name=Drop Over Me
Comment=Execute the script with the file dropped
Exec=gnome-terminal -e "/folder/to/the/script/launchme.sh \"%u\""
Icon=utilities-terminal
Type=Application

I use gnome-terminal as I have Ubuntu on my PC, use your preferred terminal application.

And the script could be something like:

#! /bin/bash

echo "Launched with $1" >> /tmp/history.log