Create a text file with a keyboard shortcut in Windows Explorer

6,568

Solution 1

I'm finally using:

  • @davidmneedham's solution when in Explorer window: Alt, f, w, t

  • Right click and w, t in other cases (such as Desktop)

Here is the code:

#IfWinActive ahk_class CabinetWClass
F4:: Send {ALT}fwt
#IfWinActive

F4:: 
Click,,Right
Send wt
Return

Solution 2

; ahk_group ExplorerGroup
GroupAdd, ExplorerGroup, ahk_class ExploreWClass
GroupAdd, ExplorerGroup, ahk_class CabinetWClass

; ahk_group DesktopGroup
GroupAdd, DesktopGroup, ahk_class Progman
GroupAdd, DesktopGroup, ahk_class WorkerW

; ahk_group ExplorerDesktopGroup
GroupAdd, ExplorerDesktopGroup, ahk_group ExplorerGroup
GroupAdd, ExplorerDesktopGroup, ahk_group DesktopGroup

            RETURN   ; === end of auto-execute section ===


#IfWinActive ahk_group ExplorerDesktopGroup

    F1::
    WinGet, active_id, ID, A
    InputBox, name, Create a New Text Document, Enter a name:,, 300, 120
    If !ErrorLevel
    {
        WinActivate, ahk_id %active_id%
        IfWinActive ahk_group DesktopGroup ; desktop
        {       
            FileAppend,, %A_Desktop%\%name%.txt
            Run, %A_Desktop%\%name%.txt
        }
        else
        IfWinActive ahk_group ExplorerGroup ; explorer
        {
            FolderPath := GetActiveExplorerPath()           
            FileAppend,, %FolderPath%\%name%.txt
            Run, %FolderPath%\%name%.txt
        }
    }
    return 

#IfWinActive

;* =========   FUNCTION     ===================

; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
GetActiveExplorerPath()
{
    explorerHwnd := WinActive("ahk_class CabinetWClass")
    if (explorerHwnd)
    {
        for window in ComObjCreate("Shell.Application").Windows
        {
            if (window.hwnd==explorerHwnd)
            {
                return window.Document.Folder.Self.Path
            }
        }
    }
}

Solution 3

I have a hack for this. PgUp selects the first file. Then Ctrl + Space, clears the selected file.

You can add that to your authotkey script, just before creating a new file. That's the simplest approach.

;   New text file

#IfWinActive AHK_CLASS #32770
    Capslock & f11::
#IfWinActive AHK_CLASS CabinetWClass
    Capslock & f11::

    ; make it work even though a file is previously selected
    Send {PgUp} ; Force select the first file 
    Send ^{Space} ; Clear the selection

    Sleep 250 ; Remove delay if not required
    Send {AppsKey} ; Menu key
    Send w ; New
    Send t ; Text Document
    return
#IfWinActive
  • Works on file dialogs (save as / open file)
  • Works even though a file is selected
  • Works even though cursor is positioned off screen

Change "Capslock & f11" to your preferred shortcut.


To understand the syntax above, see below example,

;   Syntax - To have the same hotkey subroutine executed by more than one variant, the easiest way is to create a stack of identical hotkeys, each with a different #IfWin directive above it. For example:
    ;#IfWinActive ahk_class Notepad
    ;#z::
    ;#IfWinActive ahk_class WordPadClass
    ;#z::
    ;MsgBox You pressed Win+Z in either Notepad or WordPad.
    ;return

Hope you find this useful!

Solution 4

The File menu in Windows Explorer displays the New > Text Document menu option within a folder whether or not a file or folder is selected.

You can create a text document by simulating pressing Alt, f, w, t with this AutoHotkey script (bound to F4):

F4::
  Send {ALT}fwt
Return
Share:
6,568
Basj
Author by

Basj

Love to work on R&D involving Python, maths, machine learning / deep learning, data science, product design, and MacGyver solutions to complex problems.

Updated on September 18, 2022

Comments

  • Basj
    Basj over 1 year

    I'm using the solution from How to create a new text document (TXT) file by a Hotkey? since a few years, with Autohotkey, and it allows to create a new text file anywhere in the Windows explorer with a keyboard shortcut.

    There is one drawback: when a file already has focus in the Explorer file list ("Details" view), it doesn't work, mainly because, when a file is selected, the Contextual menu doesn't show the "New > Text document".

    Question: how to have a shortcut that creates a new text document, even if a file is currently selected in Details view of the Windows Explorer?

    • Dave
      Dave over 5 years
      Add the command for the escape key to deselect the selected file at the start of the script?
    • Basj
      Basj over 5 years
      @Dave Already tried, but doesn't work: go in Explorer, Details view in Windows 7. Click on a file (it will be selected/highlighted), then hit Escape, it will stay selected.
  • Basj
    Basj over 5 years
    Yes @davidmneedham, it works great, except on Desktop (there, it doesn't). Would you have an idea of shortcut that works both on Desktop and in Windows Explorer / Details view?
  • davidmneedham
    davidmneedham over 5 years
    @Basj you should edit your question to indicate that you want it to function both on the Desktop and within Windows Explorer. (user3419297's answer)[superuser.com/a/1369167/206587] is one option that fits your use case.
  • Basj
    Basj over 4 years
    What does ALT+H do? For me (Win7), it does show the Help menu of the Explorer window, so it does not work.
  • elig
    elig about 4 years
    Great, this works in Windows 10 too