How to check whether there are cyrillic symbols, typed on a field

Get help with using AutoHotkey and its commands and hotkeys
lodiko
Posts: 3
Joined: 12 Aug 2018, 10:24

How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 10:35

The situation is this one:

I have a web page with some amount of fields. In some fields the typed text is English, in other - Cyrillic+English.
I need to jump with Tab button between all the fields and to check whether there are cyriliic symbols, typed on the field. If there are such symbols - to copy the field content to the cipboard and to jump to the next field.

Is this possible to be done with AHK?
garry
Posts: 1575
Joined: 22 Dec 2013, 12:50

Re: How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 12:07

just an idea
EDIT1 = added stringlen
EDIT2 = skip if only latin characters / save to notepad not latin characters / add numbers from edit
EDIT3 = check for cyrillic

Code: Select all

;- notepad++ save as UTF-8-BOM
;- use TAB for check the 4 EDIT's / SKIP if latin characters
;- https://unicode-table.com/en/#0421
;- MODIFIED=20180813 11:18  parse for ASCII 1024-1279 (cyrillic)
;-----------------------------------------------------------
#warn
#NoEnv
setworkingdir,%a_scriptdir%
i:=0
j=0
total:=4  ; total edit's
endx:=(total+1)
;----------------------------
Gui,2:default
Gui,2:Font,s12 cGray,Lucida Console
Gui,2:Color,Black
Gui,2:Color,ControlColor, Black
Gui,2:add,text,x0 y0 w0 h0 vTx0
Gui,2:add,text,   x8   y10  w20         vTxt1 cRed,
Gui,2:add,text,   x8   y50  w20         vTxt2 cRed,
Gui,2:add,text,   x8   y90  w20         vTxt3 cRed,
Gui,2:add,text,   x8   y130 w20         vTxt4 cRed,
Gui,2:add,Edit,   x30  y10  w220 h30   right  cYellow          vEd1,Masha i medved
Gui,2:add,Edit,   x30  y50  w220 h30   right  cYellow          vEd2,Маша и Медведь CCCP
Gui,2:add,Edit,   x30  y90  w220 h30   right  cYellow          vEd3,Маша и Медведь
Gui,2:add,Edit,   x30  y130 w220 h30   right  cYellow          vEd4,САРАЕВО test sarajevo   ;- also cyrillic
Gui,2:add,Edit,   x270 y10  w500 h190  left   cGray            vEd9
Gui,2:add,text,   x270 y220 w500       center cTeal            vTx9,Show cyrillic in edit 
Gui,2:add,Button, x30  y170 w220 h30   gA2,Cyrillic -table
Gui,2:add,Button, x30  y210 w220 h30   gA1,Send Edit to notepad
Gui,2:show,x100 y20 w800 h260,TEST ( Use TAB for test )
Guicontrol,2:Focus,tx0
Return
;-------------------------------
2GuiClose:
ExitApp

a2:
run, https://unicode-table.com/en/#0421
return

;-- check for x Edit's
tab::
Gui,2:submit,nohide
loop,%total%
  Guicontrol,2:,txt%a_index%
;msgbox,%end%
i++
if (i>total)
  {
  if (i=endx)
     {
     aad:= ed9 . "`n-------------------------------" 
     Guicontrol,2:,ed9,%aad%
	 return
	 }
  return
  }
  
x := % ed%i%
z=txt%i%
Guicontrol,2:,%z%,%i%
;- check for ASCII 1024-1279  (32-165)
e:=""
;- check if cyrillic
loop,parse,x,`
   {
   Transform,B2,ASC,%A_LOOPFIELD%
   if ((b2>1023) and (b2<1280))
     e .= a_loopfield
   }
if e<> 
  {
  j++
  if j=1
    aac:= I . "-" . x
  else	
    aac:= ed9 . "`n" . I . "-" . x 
  Guicontrol,2:,ed9,%aac%
  Guicontrol,2:Focus,tx0
  return
  }
send,{TAB} 
Guicontrol,2:Focus,tx0
return

a1:
Gui,2:submit,nohide
;msgbox, 262208, ,ED1=%ed1%`nED2=%ed2%`nED3=%ed3%`n------------------`nED9=`n%ed9%,
;-- Copy to clipboard and write to notepad --
pr=notepad.exe
sc=ahk_exe notepad.exe
IfWinNotExist,%sc%
 Run, %pr%,,,
IfWinNotActive ,%sc%,,WinActivate,%sc%
    WinWaitActive,%sc%
stringreplace,ed9,ed9,`n,`r`n,all
clipboard=%ed9%`r`n
send,^v
clipboard=
return
;================ end script =================================================

