Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 646
HitomiTenshi Avatar asked May 01 '12 03:05

HitomiTenshi


2 Answers

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
like image 154
PhoneixS Avatar answered Nov 18 '22 23:11

PhoneixS


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.

like image 3
David Shih Avatar answered Nov 18 '22 22:11

David Shih