13

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

2 Answers 2

6

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, 2020 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, 2020 at 6:56
1

You can use Selenium for this task. It is an open source tool to interact with web browsers. Assuming you use Arch Linux and want to write in python, install python-selenium aur package (or via pip: pip install selenium), then run pip install webdriver-manager (for automatically download needed version of driver that matches your browser version).

Selenium even does not require you to drag the required file, just pass an absolute path to file you want. Of course if you wish, you still can instead just simulate a click to an upload button or even dropping something to it.

You can work with the existing (previously opened) browser instead of opening a new window. In this case however you may need to use my solution for selecting the currently gui active tab to correspond programmatically choosen active tab.

2
  • Selenium would work when dragging a file to a supported web browser, right? Apr 13 at 14:26
  • I am not sure about interactive element selection in random web page. If you figure that out, then it may be possible, see code here. In my case I know exactly where I want to drop the file (the page has a single input element with type file, and I select it), so I automated it. Maybe try to list all input elements and show a dialog if there are several? But probably overkill for your use.
    – Ashark
    Apr 15 at 22:15

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.