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?

  • 2
    Add the command for the escape key to deselect the selected file at the start of the script? – Dave Oct 23 at 7:13
  • @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 Oct 23 at 8:14

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
  • 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? – Basj Oct 22 at 22:17
  • @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. – davidmneedham Oct 23 at 15:21
#If (WinActive("ahk_class Progman") || WinActive("ahk_class WorkerW") || WinActive("ahk_class CabinetWClass"))   ; desktop or explorer

    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%
        If WinActive("ahk_class Progman") or WinActive("ahk_class WorkerW") ; desktop
        {       
            FileAppend,, %A_Desktop%\%name%.txt
            Run, %A_Desktop%\%name%.txt
        }
        else
        if WinActive("ahk_class CabinetWClass") ; explorer
        {
            WinGetTitle, ActiveTitle, A
            If InStr(ActiveTitle, "\")  ; If the full path is displayed in the title bar (Folder Options)
                Folderlpath := ActiveTitle
            else
            If InStr(ActiveTitle, ":") ; If the title displayed is something like "DriveName (C:)"
            {
                Folderlpath := SubStr(ActiveTitle, -2)
                Folderlpath := SubStr(Folderlpath, 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 Folderlpath := window.Document.Folder.Self.Path
                SplitPath, Folderlpath, title
                If (title = ActiveTitle)
                    break
            }
            FileAppend,, %Folderlpath%\%name%.txt
            Run, %Folderlpath%\%name%.txt
        }
    }
    return 

#If

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.