Wikipedia:AutoHotkey

A page for tips, and sharing macros, for Wikipedians who edit using AutoHotkey (AHK), a free, open-source custom scripting language for Microsoft Windows.

Anyone using AHK for large-scale editing or article creation {{Smallcaps|must}} comply with the policy at WP:BOTASSIST.

Typing aid

  • See http://pigsonthewing.org.uk/using-autohotkey-macros-make-typing-life-easier/

Example scripts

= Simple blank template =

::*:\ac::{{}{{}Authority control{}}{}} ; typing \ac (one character at a time) sends {{Authority control}}

= Complex template: Cite episode =

{{Collapse top|title=Complex template: Cite episode }}

:*:\ce::{{}{{}Cite episode |title= |series= |series-link= |url= |access-date= 2017- |network= |station= |date= |season= |series-no= |number= |transcript= |transcript-url= {}}{}}

Type: \ce to render:

{{Cite episode |title= |series= |series-link= |url= |access-date= 2017- |network= |station= |date= |season= |seriesno= |number= |transcript= |transcript-url= }}

{{Collapse bottom}}

= Keyboards with missing keys or characters needing the AltGr (right-Alt) key =

For a keyboard that needs the alternate graving AltGr (right-Alt) key to type the very frequently used characters "[", "]", "{", "}", and that has no key for "~", this script replaces the less used keys "ù", "µ", "ç", "à" with resp. "[", "]", "{", "}", and defines AltGr-s and AltGr-u to enter talk page signature, and an often used "warning template template":

{{collapse top|title=Keyboards with missing keys or characters needing the AltGr (right-Alt) key}}

:?C*:ù::{[} ; Typing ù sends [.

:?C*:µ::{]} ; Typing µ sends ].

:?C*:ç::{{} ; Typing ç sends {.

:?C*:à::{}} ; Typing à sends }.

<^>!s::~~~~ ; Typing AltGr+s sends signature

<^>!u:: ; Typing AltGr+u sends "{{subst:uw-|}} - ~~~~" and places cursor right after "uw-".

Send {}{{}{{}subst:uw-|{}}{}} - ~~~~{Left 10}

return

{{collapse bottom}}

= Today's date =

In YYYY-MM-DD format

{{collapse top|title=Today's date}}

:*:\td::

FormatTime, Time,, yyyy-MM-dd

Send %Time%

Return

Type \td to render the current day's date, in YYYY-MM-DD format.

{{collapse bottom}}

=Apostrophes, curved and Wikipedia-compliant =

To use the curved apostrophe {{char|{{serif|’}}}} generally except when editing Wikipedia (where this is prohibited by MOS:STRAIGHT), one can take advantage of the fact that the page title when editing Wikipedia articles always contains the word "Editing". This doesn't work, though, when editing talk pages using the Replying feature.

{{collapse top|title=Apostrophes, curved and Wikipedia-compliant}}

$'::

If WinActive("Editing")

SendInput, '

else

SendInput, ’

Return

{{collapse bottom}}

With this code in effect, however, some other technique is needed to produce the left single quotation mark {{char|{{serif|‘}}}}, used in America and Canada only for inner quotation marks; see {{section link|Quotation_marks_in_English#Single_nested_within_double,_or_vice_versa}}.

= BLP talk page headers =

For a new BLP article's talk page.

{{collapse top|title=BLP talk page headers}}

Note use of {enter} for a new line.

:*:\bp::{{}{{}talkheader{}}{}}{enter}{{}{{}WikiProjectBannerShell|1={enter}{{}{{}WikiProject Biography{enter}|class=stub{enter}|living= {enter}|listas= {enter}|needs-photo= yes{enter}|needs-infobox= yes{enter}{}}{}}{enter}{{}{{}WikiProject XXXX |class=stub |importance=low |needs-photo=yes |needs-infobox=yes{}}{}}{enter}{}}{}}

Type \bp to render:

{{talkheader}}

{{WikiProjectBannerShell|1=

{{WikiProject Biography

|class=stub

|living=

|listas=

|needs-photo= yes

|needs-infobox= yes

}}

{{WikiProject XXXX |class=stub |importance=low |needs-photo=yes |needs-infobox=yes}}

}}

Then complete as necessary.

{{collapse bottom}}

= Underlining in edit summaries =

Use of U+0332 COMBINING LOW LINE adds a nice u̲n̲d̲e̲r̲l̲i̲n̲e̲ to the preceding character in an edit summary. It has to be applied character by character (omitting characters with descenders), so sending it via AutoHotkey, e.g. with SendInput {U+0332} followed by SendInput {left}, makes inserting it much more practical (as well as more readable in the relevant text-entry box) than inserting &#x332; (or &#818;) throughout the relevant text. One starts at the end of a word to be underlined and presses the hotkey repeatedly until the entire word is underlined.

The approach also adds only one extra character per underlined character, rather than the five or six that would be required using &#818; or &#x332; toward the maximum length of an edit summary. See, for example, the edit summary at the right-hand side of [https://en.wikipedia.org/w/index.php?title=Cladogram&curid=48975&diff=988527576&oldid=961927157 this page], which would have been too long if '&#818;' had been used.

= Open Wikipedia watchlist =

:#w::Run https://en.wikipedia.org/wiki/Special:Watchlist ; typing Win+w (Windows key, and "w", at the same time) opens watchlist in default browser

You must be logged in.

= Search Wikipedia from clipboard =

:#s:: Run http://en.wikipedia.org/wiki/Special:Search?search=%clipboard% ; typing Win+s searches Wikipedia using the content of the clipboard

= Replace pieces of text in edit area by pressing F4 key =

{{collapse top|title=Text replacement}}

F4::CleanUpSelectedWikiCode()

CleanUpSelectedWikiCode()

{

; Save original clipboard contents.

originalClipboardContents:= ClipboardAll

; Empty the clipboard to prepare for ClipWait.

Clipboard:= ""

; Select all text.

Send ^a

; Copy selected text, making sure it's still there if CleanupWikicode() fails.

Send ^c

; Wait up to half a second for the clipboard to contain data,

; i.e. for the Copy command to execute.

ClipWait 0.5 ; Seconds.

if OK() {

; We have text on the clipboard.

; Clean up the code on the clipboard and put the result back on the clipboard.

Clipboard:= CleanupWikicode(Clipboard)

; Paste the new text back, replacing the original text.

Send ^v

; Wait for the paste to execute so we don't restore the

; clipboard before the text was actually pasted.

; Nothing is lost if we sleep too shortly, as that would just leave

; everything the way it was before, but no replacements would

; be done either.

Sleep 300 ; Milliseconds. Increase this on slow computers.

; Restore original clipboard contents.

Clipboard:= originalClipboardContents

} else

MsgBox % "CleanUpSelectedWikiCode: Didn't find any text to work on."

}

CleanupWikicode(txt) {

; Define your replacements here, returning the final result.

; Sample replacement:

; Replace
and
with
as used in HTML5

txt := StrReplace(txt, "
", "
")

txt := StrReplace(txt, "
", "
")

; Sample replacement with regex:

; Use regular expression to convert article to

; article

; fixes part of https://en.wikipedia.org/wiki/Special:LintErrors/tidy-font-bug

txt := RegExReplace(txt

, "i)\[{2}([^\|]+)\|([^\]]+)\]{2}"

, "$3")

return txt

}

OK()

{

return ErrorLevel == 0

}

Press F4 in edit area to replace defined strings.

{{collapse bottom}}