using Autohotkey to replace diacritics accents in clipboard - replace

I'm trying to write a script in Autohotkey that will take the currently highlighted word, copy it into the clipboard, and then replace accented characters with their non-accented versions. For example, if the word honorábilem is in the clipboard, I want to change it to honorabilem.
This is what I have tried:
F1::
SetTitleMatchMode RegEx
clipboard =
Send, ^c
wordToParse := %clipboard%
wordToParse = RegExReplace(wordToParse,"á","a") ; also tried this: StringReplace, clipboard, clipboard, á, a, All
MsgBox, % clipboard
But the contents of the clipboard don't change. The á never gets replaced with a. Appreciate any help.

The contents of the clipboard don't change (after the change from sending CTRL+C) becuase you're simply not changing the contents of the clipboard after that.
And another mistake you have is assigning values to variables wrong.
I'd assume you don't know the difference between = and :=.
The difference is that using = to assign values is deprecated legacy AHK and should never be used. You're assigning literal text to a variable. As opposed to assigning the result of evaluating some expression, which is what := does.
This line wordToParse = RegExReplace(wordToParse,"á","a") assigns literal text to that variable instead of calling the RegExReplace() function and assigning its result to the variable.
Also, no reason to regex replace if you're not using regex.
The StrReplace() function is what you want.
And then there's also the usage of legacy syntax in an expression:
wordToParse := %clipboard%
Referring to a variable by wrapping it in % is what you'd do in a legacy syntax.
But since you're not doing that, you're using :=, as you should, just ditch the %s.
Revised script:
F1::
;This does nothing for us, removed
;SetTitleMatchMode RegEx
;Empty clipboard
Clipboard := ""
;Switched to SendInput, it's documented as faster and more reliable
SendInput, ^c
;Wait for the clipboard to contain something
ClipWait
wordToParse := Clipboard
wordToParse := StrReplace(wordToParse, "á", "a")
;Since you want to display the contents of the clipboard in
;a message box, first we need to set what we want into it
Clipboard := wordToParse
MsgBox, % Clipboard
return

Related

RegEx to format Wikipedia's infoboxes code [SOLVED]

