Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading