Page 1 of 1

How to make an xdotool script with loops and variables? (Solved by ChatGPT)

Posted: Fri Mar 15, 2024 6:23 pm
by theroar84

I would like a script that can use xdotool with variables.

This is a sample of what I have created. I have been searching for the correct way to setup a loop with variables that grow in a bash script. Any tips that can get me moving in the right direction would be awesome!

Code: Select all

#!/bin/bash
y=250
for(j=0; j lt 3; j++)
x=450
for(i=0; i lt 4; i++)

xdotool mousemove (x,y) 
xdotool sleep 1
xdotool click --delay 800 1
xdotool sleep 1

x=x+175
Endfor
y=y+175
Endfor

It get this error https://imgur.com/a/udNsCUQ when I run it.

Thanks for your time.


Re: xdotool Script with Loops and variables?

Posted: Fri Mar 15, 2024 6:51 pm
by williwaw

the error is hard for me to decipher looking at the script. (but I am a beginner also)

could you post the script in a codeblock, (the </> button in the editor toolbar above in case your paste did not render correctly in the opening post)

Please explain what the script is supposed to do, step by step.
https://en.wikipedia.org/wiki/Structured_English has a good example of a useful type of pseudocode.


Re: xdotool Script with Loops and variables?

Posted: Sat Mar 16, 2024 2:42 am
by theroar84

I think I am all set. I posted my code to ChatGPT and it actually converted it to a script that is not popping errors anymore. I will keep you posted if I get it all working like I expect in a bit.

Thanks again for the help.


Re: xdotool Script with Loops and variables?

Posted: Sat Mar 16, 2024 2:49 am
by theroar84

This is the fixed script.

Code: Select all

#!/bin/bash

y=250
for (( j=0; j<3; j++ ))
do
    x=450
    for (( i=0; i<4; i++ ))
    do
        xdotool mousemove $x $y
        sleep 1
        xdotool click --delay 800 1
        sleep 1
        x=$(( x + 175 ))
    done
    y=$(( y + 175 ))
done

ChatGPT even explained the fixes...

  • Replaced lt with < in the loop condition.
    Enclosed the loop variables and arithmetic operations within double parentheses ((...)).
    Corrected the syntax of xdotool mousemove to move the mouse to the specified coordinates.
    Removed xdotool sleep as it's not a valid command. Used the sleep command instead.
    Fixed the increment of x within the inner loop.
    Removed unnecessary Endfor. Bash uses done to end loops.