I am a contributor to Wikipedia and I would like to make a script with AutoHotKey that could format the wikicode of infoboxes and other similar templates.
Infoboxes are templates that displays a box on the side of articles and shows the values of the parameters entered (they are numerous and they differ in number, lenght and type of characters used depending on the infobox).
Parameters are always preceded by a pipe (|) and end with an equal sign (=). On rare occasions, multiple parameters can be put on the same line, but I can sort this manually before running the script.
A typical infobox will be like this:
{{Infobox XYZ
| first parameter = foo
| second_parameter =
| 3rd parameter = bar
| 4th = bazzzzz
| 5th =
| etc. =
}}
But sometime, (lazy) contributors put them like this:
{{Infobox XYZ
|first parameter=foo
|second_parameter=
|3rd parameter=bar
|4th=bazzzzz
|5th=
|etc.=
}}
Which isn't very easy to read and modify.
I would like to know if it is possible to make a regex (or a serie of regexes) that would transform the second example into the first.
The lines should start with a space, then a pipe, then another space, then the parameter name, then any number of spaces (to match the other lines lenght), then an equal sign, then another space, and if present, the parameter value.
I try some things using multiple capturing groups, but I'm going nowhere... (I'm even ashamed to show my tries as they really don't work).
Would someone have an idea on how to make it work?
Thank you for your time.
The lines should start with a space, then a pipe, then another space, then the parameter name, then a space, then an equal sign, then another space, and if present, the parameter value.
First the selection, it's relatively trivial:
^\s*\|\s*([^=]*?)\s*=(.*)$
Then the replacement, literally your description of what you want (note the space at the beginning):
| $1 = $2
See it in action here.
#Blindy:
The best code I have found so far is the following : https://regex101.com/r/GunrUg/1
The problem is it doesn't align the equal signs vertically...
I got an answer on AutoHotKey forums:
^i::
out := ""
Send, ^x
regex := "O)\s*\|\s*(.*?)\s*=\s*(.*)", width := 1
Loop, Parse, Clipboard, `n, `r
If RegExMatch(A_LoopField, regex, _)
width := Max(width, StrLen(_[1]))
Loop, Parse, Clipboard, `n, `r
If RegExMatch(A_LoopField, regex, _)
out .= Format(" | {:-" width "} = {2}", _[1],_[2]) "`n"
else
out .= A_LoopField "`n"
Clipboard := out
Send, ^v
Return
With this script, pressing Ctrl+i formats the infobox code just right (I guess a simple regex isn't enough to do the job).

notepad++ Stop replacing at a specific line

I've been trying to figure something out for a while now and I can't seem to understand. I've looked everywhere and I still can't find it.
I'm trying to make a dictionary for an auto corrector with AutoHotKey and I need to replace the beginning of each line with "::" and somewhere in between the line with another "::"
like so:
::togehter::together
Now I have around 20,000 of these to add with no "::" yet and what I'm doing is this in the replace textbox:
Replace: ^
With: ::
Now it works fine for the first line BUT if I press replace all cause no way am I going to click 20,000 times on replace, it replaces not only from where I am to the bottom but also the beginning too. So every line now has a new "::" added.
So what I need is to be able to tell the replace at what line to stop instead of doing every single line.
Also if you could help me add the "::(word)" after the first ::(misspelled word) that would be a great help.
Image for reference
I have found that the regular expression replace-all of ^ with some text, i.e. to add some text at the start of every line, does not work in some versions of Notepad++. My workaround for this was to use the ^(.) as the search string and include \1 in the replacement. For your case the replacement would be ::\1. The effect here is to replace the first character of each line with :: plus the first character. In a quick test with Notepad++ v7.1, replacing ^ with :: worked as I would want.
Two things should be checked in the Replace dialogue before doing the replace-all: (1) that "Regular expression" is selected and (2) "In selection" is not selected.
The question is not clear how the two words in the input are separated, so assuming that one or more spaces or tabs is used the search string to use is ^(\w+)\h+ and the replace string is ::\1::.
This AutoHotkey script might do what you require.
It leaves unchanged lines that start with '::',
and prepends/replaces text in the others. You copy the original text to the clipboard, run this script, and then the desired text is put on the clipboard. (To create and run the script: copy and paste it into a text editor and save it as myscriptname.ahk, or myscriptname.txt and then drag and drop the file into the AutoHotkey exe file. Or alternatively, if you save it as an ahk file, and install AutoHotkey, you can double-click to run.) AutoHotkey
vText := Clipboard
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2*2)
StringReplace, vText, vText, `r`n, `n, All
Loop, Parse, vText, `n
{
vTemp := A_LoopField
if (vTemp = "")
if (1, vOutput .= "`r`n")
continue
if (SubStr(vTemp, 1, 2) = "::")
if (1, vOutput .= vTemp "`r`n")
continue
StringReplace, vTemp, vTemp, %A_Space%, ::, All
vOutput .= "::" vTemp "`r`n"
}
Clipboard := vOutput
MsgBox done
Return

Use AutoHotkey to implement sed like replacement on clipboard

