2

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?

2 Answers 2

5

Change your zenity.sh file with:

#!/usr/bin/python

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

To:

#!/bin/bash

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

You are not calling zenity within a python script. You are calling it from a bash/shell command so your shebang (first line) must be #!/bin/bash not #!/usr/bin/python.

2
  • +1 for noticing that shebang.... I missed it :)
    – Raffa
    May 8, 2021 at 21:08
  • Since OP runs sh ./zenity.sh, the shebang makes no difference here, Change it #! /usr/bin/bash and it will still be run with sh.
    – muru
    May 9, 2021 at 9:50
3

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'

You must log in to answer this question.

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