/*
;-------- saved at Montag, 13. August 2018 09:26:16 --------------
;;-------- https://autohotkey.com/boards/viewtopic.php?f=5&t=53866 ---
; CCCP = Союз Советских Социалистических Республик
; Chr(67) ; Latin Capital Letter C - https://unicode-table.com/en/0043/
; Chr(1057) ; Cyrillic Capital Letter Es - https://unicode-table.com/en/0421/
; btw Сараево != *Сараеbо; Астана
string1 := Chr(67) . "оюз " . Chr(67) . "оветских " . Chr(67) . "оциалистических Республик"
string2 := Chr(1057) . "оюз " . Chr(1057) . "оветских " . Chr(1057) . "оциалистических Республик"
MsgBox % string1 . "`r`n" . string2 " `r`n" ((string1 = string2) ? "are equal" : "are NOT equal, despite appearances!;)")
return
*/

;- old part check with stringlen

Code: Select all

;- notepad++ save as UTF-8-BOM
;- use TAB for check the 3 EDIT's / SKIP if latin characters
;-----------------------------------------------------------
#warn
i=0
j=0
Gui,2:default
Gui,2:Font,s12 cGray,Lucida Console
Gui,2:Color,Black
Gui,2:Color,ControlColor, Black
Gui,2:add,text,x0 y0 w0 h0 vTx0

Gui,2:add,text,x8 y10 w20 h27 vTxt1 cRed,
Gui,2:add,text,x8 y50 w20 h27 vTxt2 cRed,
Gui,2:add,text,x8 y90 w20 h27 vTxt3 cRed,

Gui,2:add,Edit,   x30  y10  w220 h30   right cYellow          vEd1,Masha i medved
Gui,2:add,Edit,   x30  y50  w220 h30   right cYellow          vEd2,Маша и Медведь CCCP
Gui,2:add,Edit,   x30  y90  w220 h30   right cYellow          vEd3,Маша и Медведь
Gui,2:add,Edit,   x270 y10  w500 h190  left  cGray            vEd9
Gui,2:add,Button, x30  y170 w220 h30   gA1,Send Edit to notepad
Gui,2:show,x100 y20 w800 h210,TEST ( Use TAB for test )
Guicontrol,2:Focus,tx0
Return
;-------------------------------
2GuiClose:
ExitApp

;-- check for 3 Edit's
tab::
Gui,2:submit,nohide
i++
Guicontrol,2:,txt1
Guicontrol,2:,txt2
Guicontrol,2:,txt3
if (i>3)
  {
  if (i=4)
     {
     aad:= ed9 . "`n-------------------------------" 
     Guicontrol,2:,ed9,%aad%
	 }
  return
  }
  
x := % ed%i%
z=txt%i%
Guicontrol,2:,%z%,%i%

stringlen,L1,x
;- check for ASCII 32--- 165
e:=""
loop,parse,x,`
   {
   Transform,B2,ASC,%A_LOOPFIELD%
   ;if ((b2>64) and (b2<91)) or ((b2>96) and (b2<123))
   if ((b2>31) and (b2<166))
     e .= a_loopfield
   }
stringlen,L2,e   
;msgbox, 262208, ,Ed%i%`n%x%`n----------------`nE= %e%,1

;- not only latin characters  = yes 
if (l1<>l2)
  {
  j++
  if j=1
    aac:= I . "-" . x
  else	
    aac:= ed9 . "`n" . I . "-" . x 
  Guicontrol,2:,ed9,%aac%
  Guicontrol,2:Focus,tx0
  return
  }
send,{TAB} 
Guicontrol,2:Focus,tx0
return

a1:
Gui,2:submit,nohide
;msgbox, 262208, ,ED1=%ed1%`nED2=%ed2%`nED3=%ed3%`n------------------`nED9=`n%ed9%,
;-- Copy to clipboard and write to notepad --
pr=notepad.exe
sc=ahk_exe notepad.exe
IfWinNotExist,%sc%
 Run, %pr%,,,
IfWinNotActive ,%sc%,,WinActivate,%sc%
    WinWaitActive,%sc%
stringreplace,ed9,ed9,`n,`r`n,all
clipboard=%ed9%`r`n
send,^v
clipboard=
return
;================ end script =================================================
Last edited by garry on 13 Aug 2018, 04:18, edited 3 times in total.
A_AhkUser
Posts: 884
Joined: 06 Mar 2017, 16:18
Location: France

Re: How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 13:48

Hi lodiko,

You can use regexmatch.

Code: Select all

#NoEnv
#SingleInstance force
#Warn

HTML =
(
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge"><meta charset="utf-8" />
		<title>test</title>
	</head>
	<body>
		<input type="text" value="мова"><br>
		<input type="text" value="Москва"><br>
		<input type="text" value="Қазақстан"><br>
		<input type="text" value="Paris"><br>
		<input type="text" value="english-русский"><br>
		<input type="text" value=""><br>
	</body>
</html>
)
GUI, Add, ActiveX, vHTMLDoc w400 h400, HTMLFile ; create a HTML document object for testing purpose
HTMLDoc.write(HTML)
HTMLDoc.close()
GUI, Show, AutoSize
return

!i::
clipboard := "" ; empty the clipboard
result := ""
inputFields := HTMLDoc.getElementsByTagName("input") ; returns a collection of all elements in the document with the input tag name
Loop % inputFields.length
{
	fieldCurrent := inputFields[ a_index - 1 ]
	fieldCurrentContent := fieldCurrent.value
	if (containsCyrillic:=containsCyrillic(fieldCurrentContent)) ; if the field does contain any cyrillic characters...
		result .= fieldCurrentContent . "`r`n" ; ...append it to the overall result
	; MsgBox % fieldCurrentContent . " does" . (containsCyrillic ? A_Space : " not ") . "contain cyrillic characters."
}
clipboard := result
MsgBox % "Result has been copied to clipboard."
return

