-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHotstrings.htm
211 lines (201 loc) · 26.9 KB
/
Hotstrings.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!DOCTYPE HTML>
<html lang="es">
<head>
<title>Hotstrings - Definition & Usage | AutoHotkey</title>
<meta name="description" content="Learn details about hotstrings in general, ending characters, options, long replacements, context-sensitive hotstrings, function hotstrings, etc." />
<meta name="keywords" content="auto-replace,autotext,autocorrect,auto text,auto correct,abbreviation expansion,abbreviation,abbreviations,autoreplace,auto replace,text,expander,free,type,typing,word,words">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="static/theme.css" rel="stylesheet" type="text/css" />
<script src="static/content.js" type="text/javascript"></script>
</head>
<body>
<h1>Hotstrings</h1>
<h2 id="toc">Tabla de contenidos</h2>
<ul>
<li><a href="#intro">Introduction and Simple Examples</a></li>
<li><a href="#EndChars">Ending Characters</a></li>
<li><a href="#Options">Options</a></li>
<li><a href="#continuation">Long Replacements</a></li>
<li><a href="#variant">Context-sensitive Hotstrings</a></li>
<li><a href="#AutoCorrect">AutoCorrect</a></li>
<li><a href="#remarks">Remarks</a></li>
<li><a href="#Function">Function Hotstrings</a> <span class="ver">[v1.1.28+]</span></li>
<li><a href="#Helper">Hotstring Helper</a></li>
</ul>
<h2 id="intro">Introduction and Simple Examples</h2>
<p>Although hotstrings are mainly used to expand abbreviations as you type them (auto-replace), they can also be used to launch any scripted action. In this respect, they are similar to <a href="Hotkeys.htm">hotkeys</a> except that they are typically composed of more than one character (that is, a string).</p>
<p>To define a hotstring, enclose the triggering abbreviation between pairs of colons as in this example:</p>
<pre>::btw::by the way</pre>
<p>In the above example, the abbreviation btw will be automatically replaced with "by the way" whenever you type it (however, by default you must type an <a href="#EndChars">ending character</a> after typing btw, such as <kbd>Space</kbd>, <kbd>.</kbd>, or <kbd>Enter</kbd>).</p>
<p id="auto">The "by the way" example above is known as an auto-replace hotstring because the typed text is automatically erased and replaced by the string specified after the second pair of colons. By contrast, a hotstring may also be defined to perform any custom action as in the following examples. Note that the commands must appear <u>beneath</u> the hotstring:</p>
<pre>::ap::
MsgBox You typed "btw".
return
:*:]d:: <em>; This hotstring replaces "]d" with the current date and time via the commands below.</em>
<a href="lib/FormatTime.htm">FormatTime</a>, CurrentDateTime,, M/d/yyyy h:mm tt <em>; It will look like 9/1/2005 3:53 PM</em>
SendInput %CurrentDateTime%
return</pre>
<p>Even though the two examples above are not auto-replace hotstrings, the abbreviation you type is erased by default. This is done via automatic backspacing, which can be disabled via the <a href="#b0">b0 option</a>.</p>
<h2 id="EndChars">Ending Characters</h2>
<p>Unless the <a href="#Asterisk">asterisk option</a> is in effect, you must type an <em>ending character</em> after a hotstring's abbreviation to trigger it. Ending characters initially consist of the following: <strong>-()[]{}':;"/\,.?!`n `t</strong> (note that `n is <kbd>Enter</kbd>, `t is <kbd>Tab</kbd>, and there is a plain space between `n and `t). This set of characters can be changed by editing the following example, which sets the new ending characters for <u>all</u> hotstrings, not just the ones beneath it:</p>
<pre>#Hotstring EndChars -()[]{}:;'"/\,.?!`n `t</pre>
<p><span class="ver">[v1.1.28+]:</span> The ending characters can be changed while the script is running by calling the <a href="lib/Hotstring.htm">Hotstring</a> function as demonstrated below:</p>
<pre>Hotstring("EndChars", "-()[]{}:;")</pre>
<h2 id="Options">Options</h2>
<p>A hotstring's default behavior can be changed in two possible ways:</p>
<ol>
<li>The <a href="lib/_Hotstring.htm">#Hotstring</a> directive, which affects all hotstrings physically beneath that point in the script. The following example puts the C and R options into effect: <code>#Hotstring <strong>c r</strong></code>.</li>
<li>Putting options inside a hotstring's first pair of colons. The following example puts the C and * options (case sensitive and "ending character not required") into effect for a single hotstring: <code>:<strong>c*</strong>:j@::[email protected]</code>.</li>
</ol>
<p>The list below describes each option. When specifying more than one option using the methods above, spaces optionally may be included between them.</p>
<p id="Asterisk"><strong>*</strong> (asterisk): An ending character (e.g. <kbd>Space</kbd>, <kbd>.</kbd>, or <kbd>Enter</kbd>) is not required to trigger the hotstring. Por ejemplo:</p>
<pre>:*:j@::[email protected]</pre>
<p>The example above would send its replacement the moment you type the @ character. When using the <a href="lib/_Hotstring.htm">#Hotstring directive</a>, use <strong>*0</strong> to turn this option back off.</p>
<p id="Question"><strong>?</strong> (question mark): The hotstring will be triggered even when it is inside another word; that is, when the character typed immediately before it is alphanumeric. For example, if <code>:?:al::airline</code> is a hotstring, typing "practical " would produce "practicairline ". Use <strong>?0</strong> to turn this option back off.</p>
<p id="b0"><strong>B0</strong> (B followed by a zero): Automatic backspacing is <u>not</u> done to erase the abbreviation you type. Use a plain <strong>B</strong> to turn backspacing back on after it was previously turned off. A script may also do its own backspacing via {bs 5}, which sends <kbd>Backspace</kbd> five times. Similarly, it may send <kbd>←</kbd> five times via {left 5}. For example, the following hotstring produces "<em></em>" and moves the caret 5 places to the left (so that it's between the tags):</p>
<pre>:*b0:<em>::</em>{left 5}</pre>
<p id="C"><strong>C</strong>: Case sensitive: When you type an abbreviation, it must exactly match the case defined in the script. Use <strong>C0</strong> to turn case sensitivity back off.</p>
<p id="C1"><strong>C1</strong>: Do not conform to typed case. Use this option to make <a href="#auto">auto-replace hotstrings</a> case insensitive and prevent them from conforming to the case of the characters you actually type. Case-conforming hotstrings (which are the default) produce their replacement text in all caps if you type the abbreviation in all caps. If you type the first letter in caps, the first letter of the replacement will also be capitalized (if it is a letter). If you type the case in any other way, the replacement is sent exactly as defined. When using the <a href="lib/_Hotstring.htm">#Hotstring directive</a>, <strong>C0</strong> can be used to turn this option back off, which makes hotstrings conform again.</p>
<p id="Kn"><strong>Kn</strong>: Key-delay: This rarely-used option sets the delay between keystrokes produced by auto-backspacing or <a href="#auto">auto-replacement</a>. Specify the new delay for <strong>n</strong>; for example, specify k10 to have a 10ms delay and k-1 to have no delay. The exact behavior of this option depends on which <a href="#SendMode">sending mode</a> is in effect:</p>
<ul>
<li>SI (SendInput): Key-delay is ignored because a delay is not possible in this mode. The exception to this is when SendInput is <a href="lib/Send.htm#SendInputUnavail">unavailable</a>, in which case hotstrings revert to SendPlay mode below (which does obey key-delay).</li>
<li>SP (SendPlay): A delay of length zero is the default, which for SendPlay is the same as -1 (no delay). In this mode, the delay is actually a <a href="lib/SetKeyDelay.htm#dur">PressDuration</a> rather than a delay between keystrokes.</li>
<li>SE (SendEvent): A delay of length zero is the default. Zero is recommended for most purposes since it is fast but still cooperates well with other processes (due to internally doing a <a href="lib/Sleep.htm">Sleep 0</a>). Specify k-1 to have no delay at all, which is useful to make auto-replacements faster if your CPU is frequently under heavy load. When set to -1, a script's process-priority becomes an important factor in how fast it can send keystrokes. To raise a script's priority, use <code><a href="lib/Process.htm">Process</a>, Priority,, High</code>.</li>
</ul>
<p id="O"><strong>O</strong>: Omit the ending character of <a href="#auto">auto-replace hotstrings</a> when the replacement is produced. This is useful when you want a hotstring to be kept unambiguous by still requiring an ending character, but don't actually want the ending character to be shown on the screen. For example, if <code>:o:ar::aristocrat</code> is a hotstring, typing "ar" followed by the spacebar will produce "aristocrat" with no trailing space, which allows you to make the word plural or possessive without having to press <kbd>Backspace</kbd>. Use <strong>O0</strong> (the letter O followed by a zero) to turn this option back off.</p>
<p id="Pn"><strong>Pn</strong>: The <a href="misc/Threads.htm">priority</a> of the hotstring (e.g. P1). This rarely-used option has no effect on <a href="#auto">auto-replace hotstrings</a>.</p>
<p id="raw"><strong>R</strong>: Send the replacement text <a href="lib/Send.htm#SendRaw">raw</a>; that is, without translating {Enter} to <kbd>Enter</kbd>, ^c to <kbd>Ctrl</kbd>+<kbd>C</kbd>, etc. This option is put into effect automatically for hotstrings that have a <a href="#continuation">continuation section</a>. Use <strong>R0</strong> to turn this option back off.</p>
<p class="note"><strong>Note:</strong> <a href="#T">Text mode</a> may be more reliable. The R and T options are mutually exclusive.</p>
<p id="SendMode"><strong>SI</strong> or <strong>SP</strong> or <strong>SE</strong> <span class="ver">[v1.0.43+]:</span> Sets the method by which <a href="#auto">auto-replace hotstrings</a> send their keystrokes. These options are mutually exclusive: only one can be in effect at a time. The following describes each option:</p>
<ul>
<li>SI stands for <a href="lib/Send.htm#SendInputDetail">SendInput</a>, which typically has superior speed and reliability than the other modes. Another benefit is that like SendPlay below, SendInput postpones anything you type during a hotstring's <a href="#auto">auto-replacement text</a>. This prevents your keystrokes from being interspersed with those of the replacement. When SendInput is <a href="lib/Send.htm#SendInputUnavail">unavailable</a>, hotstrings automatically use SendPlay instead.</li>
<li>SP stands for <a href="lib/Send.htm#SendPlayDetail">SendPlay</a>, which may allow hotstrings to work in a broader variety of games.</li>
<li>SE stands for <a href="lib/Send.htm#SendEvent">SendEvent</a>, which is the default in versions older than 1.0.43.</li>
</ul>
<p>If none of the above options are used, the default mode in <span class="ver">[v1.0.43]</span> and later is SendInput. However, unlike the SI option, SendEvent is used instead of SendPlay when SendInput is unavailable.</p>
<p id="T"><strong>T</strong> <span class="ver">[v1.1.27+]</span>: Send the replacement text <a href="#raw">raw</a>, without translating each character to a keystroke. For details, see <a href="lib/Send.htm#SendText">Text mode</a>. Use <strong>T0</strong> or <strong>R0</strong> to turn this option back off, or override it with <strong>R</strong>.</p>
<p id="X"><strong>X</strong> <span class="ver">[v1.1.28+]</span>: Execute. Instead of replacement text, the hotstring accepts a command or expression to execute. For example, <code>:X:~mb::MsgBox</code> would cause a message box to be displayed when the user types "~mb" instead of auto-replacing it with the word "MsgBox". This is most useful when defining a large number of hotstrings which call functions, as it would otherwise require three lines per hotstring.</p>
<p>When used with the <a href="lib/Hotstring.htm">Hotstring</a> function, the X option causes the <em>Replacement</em> parameter to be interpreted as a label or function name instead of replacement text. However, the X option has this effect only if it is specified each time the function is called.</p>
<p id="z"><strong>Z</strong>: This rarely-used option resets the hotstring recognizer after each triggering of the hotstring. In other words, the script will begin waiting for an entirely new hotstring, eliminating from consideration anything you previously typed. This can prevent unwanted triggerings of hotstrings. To illustrate, consider the following hotstring:</p>
<pre>:b0*?:11::
SendInput xx
return</pre>
<p>Since the above lacks the Z option, typing 111 (three consecutive 1's) would trigger the hotstring twice because the middle 1 is the <em>last</em> character of the first triggering but also the <em>first</em> character of the second triggering. By adding the letter Z in front of b0, you would have to type four 1's instead of three to trigger the hotstring twice. Use <strong>Z0</strong> to turn this option back off.</p>
<h2 id="continuation">Long Replacements</h2>
<p>Hotstrings that produce a large amount of replacement text can be made more readable and maintainable by using a <a href="Scripts.htm#continuation">continuation section</a>. Por ejemplo:</p>
<pre>::text1::
(
Any text between the top and bottom parentheses is treated literally, including commas and percent signs.
By default, the hard carriage return (Enter) between the previous line and this one is also preserved.
By default, the indentation (tab) to the left of this line is preserved.
)</pre>
<p>See <a href="Scripts.htm#continuation">continuation section</a> for how to change these default behaviors. The presence of a continuation section also causes the hotstring to default to <a href="#raw">raw mode</a>. The only way to override this special default is to specify the <a href="#raw">r0 option</a> in each hotstring that has a continuation section (e.g. <code>:r0:text1::</code>).</p>
<h2 id="variant">Context-sensitive Hotstrings</h2>
<p>The directives <a href="lib/_IfWinActive.htm">#IfWinActive/Exist</a> can be used to make selected hotstrings context sensitive. Such hotstrings send a different replacement, perform a different action, or do nothing at all depending on the type of window that is active or exists. Por ejemplo:</p>
<pre>#IfWinActive ahk_class Notepad
::btw::This replacement text will appear only in Notepad.
#IfWinActive
::btw::This replacement text appears in windows other than Notepad.</pre>
<h2 id="AutoCorrect">AutoCorrect</h2>
<p>The following script uses hotstrings to correct about 4700 common English misspellings on-the-fly. It also includes a <kbd>Win</kbd>+<kbd>H</kbd> hotkey to make it easy to add more misspellings:</p>
<p>Download: <a href="https://www.autohotkey.com/download/AutoCorrect.ahk">AutoCorrect.ahk</a> (127 KB)</p>
<p>Author: <a href="http://www.biancolo.com/blog/autocorrect/">Jim Biancolo</a> and <a href="https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings">Wikipedia's Lists of Common Misspellings</a></p>
<h2 id="remarks">Remarks</h2>
<p>Variable references such as <code>%MyVar%</code> are not currently supported within the replacement text. To work around this, don't make such hotstrings <a href="#auto">auto-replace</a>. Instead, use the <a href="lib/Send.htm#SendInput">SendInput</a> command beneath the abbreviation, followed by a line containing only the word Return.</p>
<p>To send an extra space or tab after a replacement, include the space or tab at the end of the replacement but make the last character an accent/backtick (`). Por ejemplo:</p>
<pre>:*:btw::By the way `</pre>
<p id="NoMouse">By default, any click of the left or right mouse button will reset the hotstring recognizer. In other words, the script will begin waiting for an entirely new hotstring, eliminating from consideration anything you previously typed (if this is undesirable, specify the line <code><a href="lib/_Hotstring.htm">#Hotstring</a> NoMouse</code> anywhere in the script). This "reset upon mouse click" behavior is the default because each click typically moves the text insertion point (caret) or sets keyboard focus to a new control/field. In such cases, it is usually desirable to: 1) fire a hotstring even if it lacks the <a href="#Question">question mark option</a>; 2) prevent a firing when something you type after clicking the mouse accidentally forms a valid abbreviation with what you typed before.</p>
<p id="focus-reset">The hotstring recognizer checks the active window each time a character is typed, and resets if a different window is active than before. If the active window changes but reverts before any characters are typed, the change is not detected (but the hotstring recognizer may be reset for some other reason). The hotstring recognizer can also be reset by calling <code><a href="lib/Hotstring.htm#Reset">Hotstring("Reset")</a></code>.</p>
<p>The built-in variable <strong>A_EndChar</strong> contains the ending character that you typed to trigger the most recent non-auto-replace hotstring. If no ending character was required (due to the <a href="#Asterisk">* option</a>), it will be blank. A_EndChar is useful when making hotstrings that use the Send command or whose behavior should vary depending on which ending character you typed. To send the ending character itself, use <code>SendRaw %A_EndChar%</code> (<a href="lib/Send.htm">SendRaw</a> is used because characters such as !{} would not be sent correctly by the normal Send command).</p>
<p>Although commas, percent signs, and single-colons within hotstring definitions do not need to be <a href="misc/EscapeChar.htm">escaped</a>, backticks and those semicolons having a space or tab to their left require it. See <a href="misc/EscapeChar.htm">Escape Sequences</a> for a complete list.</p>
<p>Although the <a href="lib/Send.htm">Send command</a>'s special characters such as {Enter} are supported in <a href="#auto">auto-replacement text</a> (unless the <a href="#raw">raw option</a> is used), the hotstring abbreviations themselves do not use this. Instead, specify `n for <kbd>Enter</kbd> and `t (or a literal tab) for <kbd>Tab</kbd> (see <a href="misc/EscapeChar.htm">Escape Sequences</a> for a complete list). For example, the hotstring <code>:*:ab`t::</code> would be triggered when you type "ab" followed by a tab.</p>
<p>Spaces and tabs are treated literally within hotstring definitions. For example, the following would produce two different results: <code>::btw::by the way</code> and <code>::btw:: by the way</code>.</p>
<p>Each hotstring abbreviation can be no more than 40 characters long. The program will warn you if this length is exceeded. By contrast, the length of hotstring's replacement text is limited to about 5000 characters when the <a href="#SendMode">sending mode</a> is at its default of SendInput. That limit can be increased to 16,383 characters by switching to one of the other <a href="#SendMode">sending modes</a>. Furthermore, an unlimited amount of text can be sent by using <code><a href="lib/Send.htm#SendPlayDetail">SendPlay</a> %MyVariable%</code> in the body of the hotstring.</p>
<p>The order in which hotstrings are defined determines their precedence with respect to each other. In other words, if more than one hotstring matches something you type, only the one listed first in the script will take effect. Related topic: <a href="#variant">context-sensitive hotstrings</a>.</p>
<p>Any backspacing you do is taken into account for the purpose of detecting hotstrings. However, the use of <kbd>↑</kbd>, <kbd>→</kbd>, <kbd>↓</kbd>, <kbd>←</kbd>, <kbd>PgUp</kbd>, <kbd>PgDn</kbd>, <kbd>Home</kbd>, and <kbd>End</kbd> to navigate within an editor will cause the hotstring recognition process to reset. In other words, it will begin waiting for an entirely new hotstring.</p>
<p>A hotstring may be typed even when the active window is ignoring your keystrokes. In other words, the hotstring will still fire even though the triggering abbreviation is never visible. In addition, you may still press <kbd>Backspace</kbd> to undo the most recently typed keystroke (even though you can't see the effect).</p>
<p id="label">It is possible to <a href="lib/Gosub.htm">Gosub</a> or <a href="lib/Goto.htm">Goto</a> a hotstring label by including its first pair of colons (including any option symbols) in front of its name. Por ejemplo: <code>Gosub ::xyz</code>. However, jumping to a <a href="#auto">single-line (auto-replace) hotstring</a> will do nothing other than execute a <a href="lib/Return.htm">return</a>.</p>
<p>Although hotstrings are not monitored and will not be triggered during the course of an invisible <a href="lib/Input.htm">Input</a> command, visible Inputs are capable of triggering them.</p>
<p id="InputLevel">By default, hotstrings are never triggered by keystrokes produced by any AutoHotkey script. This avoids the possibility of an infinite loop where hotstrings trigger each other over and over. In <span class="ver">[v1.1.06]</span> and later, this behaviour can be controlled with <a href="lib/_InputLevel.htm">#InputLevel</a> and <a href="lib/SendLevel.htm">SendLevel</a>. However, auto-replace hotstrings always use send level 0 and therefore never trigger <a href="lib/_UseHook.htm">hook hotkeys</a> or hotstrings.</p>
<p><span class="ver">[v1.1.28+]:</span> Hotstrings can be created dynamically by means of the <a href="lib/Hotstring.htm">Hotstring</a> function, which can also modify, disable, or enable the script's existing hotstrings individually.</p>
<p>The <a href="lib/Input.htm">Input</a> command is more flexible than hotstrings for certain purposes. For example, it allows your keystrokes to be invisible in the active window (such as a game). It also supports non-character ending keys such as <kbd>Esc</kbd>.</p>
<p>The <a href="lib/_InstallKeybdHook.htm">keyboard hook</a> is automatically used by any script that contains hotstrings.</p>
<p>Hotstrings behave identically to hotkeys in the following ways:</p>
<ul>
<li>They are affected by the <a href="lib/Suspend.htm">Suspend</a> command.</li>
<li>They obey <a href="lib/_MaxThreads.htm">#MaxThreads</a> and <a href="lib/_MaxThreadsPerHotkey.htm">#MaxThreadsPerHotkey</a> (but not <a href="lib/_MaxThreadsBuffer.htm">#MaxThreadsBuffer</a>).</li>
<li>Scripts containing hotstrings are automatically <a href="lib/_Persistent.htm">persistent</a>.</li>
<li>Non-auto-replace hotstrings will create a new <a href="misc/Threads.htm">thread</a> when launched. In addition, they will update the built-in hotkey variables such as <a href="Variables.htm#ThisHotkey">A_ThisHotkey</a>.</li>
</ul>
<p>Known limitation: On some systems in Java applications, hotstrings might interfere with the user's ability to type diacritical letters (via dead keys). To work around this, <a href="lib/Suspend.htm">Suspend</a> can be turned on temporarily (which disables all hotstrings).</p>
<h2 id="Function">Function Hotstrings <span class="ver">[v1.1.28+]</span></h2>
<p>One or more hotstrings can be assigned a <a href="Functions.htm">function</a> by simply defining it immediately after the hotstring label, as in this example:</p>
<pre><em>; This example also demonstrates one way to implement case conformity in a script.</em>
:C:BTW:: <em>; Typed in all-caps.</em>
:C:Btw:: <em>; Typed with only the first letter upper-case.</em>
: :btw:: <em>; Typed in any other combination.</em>
case_conform_btw() {
hs := A_ThisHotkey <em>; For convenience and in case we're interrupted.</em>
if (hs == ":C:BTW")
Send BY THE WAY
else if (hs == ":C:Btw")
Send By the way
else
Send by the way
}
</pre>
<p>For additional details, see <a href="Hotkeys.htm#function-details">Function Hotkeys</a>.</p>
<p>The <a href="lib/Hotstring.htm">Hotstring</a> function can also be used to assign a function or function object to a hotstring.</p>
<h2 id="Helper">Hotstring Helper</h2>
<p>Andreas Borutta suggested the following script, which might be useful if you are a heavy user of hotstrings. By pressing <kbd>Win</kbd>+<kbd>H</kbd> (or another hotkey of your choice), the currently selected text can be turned into a hotstring. For example, if you have "by the way" selected in a word processor, pressing <kbd>Win</kbd>+<kbd>H</kbd> will prompt you for its abbreviation (e.g. btw) and then add the new hotstring to the script. It will then reload the script to activate the hotstring.</p>
<p class="note"><strong>Note:</strong> The <a href="lib/Hotstring.htm">Hotstring</a> function can be used to create new hotstrings without reloading. Take a look at the <a href="lib/Hotstring.htm#ExHelper">first example</a> in the example section of the function's page to see how this could be done.</p>
<pre class="NoIndent">#h:: <em>; Win+H hotkey
; Get the text currently selected. The clipboard is used instead of
; "ControlGet Selected" because it works in a greater variety of editors
; (namely word processors). Save the current clipboard contents to be
; restored later. Although this handles only plain text, it seems better
; than nothing:</em>
AutoTrim Off <em>; Retain any leading and trailing whitespace on the clipboard.</em>
ClipboardOld := ClipboardAll
Clipboard := "" <em>; Must start off blank for detection to work.</em>
Send ^c
ClipWait 1
if ErrorLevel <em>; ClipWait timed out.</em>
return
<em>; 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:</em>
StringReplace, Hotstring, Clipboard, ``, ````, All <em>; Do this replacement first to avoid interfering with the others below.</em>
StringReplace, Hotstring, Hotstring, `r`n, ``n, All
StringReplace, Hotstring, Hotstring, `n, ``n, All
StringReplace, Hotstring, Hotstring, %A_Tab%, ``t, All
StringReplace, Hotstring, Hotstring, `;, ```;, All
Clipboard := ClipboardOld <em>; Restore previous contents of clipboard.
; This will move the input box's caret to a more friendly position:</em>
SetTimer, MoveCaret, 10
<em>; Show the input box, providing the default hotstring:</em>
InputBox, Hotstring, New Hotstring, Type your abreviation at the indicated insertion point. You can also edit the replacement text if you wish.`n`nExample entry: :R:btw`::by the way,,,,,,,, :R:`::%Hotstring%
if ErrorLevel <em>; The user pressed Cancel.</em>
return
if InStr(Hotstring, ":R`:::")
{
MsgBox You didn't provide an abbreviation. The hotstring has not been added.
return
}
<em>; Otherwise, add the hotstring and reload the script:</em>
FileAppend, `n%Hotstring%, %A_ScriptFullPath% <em>; Put a `n at the beginning in case file lacks a blank line at its end.</em>
Reload
Sleep 200 <em>; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.</em>
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:
if not WinActive("New Hotstring")
return
<em>; Otherwise, move the input box's insertion point to where the user will type the abbreviation.</em>
Send {Home}{Right 3}
SetTimer, MoveCaret, Off
return</pre>
</body>
</html>