Search code examples
bashubuntudrag-and-dropexe

Create executable bash script that accepts drag & drop


I compiled mplayer from source on Ubuntu. I didn't want to use a GUI but I wanted to make a executable bash file that gets the path from an file that gets dropped onto the bash file. How do I make such a thing possible?

I wanted to make it look something like this: mplayer <get full path to file.file-ending>

I want the executable bash file to sit on my desktop ;)


If possible, I'd just like an rightclick -> start with mplayer function, but I don't know how to make one.


Solution

  • 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