I want to create hotkeys that can automatically generate code for me. This is my start on it.
Func test1()
Global $endString
Global $nOutput
Send("^a^c")
$clipboard = ClipGet ()
$endString = "Hello World"
$nOutput = StringRegExpReplace($clipboard, "}[\n]*\z$", $endString)
ClipPut($nOutput);
Send("^v")
EndFunc ;==>test
To modify the script I am sending the hot keys to copy all, editing it and then pasting over all. Its a bit odd but it worked until I tried the regex you see above to find the last } in the document. I suspect the clipboard doesnt have it \z. Any idea how I can do this and properly copy the file that is currently being worked on? If not... different regex?
Thanks all
HotKeySet('^r', '_test1')
HotKeySet('^q', '_Quit')
While 1
Sleep(10)
WEnd
Func _test1()
Local Static $endString = "; Hello World"
Local $sOutput
; End the tool tip.
ToolTip("")
; Get the window handle and keep it active on send.
$hWin = WinGetHandle("")
SendKeepActive($hWin)
; Select all and copy to clipboard.
Send("^a^c")
Sleep(200)
; Get text from clipboard and check if empty.
$clipboard = ClipGet()
If $clipboard == "" Then
ToolTip("Clipboard is empty.")
AdlibRegister('_TimeOut', 1000)
Return
EndIf
; Replace }$ with }$endString$.
$sOutput = StringRegExpReplace($clipboard, "(*ANYCRLF)(?m)\}$", "}" & $endString)
; Put text to the clipboard and then paste the text.
If @extended Then
ClipPut($sOutput)
Send("^v")
Else
ToolTip("No replacements to paste.")
AdlibRegister('_TimeOut', 1000)
ClipPut("")
EndIf
; Deactivate send keep active window handle.
SendKeepActive("")
EndFunc
Func _TimeOut()
; End the tool tip.
ToolTip("")
AdlibUnRegister('_TimeOut')
EndFunc
Func _Quit()
Exit
EndFunc
The example does a little more checking so that you may see if it is working ok or not.
If you use Send()
then consider SendKeepActive()
to ensure the
window is activated before use of Send()
.
No reason the variables needed to be Global
so I set them as Local
.
The Static
just assigns the variable value once so it just saves
reassigning each time the function is called.
The character }
in regular expression (PCRE) is for
character or group repetitions i.e. a{4}
is a pattern
to find aaaa
. To use }
literary, you need to escape
it with a backslash i.e. \}
.
The PCRE pattern allows for any CRLF sequence. Use
multiline handling (?m)
so the subject is treated
line by line and allows the use of the end of line
anchor $
.
Note a Sleep()
after Send("^a^c")
as it improves the
chances of success for the select all and copy. I was
viewing up to 50% failure for the copy as the script
progresses too quick for the system. Many clipboard managers
use a sleep as well.
I used ToolTip()
to inform of issues and used AdlibRegister()
to cancel the tips else they remain.
Use Hotkeys Ctrl + R to replace and Ctrl + Q to quit.