Jake Buck

Notepad Replacement Launcher


I like to use Notepad++ as a replacement for the standard Notepad that comes with the Windows operating system. With syntax highlighting for most common programming/data formats, tabs, and plugin capabilities Notepad++ meets my needs for simple editing/reading.

There are many software solutions to using Notepad++ as a replacement for Notepad, but I decided to code one myself, using my favorite scripting program, AutoHotkey. While I could just have changed the associations of files I want to open using Notepad++, some programs call notepad directly when displaying read-me files (etc), which is behavior I want to avoid.

If you are running Windows 7, you have to enable access to the notepad.exe file on your comp. You can find instructions on that here: How to Replace Notepad in Windows 7.

In order to write a program that will open your replacement for Notepad, you have to understand how your system calls notepad.exe. If you open a file associated with Notepad (a .txt file for example), the system calls notepad.exe and passes the name of the file to it via a command-line argument. All you need an intermediate program to do is save the argument and launch your replacement, passing the argument to the new program.

Here’s the script:

AutoTrim, Off
;Stops AHK from trimming leading/trailing spaces off of string assignments.

If 0 > 0 ;If the number of command-line arguments is > 0
{
    ;The first zero is a variable that contains the number of command-line arguments

    commandString =
    ;Declare/initialize commandString

    ;Command-line arguments are separated by a space. If the file name/path has a space
    ;in it, AutoHotkey splits the name into multiple arguments.  This loop combines
    ;the arguments back together so we can pass it to notepad++ later.
    Loop, %0% ;For each parameter:
    {
        param := %A_Index% ;Store the contents of the variable named by A_Index in param
        ;A_Index is a built-in variable that contains the loop iteration

        commandString = %commandString%%param%
        ;Adds the current parameter to commandString

        If %0% > %A_Index% ;If this is not the last parameter
        {
            commandString = %commandString%%A_Space%
            ;Add a space
        }
    }

    Run, C:\Program Files\Notepad++\notepad++.exe %commandString%
    ;Run notepad++ and pass commandString to it
}
Else ;No command-line arguments were passed to the script
{
    Run, C:\Program Files\Notepad++\notepad++.exe
    ;It was a shortcut to Notepad that was opened, not a file, so simply run notepad++
}