Autofill Fields in online forms

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
mzsade
Level 5
Level 5
Posts: 776
Joined: Sun Jul 19, 2009 4:36 am

Autofill Fields in online forms

Post by mzsade »

Found this on Twitter,

Code: Select all

 Command Line Magic‏ @climagic Mar 10

n=1;while :;do xinput --query-state 8|grep 'button\[1\]=d' &&{ xdotool type "$n"; n=$[n+1]; };sleep 0.3;done # Auto type next $n on click
4 replies 32 retweets 72 likes
Command Line Magic‏ @climagic Mar 10

@climagic This one came up when dealing with a stupid GUI that required me to insert the values in a bunch of fields manually.
Can't figure out how to use it. Wrote a script for one i use daily.

Code: Select all

#!/bin/bash

n=<First_Name>
n=<Last_Name>
n=<Tel_No>

n=1;while :;do xinput --query-state 8|grep 'button\[1\]=d' &&{ xdotool type "$n"; n=$[n+1]; };sleep 0.3;done 
As you can see i'm out of my depth here. Nothing happens when i click in the fields after calling the script in the terminal, and i end up having to kill it with <Ctrl+C>..
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Linux User #481272 Reg: 15th Sept., 2008
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Autofill Fields in online forms

Post by Pilosopong Tasyo »

mzsade wrote:Nothing happens when i click in the fields after calling the script in the terminal...
xinput --query-state 8 returns the following on my end:

Code: Select all

administrator@main-desktop:~$ xinput --query-state 8
1 class :
KeyClass
	key[0]=up
	key[1]=up
	key[2]=up
	key[3]=up
	key[4]=up
	key[5]=up
	key[6]=up
	key[7]=up
	key[8]=up
	key[9]=up
	key[10]=up
	key[11]=up
	key[12]=up
	key[13]=up
	key[14]=up
	key[15]=up
	key[16]=up
	key[17]=up
	key[18]=up
	key[19]=up
	key[20]=up
	key[21]=up
	key[22]=up
	key[23]=up
	key[24]=up
	key[25]=up
	key[26]=up
	key[27]=up
	key[28]=up
	key[29]=up
	key[30]=up
	key[31]=up
	key[32]=up
	key[33]=up
	key[34]=up
	key[35]=up
	key[36]=up
	key[37]=up
	key[38]=up
	key[39]=up
	key[40]=up
	key[41]=up
	key[42]=up
	key[43]=up
	key[44]=up
	key[45]=up
	key[46]=up
	key[47]=up
	key[48]=up
	key[49]=up
	key[50]=up
	key[51]=up
	key[52]=up
	key[53]=up
	key[54]=up
	key[55]=up
	key[56]=up
	key[57]=up
	key[58]=up
	key[59]=up
	key[60]=up
	key[61]=up
	key[62]=up
	key[63]=up
	key[64]=up
	key[65]=up
	key[66]=up
	key[67]=up
	key[68]=up
	key[69]=up
	key[70]=up
	key[71]=up
	key[72]=up
	key[73]=up
	key[74]=up
	key[75]=up
	key[76]=up
	key[77]=up
	key[78]=up
	key[79]=up
	key[80]=up
	key[81]=up
	key[82]=up
	key[83]=up
	key[84]=up
	key[85]=up
	key[86]=up
	key[87]=up
	key[88]=up
	key[89]=up
	key[90]=up
	key[91]=up
	key[92]=up
	key[93]=up
	key[94]=up
	key[95]=up
	key[96]=up
	key[97]=up
	key[98]=up
	key[99]=up
	key[100]=up
	key[101]=up
	key[102]=up
	key[103]=up
	key[104]=up
	key[105]=up
	key[106]=up
	key[107]=up
	key[108]=up
	key[109]=up
	key[110]=up
	key[111]=up
	key[112]=up
	key[113]=up
	key[114]=up
	key[115]=up
	key[116]=up
	key[117]=up
	key[118]=up
	key[119]=up
	key[120]=up
	key[121]=up
	key[122]=up
	key[123]=up
	key[124]=up
	key[125]=up
	key[126]=up
	key[127]=up
	key[128]=up
	key[129]=up
	key[130]=up
	key[131]=up
	key[132]=up
	key[133]=up
	key[134]=up
	key[135]=up
	key[136]=up
	key[137]=up
	key[138]=up
	key[139]=up
	key[140]=up
	key[141]=up
	key[142]=up
	key[143]=up
	key[144]=up
	key[145]=up
	key[146]=up
	key[147]=up
	key[148]=up
	key[149]=up
	key[150]=up
	key[151]=up
	key[152]=up
	key[153]=up
	key[154]=up
	key[155]=up
	key[156]=up
	key[157]=up
	key[158]=up
	key[159]=up
	key[160]=up
	key[161]=up
	key[162]=up
	key[163]=up
	key[164]=up
	key[165]=up
	key[166]=up
	key[167]=up
	key[168]=up
	key[169]=up
	key[170]=up
	key[171]=up
	key[172]=up
	key[173]=up
	key[174]=up
	key[175]=up
	key[176]=up
	key[177]=up
	key[178]=up
	key[179]=up
	key[180]=up
	key[181]=up
	key[182]=up
	key[183]=up
	key[184]=up
	key[185]=up
	key[186]=up
	key[187]=up
	key[188]=up
	key[189]=up
	key[190]=up
	key[191]=up
	key[192]=up
	key[193]=up
	key[194]=up
	key[195]=up
	key[196]=up
	key[197]=up
	key[198]=up
	key[199]=up
	key[200]=up
	key[201]=up
	key[202]=up
	key[203]=up
	key[204]=up
	key[205]=up
	key[206]=up
	key[207]=up
	key[208]=up
	key[209]=up
	key[210]=up
	key[211]=up
	key[212]=up
	key[213]=up
	key[214]=up
	key[215]=up
	key[216]=up
	key[217]=up
	key[218]=up
	key[219]=up
	key[220]=up
	key[221]=up
	key[222]=up
	key[223]=up
	key[224]=up
	key[225]=up
	key[226]=up
	key[227]=up
	key[228]=up
	key[229]=up
	key[230]=up
	key[231]=up
	key[232]=up
	key[233]=up
	key[234]=up
	key[235]=up
	key[236]=up
	key[237]=up
	key[238]=up
	key[239]=up
	key[240]=up
	key[241]=up
	key[242]=up
	key[243]=up
	key[244]=up
	key[245]=up
	key[246]=up
	key[247]=up
As you see in the output, all you get are key[...]=up. No amount of grepping 'button\[1\]=d' will ever return any result (and even if it does find a match, it still won't work -- more on this below). Hence the inline code between {...} gets skipped and it will just loop forever until you interrupt it.

If you're supposed to be monitoring button presses on your mouse, and I suspect this is the case, find the correct device id first:

Code: Select all

administrator@main-desktop:~$ xinput
⎡ Virtual core pointer                    	id=2	[master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer              	id=4	[slave  pointer  (2)]
⎜   ↳ ImExPS/2 Generic Explorer Mouse         	id=11	[slave  pointer  (2)]
⎣ Virtual core keyboard                   	id=3	[master keyboard (2)]
    ↳ Virtual core XTEST keyboard             	id=5	[slave  keyboard (3)]
    ↳ Power Button                            	id=6	[slave  keyboard (3)]
    ↳ Video Bus                               	id=7	[slave  keyboard (3)]
    ↳ Power Button                            	id=8	[slave  keyboard (3)]
    ↳ Sleep Button                            	id=9	[slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard            	id=10	[slave  keyboard (3)]
Device id is 11, on my end. xinput --query-state 11 does return something more meaningful:

Code: Select all

administrator@main-desktop:~$ xinput --query-state 11
2 classes :
ButtonClass
	button[1]=down
	button[2]=up
	button[3]=up
	button[4]=up
	button[5]=up
	button[6]=up
	button[7]=up
	button[8]=up
	button[9]=up
	button[10]=up
	button[11]=up
	button[12]=up
	button[13]=up
ValuatorClass Mode=Relative Proximity=In
	valuator[0]=832
	valuator[1]=733
	valuator[2]=0
	valuator[3]=-3127
I held the left mouse button down when I entered the command above, hence button[1]=down.

Another thing: grep 'button\[1\]=d'

The escape character is unnecessary if you're using quotes. You'll only introduce a logical bug in your code. 'button[1]=d' or "button[1]=d" will suffice:

Code: Select all

administrator@main-desktop:~$ echo button\[1\]=d
button[1]=d
administrator@main-desktop:~$ echo "button\[1\]=d"
button\[1\]=d
administrator@main-desktop:~$ echo 'button\[1\]=d'
button\[1\]=d
administrator@main-desktop:~$ echo "button[1]=d"
button[1]=d
administrator@main-desktop:~$ echo 'button[1]=d'
button[1]=d
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
User avatar
mzsade
Level 5
Level 5
Posts: 776
Joined: Sun Jul 19, 2009 4:36 am

Re: Autofill Fields in online forms

Post by mzsade »

Thank you so much, i checked the device number with xinput like you asked and it was 11 (the usb mouse i use on my laptop).
Made a few clumsy changes and i have got something that works, i can fill in the fields with a click but not always in the order i want, but it works and we can go about polishing it at our leisure. Thanks again, btw, in my case the escape characters seemed to be essential to the script, the thing wouldn't work without them.

Code: Select all

#!/bin/bash

n=1; while :;
        do xinput --query-state 11|grep 'button\[1\]=d' &&{ xdotool type "First_Name"; n=$[n+1]; };sleep 0.3;
           xinput --query-state 11|grep 'button\[1\]=d' &&{ xdotool type "Last_Name"; n=$[n+1]; };sleep 0.3;
           xinput --query-state 11|grep 'button\[1\]=d' &&{ xdotool type "Tel_No"; n=$[n+1]; };sleep 0.3;
        done 
Linux User #481272 Reg: 15th Sept., 2008
User avatar
Pilosopong Tasyo
Level 6
Level 6
Posts: 1432
Joined: Mon Jun 22, 2009 3:26 am
Location: Philippines

Re: Autofill Fields in online forms

Post by Pilosopong Tasyo »

mzsade wrote:...btw, in my case the escape characters seemed to be essential to the script, the thing wouldn't work without them.
Ah, good to know. You're welcome. :)
o Give a man a fish and he will eat for a day. Teach him how to fish and he will eat for a lifetime!
o If an issue has been fixed, please edit your first post and add the word [SOLVED].
Locked

Return to “Scripts & Bash”