1

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
  • 2
    Add the command for the escape key to deselect the selected file at the start of the script? – Dave Oct 23 '18 at 7:13
  • 1
    @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 '18 at 8:14
3
; 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
            }
        }
    }
}
1
  • Great, this works in Windows 10 too – elig May 6 '20 at 14:24
1

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
2
  • 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 '18 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 '18 at 15:21
1

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!

0

As an addendum to the original script you were using, you can do the following:

F4::
Macro1:
!h, sn           ; <-- this is what you want, ALT is denoted by !
Click, Right, 1
Sleep, 10
SendRaw, wt
Return

(ALT+H, SN = select none)

Which is to say, do ALT+H then S followed by N. This effectively does a "Select None" then proceeds to emulate the context menu method of creating said file.

This works in File Explorer and the Desktop, although this was Windows 10, I'd assume this would work in Windows 7.

1
  • What does ALT+H do? For me (Win7), it does show the Help menu of the Explorer window, so it does not work. – Basj Oct 24 '19 at 22:44
0

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
0

Keyboard shortcut is available by default in Windows Explorer. Basically you need to create or modify the shortcut for a program that you use to create a text file, usually Notepad (I am using Notepad++).

  1. Using Windows explorer, locate or create a new shortcut (in my case, it's C:\ProgramData\Microsoft\Windows\Start Menu\Programs\notepad++.lnk)
  2. Right click, click Properties
  3. Click Shortcut
  4. Go to Shortcut key, put your cursor in a box where it says None, and then press your keys.

For details: https://www.techjunkie.com/add-custom-hotkeys-windows-10/

Note:

  1. This hotkey will override existing hotkey. So for example you assign Ctrl+Shift+T for this shortcut and you want to re-open the last closed tab on your browser (e.g. Firefox), Windows will open a text editor instead of Firefox opening your last closed tab. So make sure to assign an unused hotkey.
  2. Deleting the shortcut key will also remove the hotkey assigned to it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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