containsCyrillic(_string) {
; return RegExMatch(_string, "\p{Cyrillic}") ; regexmatch specifying an Unicode character property in option (https://autohotkey.com/docs/misc/RegEx-QuickRef.htm)
return _string ~= "\p{Cyrillic}" ; same as above using a shorthand operator
}
- btw:
- Cyrillic (range: 0400 — 04FF)
- 0400/1024 04FF/1279 255 characters (1279-1024)

Code: Select all

MsgBox % Chr(1024) ; Cyrillic Capital Letter Ie with Grave (https://unicode-table.com/fr/0400/)
MsgBox % Chr(1279) ; Cyrillic Small Letter Ha with Stroke (https://unicode-table.com/fr/04FF/)
Hope this helps.
lodiko
Posts: 3
Joined: 12 Aug 2018, 10:24

Re: How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 14:43

A_AhkUser,

This may help me. But how to proceed when every field in the web page has a different name?

Is it possible to do this thing - when the script detects Cyrillic symbol in the field to:
1. cut field's entire content into the clipboard
2. enter '1' in the beginning of the field
3. paste the clipboard right after the '1'

and after this the script jumps to the next field and makes the same check.
When the field is filled only with English symbols, to jump directly to the next field.
garry
Posts: 1575
Joined: 22 Dec 2013, 12:50

Re: How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 15:16

Is it possible to do this thing - when the script detects Cyrillic symbol in the field to:
1. cut field's entire content into the clipboard
2. enter '1' in the beginning of the field
3. paste the clipboard right after the '1'

and after this the script jumps to the next field and makes the same check.
When the field is filled only with English symbols, to jump directly to the next field.
I tried this / modified script above
------------------------------------------------
@A_AhkUser , thank you for cyrillic codes chr(1024)-- chr(1279)
( sometimes russian also only contents latin letters , example : Actaha, CCCP, Capaebo, Beha .... )
Last edited by garry on 12 Aug 2018, 16:53, edited 2 times in total.
A_AhkUser
Posts: 884
Joined: 06 Mar 2017, 16:18
Location: France

Re: How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 15:39

lodiko wrote:This may help me. But how to proceed when every field in the web page has a different name?
What do you mean by 'name'?
. input elements' 'value' attribute values (i.e. the visible content of the fields)?
. input elements' 'name' attribute values?
. tag names?
The first two should not be an issue as demonstrated in the example above and, a fortiori in the following example. Based on your description I assume that the clipboard is at stake here only to manipulate a temporary value. Correct me if I'm wrong. If so,I suggest instead to simply update the value of the input element:

Code: Select all

#NoEnv
#SingleInstance force
#Warn

HTML =
(
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge"><meta charset="utf-8" />
		<title>test</title>
	</head>
	<body>
		<input type="text" name="test1" value="мова"> Ukrainian<br>
		<input type="text" name="test2" value="Москва"> Russian<br>
		<input type="text" name="test3" value="Қазақстан"> Kazakh<br>
		<input type="text" name="test4" value="Paris"> French<br>
		<input type="text" name="test5" value="english-русский"> A combination of the two<br>
		<input type="text" name="test6" value=""> Nothing<br>
	</body>
</html>
)
GUI, Add, ActiveX, vHTMLDoc w400 h400, HTMLFile ; create a HTML document object for testing purpose
HTMLDoc.write(HTML)
HTMLDoc.close()
GUI, Show, AutoSize
return

!i::
	inputFields := HTMLDoc.getElementsByTagName("input") ; returns a collection of all elements in the document with the input tag name
	Loop % inputFields.length
	{
		fieldCurrent := inputFields[ a_index - 1 ]
		fieldCurrentContent := fieldCurrent.value
		if (fieldCurrentContent ~= "\p{Cyrillic}") { ; if the field does contain any cyrillic characters...
			fieldCurrent.value := "1" . fieldCurrentContent ;  update its value by prepending '1' in the beginning of the field
		}
	}
return
If you want to operate on a specific subset of input element with a specific name attribute, use the getELementsByName method.
A_AhkUser
Posts: 884
Joined: 06 Mar 2017, 16:18
Location: France

Re: How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 16:11

@ garry
garry wrote:thank you for cyrillic codes chr(1024)-- chr(1279)
Your welcome.
Btw, see also: Cyrillic — Unicode® character table
garry wrote:( sometimes russian also only contents latin letters , example : Actana, CCCP, Capaebo, Beha .... )
Yes but it is only true from a visual standpoint (furthermore, even that statement might be wrong when switching from a font to another one).

Code: Select all

; CCCP = Союз Советских Социалистических Республик
; Chr(67) ; Latin Capital Letter C - https://unicode-table.com/en/0043/
; Chr(1057) ; Cyrillic Capital Letter Es - https://unicode-table.com/en/0421/
; btw Сараево != *Сараеbо; Астана

string1 := Chr(67) . "оюз " . Chr(67) . "оветских " . Chr(67) . "оциалистических Республик"
string2 := Chr(1057) . "оюз " . Chr(1057) . "оветских " . Chr(1057) . "оциалистических Республик"
MsgBox % string1 . "`r`n" . string2 " `r`n" ((string1 = string2) ? "are equal" : "are NOT equal, despite appearances!;)")
Cheers
garry
Posts: 1575
Joined: 22 Dec 2013, 12:50

Re: How to check whether there are cyrillic symbols, typed on a field

12 Aug 2018, 17:00

; btw Сараево != *Сараеbо; Астана
yes , sorry , I was wrong
thank you for the example
lodiko
Posts: 3
Joined: 12 Aug 2018, 10:24

Re: How to check whether there are cyrillic symbols, typed on a field

13 Aug 2018, 08:01

Excuse me for the stupid questions, but as you see, I'm completely new here.
@A_AhkUser, yes I thing in my case I need to check fields with a different tag names. But I don't know how to do this.
That's how the webform looks:
https://imgur.com/a/cGAOezr

Image
A_AhkUser
Posts: 884
Joined: 06 Mar 2017, 16:18
Location: France

Re: How to check whether there are cyrillic symbols, typed on a field

13 Aug 2018, 12:17

lodiko wrote:yes I thing in my case I need to check fields with a different tag names. [...] That's how the webform looks[...]
Yea you're right they looks like textarea elements; I would have though of that.. This does not fundamentally change the script - the same example using textarea elements instead:

Code: Select all

#NoEnv
#SingleInstance force
#Warn

HTML =
(
<!DOCTYPE html>
<html>
	<head>
		<meta http-equiv="X-UA-Compatible" content="IE=edge"><meta charset="utf-8" />
		<title>test</title>
	</head>
	<body>
		<textarea rows="3" cols="40">мова</textarea>
		<textarea rows="3" cols="40">Москва</textarea>
		<textarea rows="3" cols="40">Қазақстан</textarea>
		<textarea rows="3" cols="40">Paris</textarea>
		<textarea rows="3" cols="40">english-русский</textarea>
		<textarea rows="3" cols="40"></textarea>
	</body>
</html>
)
GUI, Add, ActiveX, vHTMLDoc w400 h400, HTMLFile ; create a HTML document object for testing purpose
HTMLDoc.write(HTML)
HTMLDoc.close()
GUI, Show, AutoSize
return

!i::
	textAreas := HTMLDoc.getElementsByTagName("textarea")
	Loop % textAreas.length
	{
		this_textArea := textAreas[ a_index - 1 ]
		this_textAreaContent := this_textArea.innerText
		if (this_textAreaContent ~= "\p{Cyrillic}") { ; if the field does contain any cyrillic characters...
			this_textArea.innerText := "1" . this_textAreaContent ;  update its value by prepending '1' in the beginning of the field
		}
	}
return
[EDIT]
see also: Basic Webpage Controls with JavaScript / COM - Tutorial

Return to “Ask For Help”

Who is online

Users browsing this forum: Albireo, Helgef, ivanstera, newbieforever and 34 guests