Trouble replacing special character

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Ruy ElPequenoCid
Posts: 7
Joined: 12 Jun 2018, 19:51

Trouble replacing special character

18 Sep 2018, 18:33

The scenario:

I copy a certain DOS/PowerShell/etc. command string from a Word document and I send it a console using AHK. Since the command was placed in a Word document, there is at least 4 types of special characters which suffer modification when they are thrown in Microsoft Word:

For example, this is what some PowerShell command looks like (pay no attention to the command itself, it's fictitious for the purpose of illustration):

Code: Select all

Install-Feature -DBPath "C:\Windows\Logs"
Now see the comparison in the picture:
good_form_bad_form.jpg
(69.99 KiB) Not downloaded yet
This is my code so far, but for some reason the conversion is not taking place :(

Code: Select all

:*:hello::

    StringReplace, clipboard, clipboard, “ , " , All             ; Reference: chr(147) Left double quotation mark
    StringReplace, clipboard, clipboard, ” , " , All             ; Reference: chr(148) Right double quotation mark
    StringReplace, clipboard, clipboard, – , - , All             ; Reference: chr(150) En Dash
    StringReplace, clipboard, clipboard, — , - , All             ; Reference: chr(151) Em Dash
    
    clipboard = %clipboard%
    SendRaw, %clipboard%

return
This same AHK piece works just fine for other replacements, but there is something about these special characters that I cannot figure out.

Any help will be much appreciated it.

Ruy
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trouble replacing special character

19 Sep 2018, 01:02

Hallo,
try:

Code: Select all

:*:hello::

    clipboard := StrReplace(clipboard, chr(147), chr(34)) ; Reference: chr(147) Left double quotation mark
    clipboard := StrReplace(clipboard, chr(148), chr(34)) ; Reference: chr(148) Right double quotation mark
    clipboard := StrReplace(clipboard, chr(150), chr(45)) ; Reference: chr(150) En Dash
    clipboard := StrReplace(clipboard, chr(151), chr(45)) ; Reference: chr(151) Em Dash
    
    clipboard = %clipboard%
    SendRaw, %clipboard%

return
Untested, my german word uses other characters:
chr(8222) Left double quotation mark
chr(8220) Right double quotation mark
User avatar
Ruy ElPequenoCid
Posts: 7
Joined: 12 Jun 2018, 19:51

Re: Trouble replacing special character

20 Sep 2018, 04:27

Wow Rohwedder, thanks so much for pointing me in the right direction. I did not know to format my code like that. :)

I tried the code you sent and still was not working. But I read your comment at the end and got me thinking that the "entities" might have had a different number. :shock:

According to the two sources below these characters receive numbers like 8220, 8221, etc (just like you suggested).

http://slayeroffice.com/tools/ascii/ (paste or type your character and press the equal sign)
https://www.ascii.cl/htmlcodes.htm (very last section)

Yet, in a site like http://asciivalue.com/ one can paste the same characters and still get the three-digit values (which won't work for our code here). :headwall:

Here is a string featuring seven offenders for whoever would like to experiment:

“ ” ‘ ’ – — …

So thanks again for taking the time to help me with this. Here is the final code, tested and working well: :superhappy:

Code: Select all

:*:hello::

    clipboard := StrReplace(clipboard, chr(8220), chr(34)) ; Reference: |“| chr(8220) | mistakenly believed to be chr(147) | Name: Left double quotation mark
    clipboard := StrReplace(clipboard, chr(8221), chr(34)) ; Reference: |”| chr(8221) | mistakenly believed to be chr(148) | Name: Right double quotation mark
    clipboard := StrReplace(clipboard, chr(8216), chr(39)) ; Reference: |‘| chr(8216) | mistakenly believed to be chr(145) | Name: Left single quotation mark
    clipboard := StrReplace(clipboard, chr(8217), chr(39)) ; Reference: |’| chr(8217) | mistakenly believed to be chr(146) | Name: Right single quotation mark
    clipboard := StrReplace(clipboard, chr(8211), chr(45)) ; Reference: |–| chr(8211) | mistakenly believed to be chr(150) | Name: En Dash
    clipboard := StrReplace(clipboard, chr(8212), chr(45)) ; Reference: |—| chr(8212) | mistakenly believed to be chr(151) | Name: Em Dash
    clipboard := StrReplace(clipboard, chr(8230), chr(46)chr(46)chr(46)) ; Reference: |…| chr(8230) | mistakenly believed to be chr(133) | Name: Horizontal ellipsis
    
    ; Encoding Referece:
    ; http://slayeroffice.com/tools/ascii/
    ; https://www.ascii.cl/htmlcodes.htm

return
Ruy
Rohwedder
Posts: 7623
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trouble replacing special character

20 Sep 2018, 09:38

Hallo,
If you need the code of a character in Word, highlight the character and press alt+c. Word converts the character to its hexadecimal code. E.g.: Horizontal ellipsis = 2026. Alt+c again, Word converts back.
Use it in Autohotkey with chr(0x2026).

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 371 guests