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?

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'
5

Change your zenity.sh file with:

#!/usr/bin/python

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

To:

#!/usr/bin/bash

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

zenity is not a python command, it is a bash/shell command so your shebang (first line) must be #!/usr/bin/bash not #!/usr/bin/python.

4
  • +1 for noticing that shebang.... I missed it :) – Raffa May 8 at 21:08
  • @Raffa An upvote from a keener like you is like 10 upvotes from mere mortals :) – WinEunuuchs2Unix May 8 at 21:35
  • 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 at 9:50
  • @muru Good point. Normally though something with .sh extension should have shebang setup properly. Also run chmod a+x zenity.sh as well to call directly from command line. Furthermore script shouldn't be named zenity.sh leading to possible confusion with /usr/bin/zenity. Welcome back BTW :) – WinEunuuchs2Unix May 9 at 11:55

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.