Life can be much easier thanks to this Windows tool.

updated …at some nice evening of October 26 🙂 (9:44 ᴘ.ᴍ.):
new script: Quote anything the smart way
updated: Insert Your Own flexible timestamp everywhere.

AutoHotkey resembles a construction set, with the help of which You can design from scratch various mechanisms to make Your life easier. You could summarize Its bottom line the following way:

  1. Recall some action which You often perform under Windows (it may be some ordinary thing, like searching some phrase via Google).
  2. Describe the action in the AutoHotkey way (it has its own scripting language, don’t worry about the code – there are many ready-to-use examples out there),
  3. Ascribe a keyboard shortcut to it and that’s it: from this moment forward, instead of taking several steps You’re accustomed to, You need just to press a simple – usually thee-key combination.

Most likely You’ll appreciate this especially in the long run, when You can really feel how much time and hassle it saves.

Here are a few examples of how You could benefit from AutoHotkey:

Quick survey on what’s coming:

  1. Lightning quick Google search.
  2. Drag’n’drop windows much easier.
  3. Insert Your Own flexible timestamp everywhere.
  4. Launch a terminal within the active window directory.
  5. Standard text-formatting keyboard shortcuts… going Markdown 🙂 !
  6. Quote anything the smart way.
  7. Other examples.
  8. Launch an app.
  9. Close an app.
  10. Force-close (kill) an app.
  11. How to make an AutoHotkey script work?
  12. Ascribe a Your Own sys-tray icon.

You can initiate a Google search on a selected text (select the text in any app, press the keyboard shortcut of Your preference and this will open Your web-browser with a Google search results page on what You’ve selected).

Here is the code (find out how to make all the code snippets actually working, at the end of this article):

;- Control+1 = <!-- {Google it!} -->
^1::
send ^c
Sleep,50
Run, https://www.google.com/search?q=%clipboard%
return

Take a closer look on

;- Control+1 = <!-- {Google it!} -->
^1::

It is here where You have a keyboard shortcut ascribed (in this particular case the shortcut is Ctrl+1).

If You’d prefer to use Your Own keyboard shortcut, here You can learn which code stands for various keyboard and mouse buttons – thanks to which You’ll be able to ascribe Your Own keyboard shortcuts.

As a result, provided You’ve already launched the script file. whenever You press Ctrl+1, the script will be initiated. Similar way it looks in case of other scripts described below.

Drag’n’drop windows much easier.

You can reposition windows on Your screen in a much more convenient way. So far You need to click and hold on a window’s thin title bar in order to drag it to some other place. Now You can click anywhere within the window itself, provided You’ve pressed and hold a key of Your choice (in the example below it is LWin & LButton:: – which stands for holding Win logo + left mouse button clicked within a window). This will do the trick and the window will move, as effectively as before 🙂 .

; This script modified from the original: http://www.autohotkey.com/docs/scripts/EasyWindowDrag.htm
; by The How-To Geek
; http://www.howtogeek.com 

LWin & LButton::
CoordMode, Mouse  ; Switch to screen/absolute coordinates.
MouseGetPos, EWD_MouseStartX, EWD_MouseStartY, EWD_MouseWin
WinGetPos, EWD_OriginalPosX, EWD_OriginalPosY,,, ahk_id %EWD_MouseWin%
WinGet, EWD_WinState, MinMax, ahk_id %EWD_MouseWin% 
if EWD_WinState = 0  ; Only if the window isn't maximized 
	SetTimer, EWD_WatchMouse, 10 ; Track the mouse as the user drags it.
return

EWD_WatchMouse:
GetKeyState, EWD_LButtonState, LButton, P
if EWD_LButtonState = U  ; Button has been released, so drag is complete.
{
	SetTimer, EWD_WatchMouse, off
	return
}
GetKeyState, EWD_EscapeState, Escape, P
if EWD_EscapeState = D  ; Escape has been pressed, so drag is cancelled.
{
	SetTimer, EWD_WatchMouse, off
	WinMove, ahk_id %EWD_MouseWin%,, %EWD_OriginalPosX%, %EWD_OriginalPosY%
	return
}
; Otherwise, reposition the window to match the change in mouse coordinates
; caused by the user having dragged the mouse:
CoordMode, Mouse
MouseGetPos, EWD_MouseX, EWD_MouseY
WinGetPos, EWD_WinX, EWD_WinY,,, ahk_id %EWD_MouseWin%
SetWinDelay, -1   ; Makes the below move faster/smoother.
WinMove, ahk_id %EWD_MouseWin%,, EWD_WinX + EWD_MouseX - EWD_MouseStartX, EWD_WinY + EWD_MouseY - EWD_MouseStartY
EWD_MouseStartX := EWD_MouseX  ; Update for the next timer-call to this subroutine.
EWD_MouseStartY := EWD_MouseY
return

Insert Your Own flexible timestamp everywhere.

It would be a nice idea to be able to insert Your Own timestamp with one single keyboard shortcut – in any app You’re using at the moment. Furthermore, what if the timestamp would be flexible, e.g. depending on the time of the day?

It may look like this (You can design it from scratch to Your Own liking):

…at some nice evening of October 23 🙂 (19:42).

or

…at some nice day of October 23 🙂 (1:43 ᴘ.ᴍ.)

or

…at some nice night of October 23 🙂 (10:52 ᴘ.ᴍ.)

etc.

To make such a timestamp You need the following code:

24-hour format:

;- Control+Shift+D = <!-- {text} -->
^+D::

formattime, hourvar, , HH
formattime, minutevar, , mm
formattime, dayvar, , dd
formattime, monthvar, , MM
formattime, yearvar, , yyyy

ctime:=hourvar . minutevar
m:=minutevar
h:=hourvar
d:=dayvar

if (monthvar=01)
(cmonth:="January")
if (monthvar=02)
(cmonth:="February")
if (monthvar=03)
(cmonth:="March")
if (monthvar=04)
(cmonth:="April")
if (monthvar=05)
(cmonth:="May")
if (monthvar=06)
(cmonth:="June")
if (monthvar=07)
(cmonth:="July")
if (monthvar=08)
(cmonth:="August")
if (monthvar=09)
(cmonth:="September")
if (monthvar=10)
(cmonth:="October")
if (monthvar=11)
(cmonth:="November")
if (monthvar=12)
(cmonth:="December")

cyear:=yearvar

if (ctime>=500) and (ctime<=759)
	send ...at some nice morning of %cmonth% %d% :) (%h%:%m%)

if (ctime>=800) and (ctime<=1859)
	send ...at some nice day of %cmonth% %d% :) (%h%:%m%)

if (ctime>=1900) and (ctime<=2159)
	send ...at some nice evening of %cmonth% %d% :) (%h%:%m%)

if (ctime>=2200) and (ctime<=2359)
	send ...at some nice night of %cmonth% %d% :) (%h%:%m%)

if (ctime>=0000) and (ctime<=0459)
	send ...at some nice night of %cmonth% %d% :) (%h%:%m%)

return

12-hour format (ᴀ.ᴍ./ᴘ.ᴍ.):

;- Control+Shift+D = <!-- {text} -->
^+D::

formattime, hourvar, , HH
formattime, hourvar2, , h
formattime, minutevar, , mm
formattime, dayvar, , dd
formattime, monthvar, , MM
formattime, yearvar, , yyyy

ctime:=hourvar . minutevar
m:=minutevar
h:=hourvar2
d:=dayvar

if (monthvar=01)
(cmonth:="January")
if (monthvar=02)
(cmonth:="February")
if (monthvar=03)
(cmonth:="March")
if (monthvar=04)
(cmonth:="April")
if (monthvar=05)
(cmonth:="May")
if (monthvar=06)
(cmonth:="June")
if (monthvar=07)
(cmonth:="July")
if (monthvar=08)
(cmonth:="August")
if (monthvar=09)
(cmonth:="September")
if (monthvar=10)
(cmonth:="October")
if (monthvar=11)
(cmonth:="November")
if (monthvar=12)
(cmonth:="December")

cyear:=yearvar

if (ctime>=500) and (ctime<=759)
	send ...at some nice morning of %cmonth% %d% :) (%h%:%m% ᴀ.ᴍ.)

if (ctime>=800) and (ctime<=1159)
	send ...at some nice day of %cmonth% %d% :) (%h%:%m% ᴀ.ᴍ.)

if (ctime>=1200) and (ctime<=1859)
	send ...at some nice day of %cmonth% %d% :) (%h%:%m% ᴘ.ᴍ.)

if (ctime>=1900) and (ctime<=2159)
	send ...at some nice evening of %cmonth% %d% :) (%h%:%m% ᴘ.ᴍ.)

if (ctime>=2200) and (ctime<=2359)
	send ...at some nice night of %cmonth% %d% :) (%h%:%m% ᴘ.ᴍ.)

if (ctime>=0000) and (ctime<=0459)
	send ...at some nice night of %cmonth% %d% :) (%h%:%m% ᴀ.ᴍ.)

return

As far as ᴀ.ᴍ./ᴘ.ᴍ. are concerned, I’m currently reading Benjamin Dreyer’s Dreyer’s English – An Utterly Correct Guide to Clarity and Style, in which the author recommends to use so-called small caps instead of capital letters:

"When writing of time, I favor (…) those pony-size capital letters (affectionately known as small caps) rather than the horsier A.M./P.M. or the desultory-looking a.m./p.m. (AM/PM and am/pm are out of the question)."

Since I’ve noticed that You may encounter issues with copy&paste the code with the small caps included – in this particular case it’s better for You to download the script file from here – this way You’ll be sure that everything will be working fine.

If, however, You’d like to create such a script from scratch, small caps included – then 1) make sure that Your text file is UTF-8 encoded and 2) use this website to copy&paste small caps version of ᴀ.ᴍ./ᴘ.ᴍ..

The dependence on the time of the day is based on the following time spans:

  • 5:00 am – 7.59 am
  • 8:00 am -6:59 pm
  • 7:00 pm – 9:59 pm
  • 10:00 pm – 11:59 pm
  • 12:00 pm – 4:59 am

There are more time spans in
case of 12-hour format, due to
determining whether am or pm text should be displayed.

The script is international, i.e.:
1. You can replace month names with Your Own (by default AutoHotkey always uses month names according to what Windows language You use).
2. In case of 12-hour version used in 24-hour format countries: You should still have them properly displayed (despite of that by default AutoHotkey supports 12-hour am/pm format, but it may not work properly when You live in 24-hour format country and use this country Windows language).

Interestingly, once You press the timestamp keyboard shortcut (both examples above use Ctrl+Shift+D), it will be inserted into Your text editor in a characteristic way, resembling live-typewriting. If You, however, would prefer to show it instantly, replace all the instances of send with sendInput.

Launch a terminal within the active window directory.

If You often use a terminal, You can launch it alongside a currently window-opened directory (the one which You have displayed in a Windows Explorer active window) – so You don’t need to use additional commands to navigate to the desired place.

;; Open terminal in current Explorer window folder
#If WinActive("ahk_class CabinetWClass") ; explorer

	F4::
	WinGetTitle, ActiveTitle, A
	If InStr(ActiveTitle, "\")  ; If the full path is displayed in the title bar (Folder Options)
		Fullpath := ActiveTitle
	else
	If InStr(ActiveTitle, ":") ; If the title displayed is something like "DriveName (C:)"
	{
		Fullpath := SubStr(ActiveTitle, -2)
		Fullpath := SubStr(Fullpath, 1, -1)
	}
	else    ; If the full path is NOT displayed in the title bar 
	; https://autohotkey.com/boards/viewtopic.php?p=28751#p28751
	for window in ComObjCreate("Shell.Application").Windows
	{
		try Fullpath := window.Document.Folder.Self.Path
		SplitPath, Fullpath, title
		If (title = ActiveTitle)
			break
	}
	Run, mintty.exe, %Fullpath%
	return 

#If

One note: the code above runs Cygwin – a very interesting terminal which You can use under Windows to introduce a bunch of GNU/Linux commands and apps into Your non-Linux operating system. Besides, it has some advantages over the classic Windows command prompt. If You, however, would prefer to use the Windows terminal instead of Cygwin – just replace mintty.exe with cmd.exe.

Standard text-formatting keyboard shortcuts… going Markdown 🙂 !

If You write in Markdown, You can make well-known classic text-formatting shortcuts acting Markdown way, e.g. Ctrl+B will bold the text, Ctrl+I will italicize it, and so on. A set of common shortcuts You’ll find here.

Quote anything the smart way.

We are accustomed to the basic punctuation marks such as quotes. Whether it comes to double or single quotation marks, there is one intriguing thing which You might not know about. If You open Windows Notepad, for example, and insert quotation marks – they look differently than the ones You know pretty well from books, articles on the Web, etc. The latter are a little bit curly, while the former seems to be more straight.

"straight quotes" vs “curly quotes”

Although quotation mark button on the keyboard results in the straight form (with the exceptions of some apps like Microsoft Word – which change the button’s behavior in favor of the curly quotes) – it seems that the curly one is much more popularized (let alone it looks nicer). The thing is: how to insert curly quotes, if they don’t have their own button on a keyboard?

Although there are some key sequences to bring those characters forth – they definitely are not convenient in use (think about the Alt+0147 and Alt+0148 numeric keypad sequences for the left and right double quotation marks, respectively). What about one simple keyboard shortcut instead? I mean, any shortcut of Your preference, even one single button, if You like.

The script looks like that:

^2::
saved := clipboardall
Send, ^x
SendInput, {U+201C}%clipboard%{U+201D}
clipboard := saved
saved := ""
Return

In the example above I use Ctrl+2 (^2::) as the shortcut.

Now, if You want to wrap something with curly double quotation marks, just select the text and press the keyboard shortcut You’ve chosen for the script.

Note that it could be good to include all the rest appropriate variants: single curly quotation marks and a curly apostrophe, too. In case of single curly quotation marks the code goes as follows:

^3::
saved := clipboardall
Send, ^x
SendInput, {U+2018}%clipboard%{U+2019}
clipboard := saved
saved := ""
Return

The last piece is a single curly apostrophe, which – surprisingly or not – is exactly the same character as the right curly single quotation mark:

“The official Unicode name for the curly type of apostrophe is ‘right single quotation mark (…) this is [also] the preferred character to use for apostrophe.

In other words, a curly apostrophe and a right single curly quotation mark are the same thing”. ( source )

So the code goes as follows:

^4::
Send, {U+2019}
Return

Other examples.

AutoHotkey is a sky’s-the-limit kind of an app, with which You can do a great deal of things (e.g. close or open particular apps, launch files, etc.). The final frontier seems to be Your Own imagination. Here are some other code snippets for You to grab’n’go 🙂 :

Launch an app.

Run "C:\Program Files\Siber Systems\GoodSync\GoodSync.exe" /tray

return

The example above runs the GoodSync app (a tool to make backup of Your data). In addition, it runs it in Windows tray – which is not an AutoHotkey command, though, but a GoodSync’s supported launch parameter. I use this simple script in tandem with Watch 4 Idle, which detects that I’m back at My desk, so it runs the backup app (it also deactivates it when it detects that I’m out for a long enough time 🙂 – You can read more about this concept here).

If You’d like to launch any other app, just replace the C:\Program Files\...... directory with the one leading to the executable file of Your app of preference.

Close an app.

When You need just to close a particular window – this little script may come in handy. Remember that it only closes a window – while the app responsible for it may still be working in the background (if what You want is to make an app stop working – look at the next script). If You’d like to use this, first find out what is the window’s title (the one which You want to close by the script) – then replace YourAppWindowTitle with the title.

PostMessage, 0x112, 0xF060,,, YourAppWindowTitle ; 0x112 = WM_SYSCOMMAND, 0xF060 = SC_CLOSE

return

Force-close (kill) an app.

This one is useful when You need to completely shutdown an app – close both its windows and processes working in the background. If You want to use this, first You need to find out what is the name of Your app’s process. To do so, try to find it through Windows Task Manager: just press Ctrl + Shift + Esc and (on the Processes tab) try to find Your app’s item (it often has a recognizable name followed by .exe extension). As before, replace GoodSync.exe with Your app’s process name:

process, close, GoodSync.exe
return

How to make an AutoHotkey script work?

  1. Install AutoHotkey.
  2. Create a new text file (e.g. in Windows Notepad), copy&paste a desired code into it and save it as YourName.ahk.
  3. Whenever You want to initiate the script, simply double-click the file You’ve just created – and/or use the keyboard shortcut You’ve ascribed to it (in theory You don’t have to ascribe a keyboard shortcut, but it often comes in handy – especially when You perform some action many times a day).
  4. Whenever You want the script to be automatically launched on every Windows startup:
    1. Open the Autostart folder (under Windows 7 its location may look like this: C:\Users\YourUserName\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup).
    2. Hold the Alt key and drag’n’drop Your script file into the Autostart folder to create a shortcut to it there.

Ascribe Your Own sys-tray icon.

Once You run an AutoHotkey script, it will be represented among other icons on Your system tray. It’s good to know that You can change its icon to make it more intuitive – which especially comes in handy when You use many scripts. If You ascribe an individual icon to each script, it will be easy to determine which icon in fact represents which script.

To do so, simply paste the following code at the beginning of Your script, and replace C:\YourPathtoTheIcon\IconFileName.ico with a real path leading to the icon You want to use:

;- Optional = <!-- {If You want to ascribe a custom icon tray for this script.} -->
Menu, Tray, Icon , C:\YourPathtoTheIcon\IconFileName.ico

Leave a Reply