First, I store the pid of an active window.

active_window_pid=$(xdotool getactivewindow getwindowpid)

Second, I click on another window, which is actually function below.

mouse_click     4000 1000

Third, I want to reactivate the previously active window, sadly unsuccessfully.

xdotool search --pid $active_window_pid windowactivate

function mouse_click

    function mouse_click {

            # 1. activate window and wait for sync,
            #    we need to do this before each click,
            #    because the user may have clicked on some other window during the 2 second delay
            # 2. move the mouse cursor to the given position and wait for sync
            # 3. click the left mouse button
            # 4. restore the original mouse cursor position and wait for sync
            # 5. wait for 2 seconds

            xdotool         search --name "window name" windowactivate --sync

            xdotool         mousemove --sync $1 $2 \
                            click 1 \
                            mousemove --sync restore \
                            sleep 2


What did I do wrong this time? Any ideas?


There is a bug in xdotool in some versions at least.

bug string:

Can't consume 1 args; are only 0 available. This is a bug.

bug tracker:

bug workaround:
set --name "whatever"

function activate_window_via_pid {

    # for the chrome browser there are multiple processes,
    # therefore we must pick one of them,
    # the last one seems to work

    window_id=$(xdotool search --pid $1 --name "bug workaround" | tail -1)

    xdotool windowactivate --sync $window_id


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.