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?


3 Answers 3


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. 


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 () {
    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
    xdotool mousemove --sync -w "$xid" 1 1 mousedown 1 mousemove $X $Y
    notify-send "Click to drop $file..."

main "$@"

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

example on how drop it is used

Found this question while trying to figure out how to solve this, I eventually went the route of developing a solution to fit my needs. I wanted to post it here for anyone in the future that might be interested in the solution i came up with.

You can find the source for drop here. To run it:

$ git clone https://github.com/sillybanaja/drop.git
$ cd drop
$ make

For context I want a simple way to drag-and-drop files from the terminal into windows that force drag-and-drop without having to drag the file or interact with a GUI.

drop is written in C using Xlib, which is a lightweight library widely available on Unix-like systems. drop takes a list of filenames as arguments and allows you to drop them onto any application that supports XDND (X drag-and-drop), without requiring you to drag the files. Yay one less click!

Here's the basic usage: drop filename ..., and here's a example:

$ drop ~/Downloads/*.jpg file.txt /usr/local/bin/*

Also drop allows you to use globbing patterns like "~/Downloads/*.jpg". Once you have pressed enter you can navigate to the destination window and click where you want to drop the files. Press Escape at any time to close the program.


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.

  • Selenium would work when dragging a file to a supported web browser, right? Commented Apr 13, 2022 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
    Commented Apr 15, 2022 at 22:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .