A handy Notepad script

After answering someone asking online for useful macros and scripts using AutoHotKey, it appears a little trick I’ve been using for years could be useful to others.

I tend to keep Notepad open most of the day for little tasks like de-formatting text or taking notes, but it’s annoying to bring up when I’m already typing. So I made a little script to do it without reaching for my mouse or alt-tabbing around.

When I hit tilde (`), the script launches Notepad with a new, untitled plaintext document if there isn’t one already. If I’m in another window or application, it will switch focus to that document. And if I’m already focused on Notepad, it will close the document (after confirmation). That’s all! It’s very simple and very useful. Maybe you can use it too. (Code after the fold.)

Just put the following code into your AHK script file:

`::
IfWinActive Untitled - Notepad
	WinClose
else
IfWinActive *Untitled - Notepad
	WinClose
else
IfWinExist Untitled - Notepad
	WinActivate
else
IfWinExist *Untitled - Notepad
	WinActivate
else
	Run Notepad
return

I don’t know if including the * variations matters because I don’t know about wildcard rules in AHK and Windows naming conventions, but anyhow it works.

That’s it — and literally the only script I run in AHK. Of course you can’t use tilde, but you can easily substitute another key or combination.