I want to use AutoHotkey to implement sed like replacement on the clipboard. I have tried several different ways to implement it, although I would like to make something which can be easily extended and be as functional as sed. Ideally it would take the act and take the same commands as sed and replace the current clipboard with the output. Since I use Ditto I will then have both the origlinal and output saved.
The solutions I have thought of and tested are to either make a hotstring which performs one specific sed replacement, e.g. using RegExreplace:
; Make text in clipboard "Camel Case" (retaining all spaces):
:*:xcamelsc::
haystack := Clipboard
needle := "\h*([a-zA-Zåäö])([a-zåäö]*)\h*" ; this retains new lines
replacement := "$U1$2 "
result := RegExReplace(haystack, needle, replacement)
Clipboard =
Clipboard := result
ClipWait
sleep 100
send ^v
return
Another example is
;replace multiple underscore with one space
:*:xr_sc::
haystack := Clipboard
needle := "[\h]*[\_]+"
replacement := " "
result := RegExReplace(haystack, needle, replacement)
Clipboard =
Clipboard := result
ClipWait
return
The flaw with this system is that I would have to make ~500 combinations of hotstrings for each possible combination I would like to have (e.g. a separate hotstring which to make all space underscore). I am not sure how to easily extend this.
Another way to do this is to use a GUI which previews the output and makes it possible to do more things, as implemented in clipboard replace. For this to be satisfactory I have made a hotstring which opens the GUI with the initial replacement filled in, and a hotkeys which automatically performs the replacement and pastes the output, etc. This system only requires that I specify the thing to replace, but I would rather have a system similar to the above which uses variables for all possible replacements so that I can refer to e.g. /^[\t]// to directly perform replacement.
A solution to do this would be to have a hotstring activate if I type
"xr[a string of text to indicate what to replace][a string of text to indicate what to replace with]xx"
i.e. "xx" would take the word just typed, parse it into the command, and perform it.
This would mean that if I type "xr_sxx", the "s" part would be interpreted as two separate variables, and the "" would be assigned the needle and the "s" would be looked up in a table and then inserted in the replacement variable of the RegExReplace.
Does anyone know of a way to do this?
This system only requires that I specify the thing to replace, but I
would rather have a system similar to the above which uses variables
for all possible replacements so that I can refer to e.g. /^[\t]// to
directly perform replacement.
Does anyone know of an easy way to do this?
Rather than specifying Hotstring for each scenario to perform a similar function I've used a method with a Hotkey, Input, and values stored in an Associative Array's.
Here's an Example:
data := {"xcamelsc": ["\h*([a-zA-Zåäö])([a-zåäö]*)\h*", "$U1$2 "]
, "xr_sc": ["[\h]*[\_]+", " "]}
f1::
word := ""
key := ""
Loop {
input, key, V L1 M T1, {Space}{Enter}{Tab}
if (errorlevel == "EndKey:Space") {
if (data.HasKey(word)) {
sendInput % "{BackSpace " StrLen(word)+1 "}"
haystack := Clipboard
needle := data[word].1
replacement := data[word].2
result := RegExReplace(haystack, needle, replacement)
Clipboard =
Clipboard := result
ClipWait
sleep 100
send ^v
}
word := ""
Break
}
else {
word .= Format("{1:L}", key)
}
}
return
; Necessary for typing mistakes when using Input
$BackSpace::
word := SubStr(word, 1, -1)
sendInput, {BackSpace}
return
esc::exitapp

Rename file, remove unnecessary character from file name using Autohotkey

