How to interact with zenity window and type some text inside it?

Advertisements

I have python code :

import subprocess
subprocess.call(['sh', './zenity.sh'])

and zenity.sh file which is

#!/usr/bin/python

zenity --forms --title="Question" \
   --add-entry="Question" \

Running it opens window with field to type some text.

I want to type some text inside this zenity window using for e.g. xdotool I tried to use

subprocess.call(["xdotool", "type", "some text"])

not working

then I created another .sh

#!/bin/bash


xdotool search --class zenity windowfocus type 'some text'

also not working

Any ideas?

>Solution :

There are two requirements for xdotool to work with your example:

  • You should be running X11 and not Wayland.
  • Your window created by zenity must be fully loaded before the xdotool command is run.

To do this properly, you need to:

  1. Load the window in the background ( with & ) like so:

    zenity --forms --title="Question" --add-entry="Question" &
    
  2. Give some time for the window to fully load ( with sleep ) like so:

    sleep 1
    
  3. Get your window ID by name like so:

    window="$(xdotool search --name 'Question')"
    
  4. Activate your window by ID ( stored in $window in step 3 above ) like so:

    xdotool windowactivate "$window"
    
  5. Type the text in the window like so:

    xdotool type 'some text'
    

So the final script will look like this:

#!/bin/bash

zenity --forms --title="Question" --add-entry="Question" &

sleep 1

window="$(xdotool search --name 'Question')"

xdotool windowactivate "$window"

xdotool type 'some text'

Leave a ReplyCancel reply