Delayed Desktop Automation with Bash and xdotool
I had an AI conversation that could not continue until I sent another message. The service had usage limits, so rather than sitting in front of the laptop waiting for the next opportunity, I prepared the next prompt, left the application open, and scheduled a keyboard event for the early hours of the morning.
The message was simply Continue. A small shell script waited for the chosen interval and then sent Enter to the focused window. When I checked the laptop the following morning, the service had produced its response.
There is nothing particularly sophisticated about the mechanism. It combines a timer with a simulated input event. But that small combination is useful: it turns a manual desktop interaction into a scheduled operation, without introducing a browser automation framework, a custom application, or an API integration.
The interesting idea is not automating an AI service; it is treating an ordinary desktop interaction as an event that can be prepared now and triggered later.
The problem: an interaction that should happen later
Many desktop tasks are not difficult; they are inconvenient because they need to happen at a particular time. A form may already be filled in, a message may already be written, or an application may be waiting for a confirmation. The remaining action is a single keystroke or click, but someone has to be there to perform it.
In my case, the next message in an AI conversation was known in advance. I did not need to generate new content at the scheduled time or make a decision then. I only needed to send the prepared message after a delay. That made a simple timer-based approach appropriate.
This is deliberately different from automating the service itself. The script does not inspect the conversation, understand the response, bypass a usage limit, or call a private endpoint. It sends a normal keyboard event through the desktop session. The service still decides whether it can accept and process the message under its own rules and limits.
The small toolchain
The implementation uses two ordinary Linux tools:
• sleep: pauses the script for a specified number of seconds.
• xdotool: sends keyboard and mouse input to an X11 desktop session.
On Arch Linux, install xdotool with:
sudo pacman -S xdotool
Check which display session is active:
echo "$XDG_SESSION_TYPE"
xdotool is primarily an X11 tool. It works naturally in an X11 session, including a typical i3 setup. Native Wayland sessions restrict applications from injecting input into other applications, so xdotool should not be treated as a universal Wayland solution. The available alternatives depend on the compositor and its input-injection permissions.
First, test the input event
Before adding a long delay, test the action on a harmless target such as a text editor. To move the pointer to screen coordinates (500, 300) and left-click:
xdotool mousemove 500 300 click 1
To send Enter to the currently focused window:
xdotool key Return
These are two different kinds of interaction. A mouse click is directed to a screen position. A keyboard event is delivered to whichever window currently has keyboard focus. For the AI conversation experiment, the keyboard event was the relevant one: the prompt was already prepared and the application was left ready to receive Enter.
To inspect the current pointer position:
xdotool getmouselocation
For a script that uses a coordinate, remember that screen coordinates are tied to the current display layout. Resolution changes, monitor rearrangement, scaling, or moving a window can make a previously chosen coordinate point somewhere else.
Build a delayed action
The basic version is almost self-explanatory:
xdotool key Return
Put a delay before it and the action becomes scheduled relative to the moment the script starts:
sleep 16200
xdotool key Return
There are 3,600 seconds in an hour, so 16,200 seconds is four and a half hours. The same pattern works for a mouse action:
sleep 16200
xdotool mousemove 500 300 click 1
For an overnight task, I wanted more than a silent wait. I wanted to see the planned execution time and a countdown, so I could verify the schedule before leaving the machine.
A practical countdown script
The following script moves the pointer to a known position, calculates the expected execution time, and updates a countdown on one terminal line. At the end, it sends Enter. Change X, Y, and DELAY to suit the task.
#!/usr/bin/env bash
X=500
Y=300
DELAY=16200
echo "Moving mouse to ($X, $Y)..."
xdotool mousemove "$X" "$Y"
CLICK_TIME=$(date -d "+$DELAY seconds" "+%Y-%m-%d %H:%M:%S")
echo "Scheduled action: $CLICK_TIME"
echo "Waiting..."
REMAINING=$DELAY
while (( REMAINING > 0 )); do
HOURS=$((REMAINING / 3600))
MINUTES=$(((REMAINING % 3600) / 60))
SECONDS=$((REMAINING % 60))
printf "\rTime remaining: %02d:%02d:%02d " \
"$HOURS" "$MINUTES" "$SECONDS"
sleep 1
((REMAINING--))
done
printf "\rTime remaining: 00:00:00\n"
echo "Sending Enter..."
xdotool key Return
echo "Done at $(date '+%Y-%m-%d %H:%M:%S')."
Save it as night-continue.sh, make it executable, and run it:
chmod +x night-continue.sh
./night-continue.sh
The countdown is intentionally simple. It sleeps for one second on each iteration and decrements a counter. The time spent printing and scheduling each iteration can accumulate into a small drift. For a task where being a few seconds or even a few minutes late is harmless, that is a reasonable trade-off: the code stays easy to read and there is no need for a more elaborate scheduler.
One detail in this example is worth noticing: the script moves the pointer, but the final action is a keyboard event. Moving the pointer does not select a window or guarantee keyboard focus. The application must still be the focused window when the Enter event is sent. If the task is a mouse click instead, replace the final command with xdotool click 1; the pointer must then still be positioned over the intended control.
What must remain true while the script waits?
A delayed desktop action is simple, but it is not independent of the desktop session. The script can wait successfully and still send its event to the wrong place if the environment changes before the deadline.
• The machine must stay awake. If the laptop suspends, the process may stop making progress until it resumes. Configure power management so the machine does not suspend during the scheduled interval.
• The graphical session must remain available. Logging out or terminating the X session can make the later input event fail.
• The intended window must retain focus. Notifications, another application, a dialog, or a user interaction can change the focused window.
• The application must remain ready. A page reload, expired session, connection error, or unexpected dialog can change what Enter means.
• The script must keep running. Closing the terminal or ending its process can cancel the wait unless it was deliberately detached.
For a one-off task, the simplest approach is often to run the script in a terminal and leave the session alone. If it needs to run after the terminal closes, a basic detached launch is possible:
nohup ./night-continue.sh > "$HOME/night-continue.log" 2>&1 &
This preserves the environment inherited by the process, but it does not make GUI automation reliable after logout or guarantee that the display session remains available. Check the log afterward, and do not assume that a background process has successfully completed merely because it was launched.
When a timer is enough, and when it is not
This approach is time-triggered automation: the action is sent after a known delay. It does not know whether the application is ready, whether the previous operation has finished, or whether the expected button is still present.
That distinction matters when deciding whether to expand the script:
| Approach | What it knows | Suitable use |
|---|---|---|
sleep + xdotool | How much time has elapsed | A prepared action that should happen after a fixed delay |
| Window lookup | Whether a matching window exists | Reducing dependence on whichever window happens to be active |
| Visual or application-state checks | Whether an expected state appears to be present | Workflows where timing alone is not sufficient |
| Application API | Application-level operations and responses | Structured automation with explicit success and error handling |
For example, xdotool can search for a window by its title and activate it before sending a key. That can be useful, but window titles are not always stable or unique, and activation behavior depends on the window manager. It is not a substitute for checking that the correct page, prompt, or dialog is ready.
For more complex workflows, a browser automation tool or a supported application API is usually a better foundation. Those tools can inspect state, wait for conditions, and report errors in ways that a blind keypress cannot. The point is not to use xdotool for everything; it is to recognize when a small desktop-level action is all the task requires.
Respect the application's own limits
In the AI example, the script sends a normal message through the visible interface. It does not create additional service capacity or remove the provider's usage restrictions. The service may still reject, defer, or fail to process the message, and automated interaction must remain consistent with the service's terms and usage policies.
That is an important boundary for this technique. Scheduling a legitimate interaction is different from trying to evade a quota, defeat an access control, or generate activity the service does not permit. A timer can decide when to send an input; it cannot guarantee what the remote service will do with it.
Where this technique fits
The same pattern applies beyond AI conversations. A prepared message can be sent later, a desktop application can receive a keyboard shortcut after a long-running local task, or a confirmation can be triggered at a chosen time. In each case, the useful property is that the human decision and preparation happen first, while the mechanical input happens later.
For recurring schedules, a system scheduler such as a systemd user timer may be more appropriate than manually starting a long-running sleep process. But scheduled GUI input still depends on an active graphical session, so moving the timer into systemd does not remove the desktop-state problem. A scheduler improves when the command starts; it does not make the target application ready.
Final thoughts
It is easy to dismiss this as a small Linux trick. The commands are indeed simple, and xdotool is not a new automation framework. What makes the experiment useful is the way a few existing primitives fit together: prepare an application, wait, and deliver an input event at the right moment.
Not every automation problem needs an API, a framework, or a dedicated service; sometimes a shell script and the desktop's existing input interface are enough.
The important engineering judgement is knowing the limits. A delayed keypress is deterministic about the timer, but not about the state of the application receiving it. For a prepared, low-risk action where a little timing drift is acceptable, that may be exactly the right level of automation. When correctness depends on what the application is showing or whether a previous operation has completed, the next step is state-aware automation rather than a longer list of blind keystrokes.
Leave a comment