r/AutoHotkey•
Ahks

regexreplace performance issue? maybe?

I am exporting old Lotus Notes documents from a database. During which I need to convert the titles of the documents with a strings like

Regex: Test./*,.|?"

Into

Regex_ Test_________

so Windows doesn't puke on the file name when I export it. I know my regex works, but...

Here is the newbie script I wrote this morning:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^#e:: ; hotkey to start

Loop, 402
{
	Sleep 1000
	Send {Enter} ; Enter to open/select next
	Sleep 1000
	MouseClickDrag, left, 473, 217, 807, 217 ; drag subject 
	Sleep 500
	Send ^c ; copy with ctrl+c
		ClipWait
		string := Clipboard
		Clipboard := RegExReplace(string, "i)[^0-9a-z]","_")
		;MsgBox % ErrorLevel
	Sleep 500
	Send !f 
	Sleep 500
	Send e 
	Sleep 500
	Send {Enter} 
	Sleep 500
	Send !t ; alt+t 
	Sleep 500
	Send {Down 3} 
	Sleep 500
	Send {Enter}
	Sleep 500
	Send !n ; alt+n 
	Sleep 500
	Send ^v ; ctrl+v
	Sleep 500
	Send {NumpadDot}rtf
	Sleep 500
	Send !x ; alt+x
}
return    

The problem is that when I comment out

MsgBox % ErrorLevel

or click the message box real fast, the regexreplace fails to modify my clipboard so I paste the original string later in the script. My last test is to add in a Sleep 5000 like after

Clipboard := RegExReplace(string, "i)[^0-9a-z]","_")
Sleep 5000

and this only works half the time...

I am reading that regexreplace isn't super performative, but what can I do to make my script wait "long enough" or "until the replace is done"?

Posted Sep 1, 2020, 5:25 PM UTC · edited

39
AhksOP•

I may have worked around this with

Send ^c ; copy with ctrl+c
    ClipWait
    string := Clipboard
    fileName := RegExReplace(string, "i)[^0-9a-z]","_")
    

Then later, instead of

send ^v

I do a

Send %fileName%

Still testing and trying to tighten it up...

1
KcDaRookie•

You can also skip the string sub step

Send ^c ; copy with ctrl+c
    ClipWait
    fileName := RegExReplace(Clipboard, "i)[^0-9a-z]","_")
1
AhksOP•

The problem I'm having is with regex not working properly on Clipboard. It works fine on %string% to store it in %fileName%, but when I regex the clipboard itself, I get unreliable or very slow results.

I'm starting to wonder if something in my AV or something in Windows10 is trying to prevent clipboard manipulation.

1
KcDaRookie•

Can you explain what all the sends do? Maybe there is a better way to do it, but I would need some more insight where what is happening.

1
AhksOP•

It's mostly just navigating menu's and windows using the hotkeys in the software.

1
radiantcabbage•

export the data first, save to csv. then it can be parsed as text all at once instead of wrestling with a gui, your code would be leagues more efficient

1
AhksOP•

That would be great... but...

I'll have to wrestle the GUI to export the rich text item no matter how I get the file names. IBM/Lotus notes is "great" in that exporting a rich text document requires the document be open and the body has to be selected. So no matter how I do this, unless I learn lotusscript by osmosis, I have to open each item and export it manually.

1
radiantcabbage•

you can still optimise to not use kbm/clipboard if you don't have to, this is where all your execution time goes. what does the ui look like when you hover it with window spy, lotus is apparently built in a windows environment with named controls.

for example try ControlGetText | ControlSetText instead of copy paste operations, wait for window handles to exist instead of sleeping an arbitrary amount of time between events, ControlFocus instead of manual navigation.

1
AhksOP•

Didn't know about these...

I did notice that window spy changed information when I pointed it at different parts of the Notes window...

I'll read up on these and see what I can hack together. Thanks

1