kybd macro script is not removing occurrence of the carets

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

kybd macro script is not removing occurrence of the carets

11 Dec 2018, 12:51

For some reason this keyboard macro is not eliminating the caret character. Can someone please point out my error?

Code: Select all

;alt-=                        remove caret
!=::
sendinput, ^a^c
in_put:="^"
out_put:=""
clipboard:=strreplace(clipboard, in_put, out_put) ; OR clipboard:=regexreplace(clipboard, in_put, out_put)
sendinput, ^v
return
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
User avatar
TLM
Posts: 1608
Joined: 01 Oct 2013, 07:52
Contact:

Re: kybd macro script is not removing occurrence of the carets

11 Dec 2018, 23:20

try messing with sendmode, play placed before the sendinput
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: kybd macro script is not removing occurrence of the carets

12 Dec 2018, 11:29

TLM, thanks for he suggestion but sadly, it doesn't work. Modified the original but ran into problems. In the script below, the "1%clipboard%" variable is 90% empty, and the "2%clipboard%" variable is always empty. For testing, attached is a sample text page in which I replaced all occurrences of the word "the" with "^the".

Another problem I encountered is that sometimes the text contains wikipedia markup language in which there are numerous occurrences of double braces like "{{" and "}}". On the rare occasion the script worked, it stripped away all the double braces and their contents, as well as added carriage returns to each line of the result.

Code: Select all

;alt-=                        remove caret 
!=::
clipboard:=
sendinput, ^a^c
	msgbox, 1%clipboard%
in_put:="^"
out_put:=""
	clipboard:=strreplace(%clipboard%, in_put, out_put)
	msgbox, 2%clipboard%
sendinput, %clipboard%
return
Attachments
doc6.txt
(2.54 KiB) Downloaded 30 times
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: kybd macro script is not removing occurrence of the carets

12 Dec 2018, 11:53

Try this: (with ClipWait and a syntax correction)

Code: Select all

!=::    ;   hotkey Alt + "="
    clipboard:=
    sendinput, ^a^c
    ClipWait
    msgbox, 1%clipboard%
    in_put:="^"
    out_put:=""
    clipboard:=strreplace(clipboard, in_put, out_put)
    msgbox, 2%clipboard%
    sendinput, %clipboard%
return
I hope that helps.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: kybd macro script is not removing occurrence of the carets

12 Dec 2018, 18:51

wolf_II, thanks for the correction. It functions as it is supposed to except it inserts an additional CR/LF which generates an empty line. Is there a way to strip that extra CR/LF from the the end of the %clipboard% variable ?
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: kybd macro script is not removing occurrence of the carets

12 Dec 2018, 19:13

Try RTrim():

Code: Select all

!=::    ;   hotkey Alt + "="
    clipboard:=
    sendinput, ^a^c
    ClipWait
    msgbox, 1%clipboard%
    in_put:="^"
    out_put:=""
    clipboard:=strreplace(clipboard, in_put, out_put)
    Clipboard := RTrim(Clipboard, "`r`n")   ; <-- here
    msgbox, 2%clipboard%
    sendinput, %clipboard%
return
Untested.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: kybd macro script is not removing occurrence of the carets

13 Dec 2018, 13:34

wolf_II, thanks again. It did not work, the results were the same regardless of where the Rtrim() statement was placed logically. Also tried Trim(), just in case but it didn't matter.

After it completed the paste, the keyboard went crazy and had to reboot to reset. Decided to abandon the idea for the time being and many thanks again for your help.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: kybd macro script is not removing occurrence of the carets

13 Dec 2018, 13:52

There might be a problem with doing all this to the Clipboard variable.
I know for sure that it behaves different to "normal" variables. aka slower.
You could consider reading the file contents to a "normal" variable via FileRead, or maybe (after ClipWait) copy Clipboard to a "normal" variable.
Then look for the origin of CRLF, and do the replacements needed.
Also, I guess the needed reboot may have been caused by SendInput (sending wrong stuff to wrong places?)
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: kybd macro script is not removing occurrence of the carets

13 Dec 2018, 15:31

wolf_II, the text is on screen (at wikisource) and not in a file. Also moved the script to 'alt+shift+9' key combination. Below, is my attempt at copying the text into physical file in the working directory which should have stripped the carriage returns but I am doing something wrong, the %clipboardall% variablle is empty!!

SetWorkingDir, D:\ahk-functions-library

Code: Select all

;alt-shift-9                  remove caret 
!+9::
	file_name:= "temp.txt"
	if FileExist(file_name)
		filedelete, file_name
    page_in:=
    page_out:=
    sendinput, ^a^c
	clipwait
	fileappend, file_name, *c %clipboardall%
	msgbox %clipboardall%	;this is empty!!

	clipwait
	fileread, page_in, *c file_name
	;Msgbox %page_in%
    in_put:="^"
    out_put:=""
    page_out:=strreplace(page_in, in_put, out_put)
    sendinput, %page_out%
return
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: kybd macro script is not removing occurrence of the carets

13 Dec 2018, 16:15

Try this:

Code: Select all

!+9:: ; alt-shift-9                  remove caret 
    sendinput, ^a^c
	clipwait
    newText := Clipboard    
    newText := StrReplace(newText, "^")

    ; when done with the text manipulation, we can copy back to Clipboard
    Clipboard := newText
    sendinput, ^v ; <- here I suggest to writing to a file to avoid sending stuff to wrong place
return
I hope that helps.
User avatar
ineuw
Posts: 172
Joined: 11 Sep 2014, 14:12

Re: kybd macro script is not removing occurrence of the carets

13 Dec 2018, 18:10

wolf_II, It works . . . with a minor modification. Without the Msgbox, it didn't work because it was too fast. Then I replaced Msgbox with "clipwait." Many many thanks for your patience and efforts.

Code: Select all

!+9::
    newText:=
    sendinput, ^a^c
    clipwait,
    newText:= clipboard
    ;msgbox, %newText%
    newText := StrReplace(newText, "^")
    clipwait,                 ;msgbox, %newText%
    clipboard := newText      ;when done with the text manipulation, we can copy back to Clipboard
    sendinput, ^v ; <- here I suggest to writing to a file to avoid sending stuff to wrong place
return
P.S: The keyboard problem disappeared as well.
Win 10 Professional 64bit 21H2 16Gb Ram AHK current as of 2021-12-26 .

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: jameswrightesq, mikeyww, wpulford and 326 guests