NicolasLeGland

Script for typing French accentuated characters

May 24th, 2017
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Configuration
  2. #singleinstance force
  3. fileencoding, utf-8
  4. settitlematchmode, 3
  5. stringcasesense, on
  6.  
  7. ; Initialization
  8. ascii_to_unicode := comobjcreate("Scripting.Dictionary")
  9. html_to_unicode := comobjcreate("Scripting.Dictionary")
  10. pattern_to_unicode := comobjcreate("Scripting.Dictionary")
  11. lower_to_upper := comobjcreate("Scripting.Dictionary")
  12. unicode_to_ascii := comobjcreate("Scripting.Dictionary")
  13.  
  14. ; Load
  15. load_dictionary("Text.csv")
  16.  
  17. ; Include guard
  18. goto, 70fabbc9-4cfb-443b-aca6-5434ebef765f
  19.  
  20. ; Include guard
  21. if (not (isfunc("ascii_to_unicode")))
  22. {
  23.     ; Convert ASCII to Unicode
  24.     ascii_to_unicode(text)
  25.     {
  26.         global ascii_to_unicode
  27.         for ascii in ascii_to_unicode
  28.         {
  29.             unicode := ascii_to_unicode.item(ascii)
  30.             stringreplace, text, text, %ascii%, %unicode%, all
  31.         }
  32.         global pattern_to_unicode
  33.         for pattern in pattern_to_unicode
  34.         {
  35.             text := regexreplace(text, pattern, pattern_to_unicode.item(pattern))
  36.         }
  37.         return html_to_unicode(text)
  38.     }
  39. }
  40.  
  41. ; Include guard
  42. if (not (isfunc("find_file")))
  43. {
  44.     ; Find a file in AutoHotkey path
  45.     find_file(name)
  46.     {
  47.         ; Check the file
  48.         if (not (   fileexist(file := A_ScriptDir . "\" . name)
  49.                 or  fileexist(file := A_WorkingDir . "\" . name)))
  50.         {
  51.             ; Debug
  52.             msgbox, 0, File not found, "%name%"`n`nA_ScriptDir`t"%A_ScriptDir%"`nA_WorkingDir`t"%A_WorkingDir%"
  53.             return
  54.         }
  55.         return file
  56.     }
  57. }
  58.  
  59. ; Include guard
  60. if (not (isfunc("first_to_upper")))
  61. {
  62.     ; Convert first character to upper case
  63.     first_to_upper(text)
  64.     {
  65.         global lower_to_upper
  66.         letter := substr(text, 1, 1)
  67.         if ("" == lower_to_upper.item(letter))
  68.         {
  69.             code := asc(letter)
  70.             if ((97 <= code) and (code <= 122))
  71.             {
  72.                 letter := chr(code - 32)
  73.             }
  74.         }
  75.         else
  76.         {
  77.             letter := lower_to_upper.item(letter)
  78.         }
  79.         return letter . substr(text, 2)
  80.     }
  81. }
  82.  
  83. ; Include guard
  84. if (not (isfunc("html_to_unicode")))
  85. {
  86.     ; Convert HTML to Unicode
  87.     html_to_unicode(text)
  88.     {
  89.         global html_to_unicode
  90.         position := 1
  91.         while (not (0 == (position := regexmatch(text, "&([^;]+);", entity, position))))
  92.         {
  93.             length := strlen(entity)
  94.             if (regexmatch(entity1, "^#([1-9][0-9]*)$", numeric))
  95.             {
  96.                 replace := chr(numeric1)
  97.             }
  98.             else if (regexmatch(entity1, "^#x([1-9a-fA-F][0-9a-fA-F]*)$", numeric))
  99.             {
  100.                 format := A_FormatInteger
  101.                 setformat, integer, dec
  102.                 numeric = 0x%numeric1%
  103.                 numeric += 0
  104.                 setformat, integer, %format%
  105.                 replace := chr(numeric)
  106.             }
  107.             else
  108.             {
  109.                 replace := html_to_unicode.item(entity1)
  110.             }
  111.             if ("" == replace)
  112.             {
  113.                 ++position
  114.             }
  115.             else
  116.             {
  117.                 text := substr(text, 1, position - 1) . replace . substr(text, position + length)
  118.                 position += strlen(replace)
  119.             }
  120.         }
  121.         return text
  122.     }
  123. }
  124.  
  125. ; Include guard
  126. if (not (isfunc("is_ascii")))
  127. {
  128.     ; Test text form ASCII
  129.     is_ascii(text)
  130.     {
  131.         loop, parse, text
  132.         {
  133.             if (127 < asc(A_LoopField))
  134.             {
  135.                 return false
  136.             }
  137.         }
  138.         return true
  139.     }
  140. }
  141.  
  142. ; Include guard
  143. if (not (isfunc("load_dictionary")))
  144. {
  145.     ; Load dictionary file
  146.     load_dictionary(name)
  147.     {
  148.         file := find_file(name)
  149.         global ascii_to_unicode, html_to_unicode, lower_to_upper, pattern_to_unicode, unicode_to_ascii
  150.         loop, read, %file%
  151.         {
  152.             if ("//" == substr(trim(A_LoopReadLine), 1, 2))
  153.             {
  154.                 ; Ignore comment
  155.             }
  156.             else if ("" == A_LoopReadLine)
  157.             {
  158.                 type :=
  159.             }
  160.             else if ("" == type)
  161.             {
  162.                 type := A_LoopReadLine
  163.                 if (not (("emoticon" == type) or ("html" == type) or ("name" == type) or ("regex" == type) or ("string" == type) or ("test" == type) or ("unicode" == type) or ("upper" == type) or ("word" == type)))
  164.                 {
  165.                     msgbox, 0, Malformed dictionary, Unknown type "%type%" on line %A_Index%.
  166.                     return
  167.                 }
  168.             }
  169.             else
  170.             {
  171.                 first :=
  172.                 loop, parse, A_LoopReadLine, csv
  173.                 {
  174.                     cell := html_to_unicode(A_LoopField)
  175.                     if ("" == first)
  176.                     {
  177.                         first := cell
  178.                         if ("name" == type)
  179.                         {
  180.                             load_pattern(first, first)
  181.                         }
  182.                         else if ("word" == type)
  183.                         {
  184.                             load_pattern(first, first)
  185.                             upper := first_to_upper(first)
  186.                             load_pattern(upper, upper)
  187.                         }
  188.                     }
  189.                     else if ("emoticon" == type)
  190.                     {
  191.                         emoticon = :%cell%:
  192.                         pattern = \B%emoticon%\B
  193.                         pattern_to_unicode.item(pattern) := first
  194.                         unicode_to_ascii.item(first) := emoticon
  195.                     }
  196.                     else if ("html" == type)
  197.                     {
  198.                         html_to_unicode.item(A_LoopField) := chr(first)
  199.                     }
  200.                     else if (("name" == type) or ("word" == type))
  201.                     {
  202.                         load_pattern(cell, first)
  203.                         load_pattern(first_to_upper(cell), first_to_upper(first))
  204.                     }
  205.                     else if ("regex" == type)
  206.                     {
  207.                         pattern_to_unicode.item(cell) := first
  208.                     }
  209.                     else if ("string" == type)
  210.                     {
  211.                         ascii_to_unicode.item(A_LoopField) := first
  212.                         unicode_to_ascii.item(first) := A_LoopField
  213.                     }
  214.                     else if ("test" == type)
  215.                     {
  216.                         load_test(cell, first)
  217.                     }
  218.                     else if ("unicode" == type)
  219.                     {
  220.                         unicode_to_ascii.item(cell) := first
  221.                     }
  222.                     else if ("upper" == type)
  223.                     {
  224.                         lower_to_upper.item(cell) := first
  225.                     }
  226.                 }
  227.             }
  228.         }
  229.     }
  230. }
  231.  
  232. ; Include guard
  233. if (not (isfunc("load_pattern")))
  234. {
  235.     ; Load a ASCII or Unicode regex pattern
  236.     load_pattern(source, target)
  237.     {
  238.         global unicode_to_ascii
  239.         for unicode in unicode_to_ascii
  240.         {
  241.             ascii := unicode_to_ascii.item(unicode)
  242.             if (2 == strlen(ascii) + strlen(unicode))
  243.             {
  244.                 stringreplace, source, source, %unicode%, [%ascii%%unicode%], all
  245.             }
  246.             else
  247.             {
  248.                 stringreplace, source, source, %unicode%, (?:%ascii%|%unicode%), all
  249.             }
  250.         }
  251.         if (not (source == target))
  252.         {
  253.             source = (*UCP)\b%source%\b
  254.             global pattern_to_unicode
  255.             pattern_to_unicode.item(source) := target
  256.         }
  257.     }
  258. }
  259.  
  260. ; Include guard
  261. if (not (isfunc("load_test")))
  262. {
  263.     ; Test ASCII or HTML to Unicode
  264.     load_test(source, target)
  265.     {
  266.         result := ascii_to_unicode(source)
  267.         reverse := unicode_to_ascii(result)
  268.         if (not ((result == target) and is_ascii(reverse) and (ascii_to_unicode(reverse) == target)))
  269.         {
  270.             source_length := strlen(A_LoopField)
  271.             source_encode := split_to_code(A_LoopField)
  272.             target_length := strlen(target)
  273.             target_encode := split_to_code(target)
  274.             result_length := strlen(result)
  275.             result_encode := split_to_code(result)
  276.             reverse_length := strlen(reverse)
  277.             reverse_encode := split_to_code(reverse)
  278.             msgbox, 0, Test failed, Source`t# %source_length%`t"%source%"`n%source_encode%`n`nTarget`t# %target_length%`t"%target%"`n%target_encode%`n`nResult`t# %result_length%`t"%result%"`n%result_encode%`n`nReverse`t# %reverse_length%`t"%reverse%"`n%reverse_encode%
  279.         }
  280.     }
  281. }
  282.  
  283. ; Include guard
  284. if (not (isfunc("send_key")))
  285. {
  286.     ; Send key and restore state
  287.     send_key(text)
  288.     {
  289.         ; Save key state
  290.         if ("1" == getkeystate("lctrl"))
  291.         {
  292.             status := status . "{lctrl}"
  293.         }
  294.         if ("1" == getkeystate("rctrl"))
  295.         {
  296.             status := status . "{rctrl}"
  297.         }
  298.         if ("1" == getkeystate("lalt"))
  299.         {
  300.             status := status . "{lalt}"
  301.         }
  302.         if ("1" == getkeystate("ralt"))
  303.         {
  304.             status := status . "{ralt}"
  305.         }
  306.         if ("1" == getkeystate("lshift"))
  307.         {
  308.             status := status . "{lshift}"
  309.         }
  310.         if ("1" == getkeystate("rshift"))
  311.         {
  312.             status := status . "{rshift}"
  313.         }
  314.  
  315.         ; Send key
  316.         send, {lctrl up}{rctrl up}{lalt up}{ralt up}{lshift up}{rshift up}
  317.         send, %text%
  318.         send, {lctrl up}{rctrl up}{lalt up}{ralt up}{lshift up}{rshift up}
  319.  
  320.         ; Restore key state
  321.         send, %status%
  322.     }
  323. }
  324.  
  325. ; Include guard
  326. if (not (isfunc("split_to_code")))
  327. {
  328.     ; Convert text to code point list
  329.     split_to_code(text)
  330.     {
  331.         loop, parse, text
  332.         {
  333.             if ("" == code)
  334.             {
  335.                 code := asc(A_LoopField)
  336.             }
  337.             else
  338.             {
  339.                 code := code . " " . asc(A_LoopField)
  340.             }
  341.         }
  342.         return code
  343.     }
  344. }
  345.  
  346. ; Include guard
  347. if (not (isfunc("text")))
  348. {
  349.     ; Process input line
  350.     text(encoding)
  351.     {
  352.         ; Check window
  353.         wingetclass, class, a
  354.         wingettitle, title, a
  355.         winget, process, ProcessName, %title%
  356.         if (false
  357.  
  358.         ; Explorer
  359.         or       (("explorer.exe" == process)
  360.         and   ("CabinetWClass" == class))
  361.  
  362.         ; MadEdit, without opened file
  363.         or       (("MadEdit.exe" == process)
  364.         and   ("wxWindowClassNR" == class)
  365.         and   ("MadEdit " == title))
  366.  
  367.         or false)
  368.         {
  369.             ; Ignore
  370.             return
  371.         }
  372.         else if (false
  373.  
  374.         ; Chrome
  375.         or     (("chrome.exe" == process)
  376.         and ("Chrome_WidgetWin_1" == class)
  377.         and regexmatch(title, " - Google Chrome$"))
  378.  
  379.         ; Comic Collector
  380.         or     (("" == process)
  381.         and         (("TfmComic" == class)
  382.             and (( regexmatch(title, "^Add Comic ")
  383.                 or regexmatch(title, "^Edit Comic: ")
  384.                 or regexmatch(title, "^Edit Multiple Comics")))
  385.             or      (("TfmSeriesLookupItem" == class)
  386.             and  ("Edit  Series" == title))
  387.             or      (("TfmSortLookupItem" == class)
  388.                 and  ("Edit  Location" == title))))
  389.  
  390.         ; Dota 2
  391.         or     (("dota.exe" == process)
  392.         and ("Valve001" == class)
  393.         and ("DOTA 2" == title))
  394.  
  395.         ; Dota 2 Reborn
  396.         or     (("dota2.exe" == process)
  397.         and ("SDL_app" == class)
  398.         and ("Dota 2" == title))
  399.  
  400.         ; Internet Explorer
  401.         or     (("IEXPLORE.EXE" == process)
  402.         and ("IEFrame" == class)
  403.         and regexmatch(title, " - Internet Explorer$"))
  404.  
  405.         ; Keepass
  406.         or     (("KeePass.exe" == process)
  407.         and ("#32770" == class)
  408.         and   (("Add Entry" == title)
  409.             or ("Add Group" == title)
  410.             or ("Edit Entry" == title)
  411.             or ("Save as Profile" == title)))
  412.  
  413.         ; Libre Office
  414.         or     (("soffice.bin" == process)
  415.         and ("SALFRAME" == class)
  416.         and regexmatch(title, " - LibreOffice Writer$"))
  417.  
  418.         ; Line
  419.         or     (("line.exe" == process)
  420.         and ("Qt5QWindowIcon" == class)
  421.         and ("LINE" == title))
  422.  
  423.         ; MadEdit, with opened file
  424.         or     (("MadEdit.exe" == process)
  425.         and ("wxWindowClassNR" == class)
  426.         and regexmatch(title, "^MadEdit - \[.+\] $"))
  427.  
  428.         ; mIRC
  429.         or     (("mirc.exe" == process)
  430.         and ("mIRC" == class)
  431.         and regexmatch(title, "^mIRC - "))
  432.  
  433.         ; Movie Collector
  434.         or     (("" == process)
  435.         and ("TfmMovie" == class)
  436.         and regexmatch(title, "^Edit Movie: "))
  437.  
  438.         ; MP3tag
  439.         or     (("Mp3tag.exe" == process)
  440.         and ("Afx:00400000:b:00010005:00000006:0FC00909" == class)
  441.         and regexmatch(title, "^Mp3tag v2\.72  -  "))
  442.  
  443.         ; Notepad
  444.         or           ((("notepad.exe" == process)
  445.             or     ("NOTEPAD.EXE" == process))
  446.         and      ((("Notepad" == class)
  447.             and regexmatch(title, " - Notepad$"))
  448.             or     (("#32770" == class)
  449.             and ("Find" == title))))
  450.  
  451.         ; RetroShare
  452.         or     (("RetroShare.exe" == process)
  453.         and ("QWidget" == class)
  454.         and regexmatch(title, "^RetroShare [0-9]\.[0-9]\.[0-9]x "))
  455.  
  456.         ; Run
  457.         or     (("Explorer.EXE" == process)
  458.         and ("#32770" == class)
  459.         and ("Run" == title))
  460.  
  461.         or false)
  462.         {
  463.             ; Continue
  464.         }
  465.         else if (false
  466.  
  467.         ; Samsung Scan Assistant
  468.         or     (("SAProc.exe" == process)
  469.         and ("#32770" == class)
  470.         and ("Save" == title))
  471.  
  472.         ; Skype
  473.         or     (("Skype.exe" == process)
  474.         and ("tSkMainForm" == class)
  475.         and regexmatch(title, html_to_unicode("^Skype&#8482;")))
  476.  
  477.         ; Spotify
  478.         or     (("Spotify.exe" == process)
  479.         and ("SpotifyMainWindow" == class)
  480.         and    (("Spotify" == title)
  481.             or  regexmatch(title, " - ")))
  482.  
  483.         ; StarCraft 2
  484.         or     (("" == process)
  485.         and ("StarCraft II" == class)
  486.         and ("StarCraft II" == title))
  487.  
  488.         ; Steam
  489.         or     (("Steam.exe" == process)
  490.         and     (("USurface_3873329" == class)
  491.             or   ("USurface_78552928" == class))
  492.         and     (("Steam" == title)
  493.             or   regexmatch(title, "^Friends - ")))
  494.  
  495.         ; TeamSpeak
  496.         or     (("ts3client_win64.exe" == process)
  497.         and ("Qt5QWindowIcon" == class)
  498.         and ("Contacts" == title))
  499.  
  500.         ; Tortoise Hg
  501.         or     (("thgw.exe" == process)
  502.         and ("QWidget" == class)
  503.         and regexmatch(title, " - TortoiseHg Workbench - "))
  504.  
  505.         ; Tortoise SVN
  506.         or     (("TortoiseProc.exe" == process)
  507.         and ("#32770" == class)
  508.         and regexmatch(title, " - Commit - TortoiseSVN$"))
  509.  
  510.         ; Trillian
  511.         or  (("trillian.exe" == process)
  512.         and   (("icoIRC" == class)
  513.             or ("icoYahoo" == class))
  514.  
  515.         ; Windows Apps
  516.         or     (("ApplicationFrameHost.exe" == process)
  517.         and ("ApplicationFrameWindow" == class)
  518.  
  519.         ; Windows Apps, Edge
  520.         and (regexmatch(title, html_to_unicode(" &lrm;- Microsoft Edge$"))
  521.  
  522.         ; Windows Apps, Facebook Messenger
  523.         or ("Messenger" == title)
  524.  
  525.         ; Windows Apps, Line
  526.         or ("LINE" == title)
  527.  
  528.         ; Windows Apps, Skype
  529.         or ("Skype" == title))))
  530.  
  531.         or false)
  532.         {
  533.             ; Continue
  534.         }
  535.         else
  536.         {
  537.             ; Debug
  538.             msgbox, 0, Unknown window, Process`t"%process%"`nClass`t"%class%"`nTitle`t"%title%"
  539.             return
  540.         }
  541.  
  542.         ; Read line
  543.         previous := clipboard
  544.         clipboard :=
  545.         send_key("{shift down}{home}{shift up}{ctrl down}c{ctrl up}")
  546.         clipwait, 5
  547.  
  548.         ; Replace content
  549.         if ("ascii" == encoding)
  550.         {
  551.             clipboard := unicode_to_ascii(clipboard)
  552.         }
  553.         else if ("unicode" == encoding)
  554.         {
  555.             clipboard := ascii_to_unicode(clipboard)
  556.         }
  557.         else
  558.         {
  559.             ; Debug
  560.             msgbox, 0, Unknown encoding, Encoding`t"%encoding%"
  561.             return
  562.         }
  563.  
  564.         ; Write line
  565.         send_key("{ctrl down}v{ctrl up}")
  566.         clipboard := previous
  567.     }
  568. }
  569.  
  570. ; Include guard
  571. if (not (isfunc("unicode_to_ascii")))
  572. {
  573.     ; Convert Unicode to ASCII
  574.     unicode_to_ascii(text)
  575.     {
  576.         global unicode_to_ascii
  577.         for unicode in unicode_to_ascii
  578.         {
  579.             ascii := unicode_to_ascii.item(unicode)
  580.             stringreplace, text, text, %unicode%, %ascii%, all
  581.         }
  582.         return text
  583.     }
  584. }
  585.  
  586. ; Ctrl + Space
  587. ^space::
  588. {
  589.     text("unicode")
  590. }
  591. return
  592.  
  593. ; Ctrl + Shift + Space
  594. ^+space::
  595. {
  596.     text("ascii")
  597. }
  598. return
  599.  
  600. ; Include guard
  601. 70fabbc9-4cfb-443b-aca6-5434ebef765f:
Advertisement