Create executable bash script that accepts drag & drop

霸气de小男生 提交于 2019-12-05 22:00:28

问题


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.


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/10393164/create-executable-bash-script-that-accepts-drag-drop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!