0
votes

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.

1

1 Answers

2
votes

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