Find and replace accented characters

Share your favorite FastKeys commands
Post Reply
SafeTex
Posts: 12
Joined: Mar 21st, ’20, 09:05

Post by SafeTex » Jan 31st, ’21, 10:04

Hello

Same problem as yesterday ;)

I think I have a valid script/command/file (or whatever you call it) found on the AHK website (this is in the akh file)

MsgBox % StrUnmark("ÁáÀàÂâǍǎĂăÃãẢảẠạÄäÅåĀāĄąẤấẦầẪẫẨẩẬậẮắẰằẴẵẲẳẶặǺǻĆćĈĉČčĊċÇçĎďĐđÐÉéÈèÊêĚěĔĕẼẽẺẻĖėËëĒēĘęẾếỀềỄễỂểẸẹỆệĞğĜĝĠġĢģĤĥĦħÍíÌìĬĭÎîǏǐÏïĨĩĮįĪīỈỉỊịĴĵĶķĹ弾ĻļŁłĿŀŃńŇňÑñŅņÓóÒòŎŏÔôỐốỒồỖỗỔổǑǒÖöŐőÕõØøǾǿŌōỎỏƠơỚớỜờỠỡỞởỢợỌọỘộṔṕṖṗŔŕŘřŖŗŚśŜŝŠšŞşŤťŢţŦŧÚúÙùŬŭÛûǓǔŮůÜüǗǘǛǜǙǚǕǖŰűŨũŲųŪūỦủƯưỨứỪừỮữỬửỰựỤụẂẃẀẁŴŵẄẅÝýỲỳŶŷŸÿỸỹỶỷỴỵŹźŽžŻż")

; Removes marks from letters. Requires Windows Vista or later.
StrUnmark(string) {
chars := {"a":"ⱥ","b":"ƀ","c":"ȼ","d":"đ","e":"ɇ","f":"ꞙ","g":"ǥ","h":"ħ","i":"ɨ"
,"j":"[ɉʄɟ]","k":"[ꝅꝁꞣꝃ]","l":"[łꝉŀ]","n":"ꞥ","o":"[ᴓꬿǿꝋꬾø]","p":"[ꝑᵽ]"
,"q":"[ꝗꝙ]","r":"[ɍꞧ]","s":"ꞩ","t":"[ŧⱦ]","v":"ꝟ","y":"ɏ","z":"ƶ"}
string := RegExReplace(string,"Ð","D")
For repl, find in chars
{
StringUpper, urepl, repl
StringUpper, ufind, find
string := RegExReplace(string,find,repl)
, string := RegExReplace(string,ufind,urepl)
}
len := DllCall("Normaliz.dll\NormalizeString", "int", 2
, "wstr", string, "int", StrLen(string)
, "ptr", 0, "int", 0) ; Get *estimated* required buffer size.
Loop {
VarSetCapacity(buf, len * 2)
len := DllCall("Normaliz.dll\NormalizeString", "int", 2
, "wstr", string, "int", StrLen(string)
, "ptr", &buf, "int", len)
if len >= 0
break
if (A_LastError != 122) ; ERROR_INSUFFICIENT_BUFFER
return
len *= -1 ; This is the new estimate.
}
; Remove combining marks and return result.
return RegExReplace(StrGet(&buf, len, "UTF-16"), "\pM")
}


but once again, I can't seem to install it

Either I get an error when trying to install it or there is no error but then it does not run when I use my shortcut

Currently, i have the ahk file in

%dir%\REMOVE ACCENTS FROM FILE NAMES.ahk

I've tried ticking hooks and even wildcards as I see the file makes reference to regex

Can anyone help please?
User avatar
Tom
Posts: 791
Joined: Nov 24th, ’15, 23:39

Post by Tom » Jan 31st, ’21, 10:30

This script attempts to remove the accented characters from the string - it works for me. I simply copied your code to the command field in FastKeys.

Type: Command
Command:

Code: Select all

MsgBox % StrUnmark("ÁáÀàÂâǍǎĂăÃãẢảẠạÄäÅåĀāĄąẤấẦầẪẫẨẩẬậẮắẰằẴẵẲẳẶặǺǻĆćĈĉČčĊċÇçĎďĐđÐÉéÈèÊêĚěĔĕẼẽẺẻĖėËëĒēĘęẾếỀềỄễỂểẸẹỆệĞğĜĝĠġĢģĤĥĦħÍíÌìĬĭÎîǏǐÏïĨĩĮįĪīỈỉỊịĴĵĶķĹ弾ĻļŁłĿŀŃńŇňÑñŅņÓóÒòŎŏÔôỐốỒồỖỗỔổǑǒÖöŐőÕõØøǾǿŌōỎỏƠơỚớỜờỠỡỞởỢợỌọỘộṔṕṖṗŔŕŘřŖŗŚśŜŝŠšŞşŤťŢţŦŧÚúÙùŬŭÛûǓǔŮůÜüǗǘǛǜǙǚǕǖŰűŨũŲųŪūỦủƯưỨứỪừỮữỬửỰựỤụẂẃẀẁŴŵẄẅÝýỲỳŶŷŸÿỸỹỶỷỴỵŹźŽžŻż")

