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

interpretive language scripts


Moderator: Forum moderators

Post Reply
User avatar
theroar84
Posts: 89
Joined: Sat May 08, 2021 4:21 pm
Location: US
Has thanked: 44 times
Been thanked: 11 times
Contact:

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

Post 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.

Last edited by rockedge on Fri Mar 15, 2024 8:57 pm, edited 1 time in total.

Running Fossa64 9.5 Multiple PC's Frugal Installs on EXT4 hard drives. Multiple Frugals per PC too.

williwaw
Posts: 1948
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 172 times
Been thanked: 369 times

Re: xdotool Script with Loops and variables?

Post 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.

User avatar
theroar84
Posts: 89
Joined: Sat May 08, 2021 4:21 pm
Location: US
Has thanked: 44 times
Been thanked: 11 times
Contact:

Re: xdotool Script with Loops and variables?

Post 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.

Running Fossa64 9.5 Multiple PC's Frugal Installs on EXT4 hard drives. Multiple Frugals per PC too.

User avatar
theroar84
Posts: 89
Joined: Sat May 08, 2021 4:21 pm
Location: US
Has thanked: 44 times
Been thanked: 11 times
Contact:

Re: xdotool Script with Loops and variables?

Post 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.

Running Fossa64 9.5 Multiple PC's Frugal Installs on EXT4 hard drives. Multiple Frugals per PC too.

Post Reply

Return to “Scripts”