AutoHotKey

AutoHotKey is a free and open-source macro utility for Windows. It can help greatly with automating repetitive, one-off tasks, especially with GUI programs that don’t have built-in CLI automation or batch scripting. Your mileage may vary, as not all programs will accept the virtual input events that AHK generates. That said, it’s quite nice when everything works.

 

Quick scripting notes

; comments begin with a semicolon
^!q::
    ; Trigger label example for key input
    ; ^ = CTRL
    ; ! = ALT
    ; q = the 'Q' key

    ; Let's send some keyboard input with some delays.
    Send,Let's cut and paste.
    Sleep, 250
    Send,{Shift Down}{Home}{Shift Up}
    Sleep, 250
    Send,{Control Down}x{Control Up}
    Sleep, 250
    Send,{Enter}{Enter}{Enter}
    Sleep, 250
    Send,{Control Down}v{Control Up}
    Sleep, 250
    Send,{Enter}{Enter}Tada!

    ; Finish a block with 'Return'
Return

 

Transfer “Untitled” Notepad text to Notepad++

My old job required me to take notes often, and quickly, during troubleshooting sessions. Here is a script I used at the end of the day to transfer the contents of all untitled Notepad instances to Notepad++, a program that can be configured to autosave everything upon closing.

Maybe this was a bad habit, and I should have just typed stuff into NP++ directly, but I found it easier while on calls / remote sessions to just hit Win+R, type notepad, and start typing away. I set NP++ as the default program for txt files, so I could tell at a glance that if something was written in Notepad, then it was fresh information from that day.

I wasn’t able to make this script perfect (it’s time-sensitive: it will screw up if your system is lagging due to slow disk access, for example), and I have neither AuthoHotKey nor Notepad++ at my current job, so this is provided as-is with no warranty.

; copy all notepad files called "Untitled" to new files in notepad++ which
; will autosave upon closure. Based on AHK help file examples.
; v1.3 2016/19/Feb

^+!s::
    ; Go no further if Notepad++ is not running.
    IfWinNotExist ahk_class Notepad++
    {
        MsgBox, Notepad++ process not found.
        Return
    }
    npp_is_running := 1

    ; Confirm run
    MsgBox, 4, , Copy contents of all "Untitled - Notepad" instances to Notepad++? This will overwrite your clipboard state. Do not execute if a remote desktop session is running.
    IfMsgBox, NO, Return

    ; Get a list of all windows
    WinGet, id, list,,, Program Manager
 
    ; Iterate through windows
    Loop, %id%
    {
 
        ; If Notepad++ crashed or something, stop.
        IfWinNotExist ahk_class Notepad++
        {
            npp_is_running := 0
            Break
        }

        ; Right now this will jump focus to every window.
        ; WIP: There must be a way to skip over programs that aren't "Untitled - Notepad".
        this_id := id%A_Index%
        WinActivate, ahk_id %this_id%
        WinGetClass, this_class, ahk_id %this_id%
        WinGetTitle, this_title, ahk_id %this_id%
 
        ; Work on this window if it's "Untitled - Notepad"
        if (this_title="Untitled - Notepad")
        {
            ; Sleep ten seconds.
            ; Adjust appropriately to match the performance of your PC.
            ; (Mine would be crawling by the end of the day.)
            Sleep, 10000
 
            ; Select all
            Send, {Ctrl Down}a{Ctrl Up}
 
            ; Cut
            Send, {Ctrl Down}x{Ctrl Up}
 
            ; Close window
            Send, {Alt Down}{F4}{Alt Up}
 
            ; Switch to Notepad++
            WinActivate, ahk_class Notepad++
            WinWaitActive, ahk_class Notepad++
 
            ; Sleep half a second 
            ; Increase if your PC is sloooowwwww
            Sleep, 500

            ; New Notepad++ tab
            Send, {Ctrl Down}n{Ctrl Up}
 
            ; Paste
            Send, {Ctrl Down}v{Ctrl Up}
         }
    }
 
    if npp_is_running = 0
    {
        MsgBox, Notepad++ process not found.
        Return
    }
 
    ; Switch focus to Notepad++ upon completion
    WinActivate, ahk_class Notepad++
    WinWaitActive, ahk_class Notepad++
Return

 

(e: 15/Sept/2016: Formatting)
(e: 27/Nov/2016: Notepad to Notepad++ transfer script)

Leave a Comment