Drag-and-drop without the drag

command linedrag and dropfreedesktopx11

I sometimes need to drag-and-drop files onto an application. (Example — but my question here is not installing Chrome userscripts.) One solution is to use Dragbox, which opens a window from which I can drag a file specified on the command line.

It's nice but I'd like to reduce the necessary mouse interaction. With Dragbox, I have to: arrange for both Dragbox and the drop zone to be visible; move the mouse cursor to the location where Dragbox displays the file; press the left mouse button; move the mouse cursor to the drop zone; release the cursor.

I'd like an interface that works more like copy-paste: run a command like dragbox --more-magic foo, then click on the drop zone. Or run the command then focus the drop zone and press a key. Is there a program to do that? Can it even be done with Freedesktop drag-and-drop?

Best Answer

2019 now but stil...

This is my current screenshot tool. The xdotool commands are relevant regarding how to automate a file drag.

2.bin$ cat drag_into
#!/usr/bin/env bash
doc="$0 <filename|'shot'>

Drags a given file to where the mouse is using dragon. Click to drop it (anywere).
If filename is 'shot' then the file will be a shot of a an area to be selected.
"

cmd_shot="shot"
file=

exit_help () { echo -e "$doc"; exit 1; }

select_shot_area () {
    # create screen shot
    notify-send "Select area - we'll shoot it and drag to where the mouse is."
    cd "$HOME/Pictures/shots/" || exit 1
    rm -f "latest.png"
    scrot -s '%Y-%m-%d_$wx$h_scrot.png' -e 'ln -s $f latest.png'
    file="`readlink latest.png`"
}

main () {
    file="$1"
    test -z "$file" -o "$file" == "-h" && exit_help
    eval "$(xdotool getmouselocation --shell)" # into $X and $Y
    test "$file" == "$cmd_shot" && { select_shot_area || return 1; }
    killall dragon 2>/dev/null # No accidential drops of wrong items ...
    dragon --and-exit "$file" &
    while true; do
        xid="$(xdotool search --onlyvisible --class  dragon | head -n 2)"
        test -z "$xid" || break
        sleep 0.05
    done
    xdotool mousemove --sync -w "$xid" 1 1 mousedown 1 mousemove $X $Y
    notify-send "Click to drop $file..."
}

main "$@"

Related Question