Create executable bash script that accepts drag & drop

前端 未结 3 392
囚心锁ツ
囚心锁ツ 2021-01-04 18: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

相关标签:
3条回答
  • 2021-01-04 19:04

    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.

    0 讨论(0)
  • 2021-01-04 19:06

    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
    
    0 讨论(0)
  • 2021-01-04 19:08

    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
    0 讨论(0)
提交回复
热议问题