Jump to content

Wikipedia:AutoHotkey

From Wikipedia, the free encyclopedia

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 must comply with the policy at WP:BOTASSIST.

Typing aid

[edit]

Example scripts

[edit]

Simple blank template

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

Complex template: Cite episode

[edit]
Complex template: Cite episode
:*:\ce::<ref name= "">{{}{{}Cite episode |title=  |series=  |series-link=  |url=  |access-date=  2017- |network=  |station=  |date=  |season=  |series-no=  |number=  |transcript=  |transcript-url=  {}}{}}</ref>

Type: \ce to render:

<ref name= "">{{Cite episode |title=  |series=  |series-link=  |url=  |access-date=  2017- |network=  |station=  |date=  |season=  |seriesno=  |number=  |transcript=  |transcript-url=  }}</ref>

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

[edit]

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":

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

Today's date

[edit]

In YYYY-MM-DD format

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.

Apostrophes, curved and Wikipedia-compliant

[edit]

To use the curved apostrophe 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.

Apostrophes, curved and Wikipedia-compliant
$':: 
If WinActive("Editing") 
        SendInput, '
else			
        SendInput, 
Return

With this code in effect, however, some other technique is needed to produce the left single quotation mark , used in America and Canada only for inner quotation marks; see Quotation marks in English § Single nested within double, or vice versa.

BLP talk page headers

[edit]

For a new BLP article's talk page.

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.

Underlining in edit summaries

[edit]

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 this page, which would have been too long if '&#818;' had been used.

Open Wikipedia watchlist

[edit]
#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

[edit]
#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

[edit]
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 <br /> and <br/> with <br> as used in HTML5
	txt := StrReplace(txt, "<br />", "<br>")
	txt := StrReplace(txt, "<br/>", "<br>")

	; Sample replacement with regex:
	; Use regular expression to convert [[Article link|<span style="color:red;">article</span>]] to
	; [[Article link|<span style="red">article</span>]]
	; fixes part of https://wiki.riteme.site/wiki/Special:LintErrors/tidy-font-bug
	txt := RegExReplace(txt
		, "i)<font color=""([^""]+)"">\[{2}([^\|]+)\|([^\]]+)\]{2}</font>"
		, "[[$2|<span style=""color:$1"">$3</span>]]")

	return txt
}

OK()
{
	return ErrorLevel == 0
}

Press F4 in edit area to replace defined strings.

[edit]