I'm trying to rename files (usually downloaded subtitle) using Autohotkey/RegEx to discard the unnecessary character, remove “.” to space in a way that the final renamed file will contain only name and the four digit year. An example as follows
Original file name/path
D:\Folder\Sub Folder\Hamburger.Hill.1987.BluRay.720p.x264.srt
Renamed file should be like this
D:\Folder\Sub Folder\Hamburger Hill 1987.srt
Initially I was intended only to remove the “.”. With contribution of “Ro Yo Mi” the AHK code is able to remove the “.” to space (Current Code Part 1) and it answered my initial question.
Later I realized there might possibility to also remove the unnecessary character (only to keep the name, year and also original file extension). Ro Yo Mi” also attempted with added new lines of code to rename the unnecessary string from the file name (Current Code Part 2). Although the code apparently showing capable to rename (show in the message code) but finally could not rename actually. There might some further upgrade or changes needed to make it operational to do the job as intended. Current status of the code could be found in the given reference.
Description
The problem the file wasn't getting renamed is because the path was not provided. Therefore AutoHotKey assumes that it's current working directory is where the changes will occur. Since the files aren't actually in the AutoHotKey's script directory, then the FileMove command fails.
This script assumes you'll be providing the fullpath and filename. So with this information this is how I'd remove the characters and rename the file using AutoHotKey.
#.:: ; Replace all "." (except before extension) with spaces
OldCLip := ClipboardAll
Clipboard=
Send ^c
ClipWait, 1
; MsgBox % Clipboard ; for testing
if ( Clipboard ) {
; set the value
String := Clipboard
; String := "D:\Folder\Sub Folder\the.Hamburger.Hill.1987.BluRay.720p.x264.srt"
; split string into the desired components: path, filename upto and including year, and extension
RegexMatch(String, "^(.*\\)(.*?[0-9]{4}).*([.][^.]{3})", SubPart)
FullPath := SubPart1
Filename := RegexReplace(SubPart2, "\.", " ") ; replace dots in the file name with spaces to improve readablity
Filename := RegexReplace(Filename, "i)^the\s+", "") ; remove the `the` and trailing spaces from the beginning of the filename if it exists.
Extension := SubPart3
NewPathFilename := FullPath . Filename . Extension
strMessage := "Renaming '" . String . "' to '" . NewPathFilename . "'"
MsgBox, % strMessage
FileMove, % String, % NewPathFilename
} ; end if
Clipboard := OldClip
return
Sample Message Box
Renaming 'D:\Folder\Sub Folder\the.Hamburger.Hill.1987.BluRay.720p.x264.srt' to 'D:\Folder\Sub Folder\Hamburger Hill 1987.srt'

regex and file read line in autohotley

well i am currently writing a script that is meant to check the logs of another script i wrote to see if it has had three or more unsuccessful pings in a row before a successful one, this is just barebones at the moment but it should look something like this
fileread,x,C:\Users\Michael\Desktop\ping.txt
result:=RegExMatch(%x% ,failure success)
msgbox,,, The file is = %x% `n the result is = %result%
now the file that is trying to read is
success failure success
and for some reason, when it reads the file it says that the variable %x% 'contains illegal characters
when i copy and paste the contents of ping.txt into the script and save it as a variable it works
i have made sure that the file has windows line endings CR +LF
i have assigned the variable generated in file read as another variable thus stripping any trailing or leading whitespace characters
the file is encoded in ANSI and still has the problem with UTF8
Function parameters take variable names without the % symbol, simply remove them.
I also want to point out that if the second parameter is meant to be a regular expression,
instead of a variable containing a regular expression, you will need quotes around it.
As is your script passes an empty string as the pattern which will always return 1
(failure is interpreted as a variable with an empty string associated with it.).
To quote Lexikos:
"An empty string, when compiled as a regex pattern, will match exactly
zero characters at whatever position you attempt to match it. Think of
it this way: For any position n in any string, the next 0 characters
are always the same."
Because you are simply truth testing,
or finding the index I want to point out that Autohotkey has a useful shorthand operator for this.
string := "this is a test"
f1::
result := RegExMatch(string, "\sis")
traytip,, %result%
Return
f2::
result := string ~= "\sis"
traytip,, % result
Return
These hotkeys both do the same thing; the second uses the shorthand operator ~=
and notice how the traytip parameter in the second example has only one %
When you start a command parameter with a % that starts an expression,
and within an expression variables are not enclosed with %.
The ternary operator ?: is also very useful:
string := "this is a test"
f3::traytip,, % (result := string ~= "\sis") ? (result) : ("nothing")
It might look complicated but it's very simple.
Think of
% as if
? as then
: as else
If (true) then (a) else (b)
% (true) ? (a) : (b)
A variable will be evaluated as False if 0 (or nothing) is assigned to it.
But in this example "\sis" is matched and the index of the space is returned (5),
so it is evaluated as True.
You can read more about variables and operators here:
http://l.autohotkey.net/docs/Variables.htm