linux bash script "xdotool windowactivate $variable" returning error

134 views Asked by At
echo $Win_ret
    #returns 98566150

#xdotool windowactivate 98566150
    #functions

xdotool windowactivate $Win_ret
    #does not function

I assume that this is a syntax error, or variable type. I don't have much experience with bash.

Full script:

#!/bin/bash
 
case $1 in
   "play")
       key="ctrl+p"
       ;;
   "next")
       key='ctrl+n'
       ;;
   "prev")
       key='ctrl+b'
       ;;
   "rand")
        key='ctrl+shift+l'
       ;;
   *)
       echo "Usage: $0 play|next|prev|rand"
       exit 1
        ;;
esac

declare -i Win_ret=&(xdotool getactivewindow)

xdotool search --name "MediaMonkey" windowactivate #--sync
sleep 0.1
xdotool key $key
sleep 0.1

echo $Win_ret
    #returns 98566150

#xdotool windowactivate 98566150
    #functions

xdotool windowactivate $Win_ret
    #does not function

exit 0

I am expecting the line:

xdotool windowactivate $Win_ret

to return me to the window 98566150 as this does:

xdotool windowactivate 98566150

Instead it pops this error when run in terminal:

There are no windows in the stack
Invalid window '%1'
Usage: windowactivate [options] [window=%1]
--sync    - only exit once the window is active (is visible + active)
If no window is given, %1 is used. See WINDOW STACK in xdotool(

I have tried with and without declaring Win_ret as an integer.

Placing " around $Win_ret gives a different error:

X Error of failed request:  BadWindow (invalid Window parameter)
  Major opcode of failed request:  20 (X_GetProperty)
  Resource id in failed request:  0x0
  Serial number of failed request:  26
  Current serial number in output stream:  26

xdotool uses binary references to windows.

1

There are 1 answers

6
mandy8055 On

You can replace & with $ on line declare -i Win_ret=&(xdotool getactivewindow) to get your script working. & operator in bash is primairly used to run a command in the background, not to get the output of a command.

#!/bin/bash
 
case $1 in
   "play")
       key="ctrl+p"
       ;;
   "next")
       key='ctrl+n'
       ;;
   "prev")
       key='ctrl+b'
       ;;
   "rand")
        key='ctrl+shift+l'
       ;;
   *)
       echo "Usage: $0 play|next|prev|rand"
       exit 1
        ;;
esac

declare -i Win_ret=$(xdotool getactivewindow) # this line

xdotool search --name "MediaMonkey" windowactivate #--sync
sleep 0.1
xdotool key $key
sleep 0.1

echo $Win_ret

xdotool windowactivate $Win_ret

exit 0
echo $Win_ret

xdotool windowactivate $Win_ret

Thanks to @pjh for suggesting a wonderful webapp shellCheck which was new for me as well to test your script for these types of minor problems and solution suggestion. You can also make use of the same to identify this problem.