; Removes marks from letters. Requires Windows Vista or later.
StrUnmark(string) {
chars := {"a":"ⱥ","b":"ƀ","c":"ȼ","d":"đ","e":"ɇ","f":"ꞙ","g":"ǥ","h":"ħ","i":"ɨ"
,"j":"[ɉʄɟ]","k":"[ꝅꝁꞣꝃ]","l":"[łꝉŀ]","n":"ꞥ","o":"[ᴓꬿǿꝋꬾø]","p":"[ꝑᵽ]"
,"q":"[ꝗꝙ]","r":"[ɍꞧ]","s":"ꞩ","t":"[ŧⱦ]","v":"ꝟ","y":"ɏ","z":"ƶ"}
string := RegExReplace(string,"Ð","D")
For repl, find in chars
{
StringUpper, urepl, repl
StringUpper, ufind, find
string := RegExReplace(string,find,repl)
, string := RegExReplace(string,ufind,urepl)
}
len := DllCall("Normaliz.dll\NormalizeString", "int", 2
, "wstr", string, "int", StrLen(string)
, "ptr", 0, "int", 0) ; Get *estimated* required buffer size.
Loop {
VarSetCapacity(buf, len * 2)
len := DllCall("Normaliz.dll\NormalizeString", "int", 2
, "wstr", string, "int", StrLen(string)
, "ptr", &buf, "int", len)
if len >= 0
break
if (A_LastError != 122) ; ERROR_INSUFFICIENT_BUFFER
return
len *= -1 ; This is the new estimate.
}
; Remove combining marks and return result.
return RegExReplace(StrGet(&buf, len, "UTF-16"), "\pM")
}
SafeTex
Posts: 12
Joined: Mar 21st, ’20, 09:05

Post by SafeTex » Jan 31st, ’21, 18:17

Hello Tom

It installs okay for me and when I select the string with accented letters and press on the shortcut, the messenger window comes up and I click on okay but then nothing happens.

Any ideas please?
Thanks
User avatar
Tom
Posts: 791
Joined: Nov 24th, ’15, 23:39

Post by Tom » Feb 1st, ’21, 21:56

This script removes accented characters from selected string:
Shortcut:
Type: Command

Code: Select all

Clipboard:=""
Send, ^c
ClipWait
SendInput, % StrUnmark(Clipboard)
return

; Removes marks from letters. Requires Windows Vista or later.
StrUnmark(string) {
chars := {"a":"ⱥ","b":"ƀ","c":"ȼ","d":"đ","e":"ɇ","f":"ꞙ","g":"ǥ","h":"ħ","i":"ɨ"
,"j":"[ɉʄɟ]","k":"[ꝅꝁꞣꝃ]","l":"[łꝉŀ]","n":"ꞥ","o":"[ᴓꬿǿꝋꬾø]","p":"[ꝑᵽ]"
,"q":"[ꝗꝙ]","r":"[ɍꞧ]","s":"ꞩ","t":"[ŧⱦ]","v":"ꝟ","y":"ɏ","z":"ƶ"}
string := RegExReplace(string,"Ð","D")
For repl, find in chars
{
StringUpper, urepl, repl
StringUpper, ufind, find
string := RegExReplace(string,find,repl)
, string := RegExReplace(string,ufind,urepl)
}
len := DllCall("Normaliz.dll\NormalizeString", "int", 2
, "wstr", string, "int", StrLen(string)
, "ptr", 0, "int", 0) ; Get *estimated* required buffer size.
Loop {
VarSetCapacity(buf, len * 2)
len := DllCall("Normaliz.dll\NormalizeString", "int", 2
, "wstr", string, "int", StrLen(string)
, "ptr", &buf, "int", len)
if len >= 0
break
if (A_LastError != 122) ; ERROR_INSUFFICIENT_BUFFER
return
len *= -1 ; This is the new estimate.
}
; Remove combining marks and return result.
return RegExReplace(StrGet(&buf, len, "UTF-16"), "\pM")
}
SafeTex
Posts: 12
Joined: Mar 21st, ’20, 09:05

Post by SafeTex » Feb 2nd, ’21, 11:13

Hello Tom

Wow, that now works thanks

I see that you added something at the start even though I still don't really understand it.

I did try to read the starters tutorial in AHK but it's really not adapted for beginners. the FastKeys help pages seem a bit better to me for beginners

A couple of years ago, I needed to learn regular expressions, at least simple ones and that was a real effort. I just can't see my mastering scripts at my age so I need to bum them off people.

So I'm very helpful for any help that I get. :)
averebazmadar
Posts: 18
Joined: Jul 30th, ’20, 08:24

Post by averebazmadar » Feb 12th, ’21, 16:25

Thanks for this code, awesome!

How should I modify this for change spaces and dots to _?

like

abc def -> abc_def
abc.def -> abc_def

I want to use it for making safenames

Thanks :)
User avatar
Tom
Posts: 791
Joined: Nov 24th, ’15, 23:39

Post by Tom » Feb 12th, ’21, 17:05

This replaces all spaces and dots in selected string to underscores ("_").

Code: Select all

Clipboard:=""
Send, ^c
ClipWait
string:=clipboard
stringreplace, string, string, %A_Space%, _, all
stringreplace, string, string, ., _, all
SendInput, % string
return
averebazmadar
Posts: 18
Joined: Jul 30th, ’20, 08:24

Post by averebazmadar » Feb 13th, ’21, 07:56

@tom Thank you very much! I really want o give you a like or something but there is no button for that... :D
Post Reply