10

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?

2
5

2019 now but stil...

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

enter image description here

Here the script (started via hotkey in the example):

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

With filename: Drags given file to where the mouse is using dragon.
               Click to drop it (anywere).
With 'shot'  : File will be a shot of a an area to be selected.
               => 'drag_into shot' on a hotkey combo makes sense. 
"

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 "$@"

2
  • Thanks, much appreciated in 2020
    – Retr0id
    Sep 21 '20 at 16:37
  • 1
    github.com/mwh/dragon to save someone else searching for the dragon tool (it's not in ubuntu yet)
    – eichin
    Dec 7 '20 at 6:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.