#Include generates special character error Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

#Include generates special character error

Post by Avastgard » 31 Jan 2023, 10:25

I have the following code:

Code: Select all

;
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         A.N.Other <myemail@nowhere.com>
;
; Script Function:
;	Template script (you can customize this template by editing "ShellNew\Template.ahk" in your Windows folder)
;

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;------------------------------------------------------------------------------
; CHANGELOG:
;
; 2021       : Script adapted for Brazilian Portuguese
; Sep 13 2007: Added more misspellings.
;              Added fix for -ign -> -ing that ignores words like "sign".
;              Added word beginnings/endings sections to cover more options.
;              Added auto-accents section for words like fiancée, naïve, etc.
; Feb 28 2007: Added other common misspellings based on MS Word AutoCorrect.
;              Added optional auto-correction of 2 consecutive capital letters.
; Sep 24 2006: Initial release by Jim Biancolo (http://www.biancolo.com)
;
; INTRODUCTION
;
; This is an AutoHotKey script that implements AutoCorrect against several
; "Lists of common misspellings":
;
; This does not replace a proper spellchecker such as in Firefox, Word, etc.
; It is usually better to have uncertain typos highlighted by a spellchecker
; than to "correct" them incorrectly so that they are no longer even caught by
; a spellchecker: it is not the job of an autocorrector to correct *all*
; misspellings, but only those which are very obviously incorrect.
;
; From a suggestion by Tara Gibb, you can add your own corrections to any
; highlighted word by hitting Win+H. These will be added to a separate file,
; so that you can safely update this file without overwriting your changes.
;
; Some entries have more than one possible resolution (achive->achieve/archive)
; or are clearly a matter of deliberate personal writing style (wanna, colour)
;
; These have been placed at the end of this file and commented out, so you can
; easily edit and add them back in as you like, tailored to your preferences.
;
; SOURCES
;
; http://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings
; http://en.wikipedia.org/wiki/Wikipedia:Typo
; Microsoft Office autocorrect list
; Script by jaco0646 http://www.autohotkey.com/forum/topic8057.html
; OpenOffice autocorrect list
; TextTrust press release
; User suggestions.
; http://www.autohotkey.com/download/AutoCorrect.ahk
; CONTENTS
;
;   Settings
;   AUto-COrrect TWo COnsecutive CApitals (commented out by default)
;   Win+H code
;   Fix for -ign instead of -ing
;   Word endings
;   Word beginnings
;   Accented English words
;   Common Misspellings - the main list
;   Ambiguous entries - commented out
;------------------------------------------------------------------------------


;------------------------------------------------------------------------------
; Settings
;------------------------------------------------------------------------------
#NoEnv ; For security
#SingleInstance force

;------------------------------------------------------------------------------
; AUto-COrrect TWo COnsecutive CApitals.
; Disabled by default to prevent unwanted corrections such as IfEqual->Ifequal.
; To enable it, remove the /*..*/ symbols around it.
; From Laszlo's script at http://www.autohotkey.com/forum/topic9689.html
;------------------------------------------------------------------------------
/*
; The first line of code below is the set of letters, digits, and/or symbols
; that are eligible for this type of correction.  Customize if you wish:
	keys = abcdefghijklmnopqrstuvwxyz
	Loop Parse, keys
		HotKey ~+%A_LoopField%, Hoty
	Hoty:
	CapCount := SubStr(A_PriorHotKey,2,1)="+" && A_TimeSincePriorHotkey<999 ? CapCount+1 : 1
	if CapCount = 2
		SendInput % "{BS}" . SubStr(A_ThisHotKey,3,1)
	else if CapCount = 3
		SendInput % "{Left}{BS}+" . SubStr(A_PriorHotKey,3,1) . "{Right}"
	Return
*/


;------------------------------------------------------------------------------
; Win+H to enter misspelling correction.  It will be added to this script.
;------------------------------------------------------------------------------
#h::

AutoTrim Off  ; Retain any leading and trailing whitespace on the clipboard.
ClipboardOld = %ClipboardAll%
Clipboard =  ; Must start off blank for detection to work.
Send ^c
ClipWait 1
if ErrorLevel  ; ClipWait timed out.
	return
; Replace CRLF and/or LF with `n for use in a "send-raw" hotstring:
; The same is done for any other characters that might otherwise
; be a problem in raw mode:
StringReplace, Hotstring, Clipboard, ``, ````, All  ; Do this replacement first to avoid interfering with the others below.
StringReplace, Hotstring, Hotstring, `r`n, ``r, All  ; Using `r works better than `n in MS Word, etc.
StringReplace, Hotstring, Hotstring, `n, ``r, All
StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All
StringReplace, Hotstring, Hotstring, `;, ```;, All
Clipboard = %ClipboardOld%  ; Restore previous contents of clipboard.
; This will move the InputBox's caret to a more friendly position:
SetTimer, MoveCaret, 10
; Show the InputBox, providing the default hotstring:
InputBox, Hotstring, Nova correção, Forneça a palavra correta no lado direito. É possível também editar o lado esquerdo.`n`nExemplo`n::dso::dos,,,,,,,,:c*:%Hotstring%::%Hotstring%

if ErrorLevel <> 0  ; The user pressed Cancel.
	return
; Otherwise, add the hotstring and reload the script:
FileAppend, `n%Hotstring%, %A_ScriptFullPath%  ; Put a `n at the beginning in case file lacks a blank line at its end.
Reload
Sleep 200 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 4,, The hotstring just added appears to be improperly formatted.  Would you like to open the script for editing? Note that the bad hotstring is at the bottom of the script.
IfMsgBox, Yes, Edit
	return

MoveCaret:
IfWinNotActive, New Hotstring
	return
; Otherwise, move the InputBox's insertion point to where the user will type the abbreviation.
Send {HOME}
Loop % StrLen(Hotstring) + 4
	SendInput {Right}
SetTimer, MoveCaret, Off
return

;#Hotstring R  ; Set the default to be "raw mode" (might not actually be relied upon by anything yet).

;------------------------------------------------------------------------------
; Trecho de script para corrigir fins de palavras
;------------------------------------------------------------------------------
:?:metne::mente
:?:metno::mento
:?:emtne::mente
:?:metnos::mentos
:?:çõa::ção
:?:çao::ção
:?:çoes::ções
:?:çãoes::ções
;
It's basically an autocorrect script. It works as intended as long as the hotstrings are in the same script. My actual script is a lot longer than this one, so I decided to split it into other scripts sorted by section to make the code cleaner. For that, I tried adding the following on line 147:

#Include C:\Users\username\AHK\script.ahk

For some reason, all special characters are being sent incorrecty. So for example, if the included script.ahk contains this hotstring: ::elabnot::ELABORAR NOTIFICAÇÃO OU CONTRANOTIFICAÇÃO, the output will be ELABORAR NOTIFICAÇÃO OU CONTRANOTIFICAÇÃO. But the hoststring works as intended if I paste it directly on the main script instead.

How can I fix this? Is it a limitation of #Include? I read the documentation and found no explicit mention to this issue.


Avastgard
Posts: 133
Joined: 30 Sep 2016, 21:54

Re: #Include generates special character error

Post by Avastgard » 31 Jan 2023, 11:32

I'm an idiot. Just a few weeks ago I pointed the exact same error on a user's script in Reddit. And for some reason I created scripts to be included as .txt files (which default to UTF-8) first that I later renamed to .ahk.

Thanks for the help, @mikeyww.

Post Reply

Return to “Ask for Help (v1)”