Create new file in current explorer window?

Get help with using AutoHotkey and its commands and hotkeys
Bruttosozialprodukt
Posts: 457
Joined: 24 Jan 2014, 22:28

Create new file in current explorer window?

25 Sep 2015, 03:50

You may or may not know of the standrad Windows hotkey Ctrl+Shift+N to create a nwe folder in the currently active explorer window (includes desktop, 'save as' dialogs etc).
I'd like to have a similar hotkey that simply created a new file.
So when I press Ctrl+Shift+M a new file (without a file extension) is created and I can directly enter a name for it, press enter and then have that file be selected/marked.

Has anyone done that before? Any idea how I could implement that in an elegant way?
lexikos
Posts: 6682
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Create new file in current explorer window?

25 Sep 2015, 05:18

I wrote this a few years ago, apparently. It seems I didn't post it anywhere.

Code: Select all

#ifWinActive ahk_class Progman
^+F::NewDesktopFile()
NewDesktopFile() {
    ; Find desktop window object.
    shellWindows := ComObjCreate("Shell.Application").Windows
    VarSetCapacity(_hwnd, 4, 0)
    desktop := shellWindows.FindWindowSW(0, "", 8, ComObj(0x4003, &_hwnd), 1)
    ; Deselect each item.
    ; IShellFolderViewDual: http://msdn.microsoft.com/en-us/library/dd894076
    sfv := desktop.Document
    items := sfv.SelectedItems
    Loop % items.Count
        sfv.SelectItem(items.Item(A_Index-1), 0)
    ; Create the file.
    FileAppend,, %A_Desktop%\New File
    ; Force the icon to show up (soon, but not instantly).
    desktop.Refresh()
    ; Select the item (doesn't matter whether it's visible yet).
    sfv.SelectItem(items.Item("New File"), 1)
    ; Loop usually isn't necessary, but adds a bit of reliability.
    Loop 5 {
        Sleep 50
        Send {F2}
        ; Verify we're now in rename mode.
        ControlGetFocus focus
    } until (focus = "Edit1")
}
"Progman" should be changed to "WorkerW" for Windows 10, though that might be ambiguous.

For other Explorer windows, find the window in the shellWindows collection rather than using FindWindowSW().

sfv.Folder.Self.Path returns the folder's path.
Bruttosozialprodukt
Posts: 457
Joined: 24 Jan 2014, 22:28

Re: Create new file in current explorer window?

25 Sep 2015, 08:18

Wow, that's a really neat piece of code!
So that solves the problem for the desktop.

But there are 2 problems left.
From what I can tell, the shellWindows collection contains windows normal explorer and internet explorer windows.
Filtering out the IE stuff shouldn't be too hard, but how could I get things like "Save As.." dialogs?

I'm also unsure about how I could find out which item belongs to which explorer window.

Any idea if and how it would be possible to use something like WBGet to get access to explorer windows and dialogs?

Code: Select all

WBGet(WinTitle="ahk_class IEFrame", Svr#=1) {               ;// based on ComObjQuery docs
   static msg := DllCall("RegisterWindowMessage", "str", "WM_HTML_GETOBJECT")
        , IID := "{0002DF05-0000-0000-C000-000000000046}"   ;// IID_IWebBrowserApp
;//     , IID := "{332C4427-26CB-11D0-B483-00C04FD90119}"   ;// IID_IHTMLWindow2
   SendMessage msg, 0, 0, Internet Explorer_Server%Svr#%, %WinTitle%
   if (ErrorLevel != "FAIL") {
      lResult:=ErrorLevel, VarSetCapacity(GUID,16,0)
      if DllCall("ole32\CLSIDFromString", "wstr","{332C4425-26CB-11D0-B483-00C04FD90119}", "ptr",&GUID) >= 0 {
         DllCall("oleacc\ObjectFromLresult", "ptr",lResult, "ptr",&GUID, "ptr",0, "ptr*",pdoc)
         return ComObj(9,ComObjQuery(pdoc,IID,IID),1), ObjRelease(pdoc)
      }
   }
}
lexikos
Posts: 6682
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: Create new file in current explorer window?

25 Sep 2015, 17:39

... but how could I get things like "Save As.." dialogs?
You can't, afaik.
I'm also unsure about how I could find out which item belongs to which explorer window.
What do you mean by "item"? If you mean "window object", just check the Hwnd property.
User avatar
MilesAhead
Posts: 232
Joined: 03 Oct 2013, 09:44

Re: Create new file in current explorer window?

27 Sep 2015, 09:41

If you have Text Document in the context menu under New it may work with this macro

You can add things to the DesktopGroup such as save as dlgs and change the hotkey

Code: Select all

Sendmode INPUT
GroupAdd,DesktopGroup, ahk_class Progman
GroupAdd,DesktopGroup, ahk_class WorkerW

#IfWinActive ahk_group DesktopGroup
^LButton::
	Click,,Right
	Sleep,100
	Send,w
	Sleep,200
	Send,t
return
"My plan is to ghostwrite my biography. Then hire another writer to put his
name on it and take the blame."

- MilesAhead
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Create new file in current explorer window?

22 Oct 2019, 10:38

A note re. VarSetCapacity(_hwnd, 4, 0), which is used in the script, and which is correct.
This *is* 64-bit compatible.
It does *not* need to be this: VarSetCapacity(_hwnd, A_PtrSize, 0).
Note: when I tested on Windows 7, this hWnd had the class 'Progman'.

Firstly, the FindWindowSW method specifies a pointer to a long. A long is a 32-bit signed integer, i.e. *4* bytes.
IShellWindows::FindWindowSW (exdisp.h) | Microsoft Docs
https://docs.microsoft.com/en-us/windows/win32/api/exdisp/nf-exdisp-ishellwindows-findwindowsw
Type: long*
A handle for the window matching the specified search criteria, or NULL if no such window was found.
Secondly, although window handles use the pointer type (8 or 4 bytes), window handles are guaranteed to have 32-bit values (4 bytes) in Windows, for both 64-bit and 32-bit versions.
winapi - What is the range of a Windows HANDLE on a 64 bits application? - Stack Overflow
https://stackoverflow.com/questions/18266626/what-is-the-range-of-a-windows-handle-on-a-64-bits-application
Interprocess Communication Between 32-bit and 64-bit Applications - Win32 apps | Microsoft Docs
https://docs.microsoft.com/en-us/windows/win32/winprog64/interprocess-communication
64-bit versions of Windows use 32-bit handles for interoperability. When sharing a handle between 32-bit and 64-bit applications, only the lower 32 bits are significant, so it is safe to truncate the handle (when passing it from 64-bit to 32-bit) or sign-extend the handle (when passing it from 32-bit to 64-bit). Handles that can be shared include handles to user objects such as windows (HWND), handles to GDI objects such as pens and brushes (HBRUSH and HPEN), and handles to named objects such as mutexes, semaphores, and file handles.
Some code re. truncation and sign-extension:

Code: Select all

;q:: ;sign-extend/truncate
;sign-extend 32-bit number to 64-bit number (copy the leftmost bit leftwards)
;truncate 64-bit number to 32-bit number

vNum := 0x80000000
;vNum := (vNum << 32) >> 32 ;sign-extend
vNum := vNum << 32 >> 32 ;sign-extend ;equivalent to line above
MsgBox, % Format("0x{:X}", vNum) ;0xFFFFFFFF80000000
vNum &= 0xFFFFFFFF
MsgBox, % Format("0x{:X}", vNum) ;0x80000000

vNum := 0x7FFFFFFF
;vNum := (vNum << 32) >> 32 ;sign-extend
vNum := vNum << 32 >> 32 ;sign-extend ;equivalent to line above
MsgBox, % Format("0x{:X}", vNum) ;0x7FFFFFFF
vNum &= 0xFFFFFFFF
MsgBox, % Format("0x{:X}", vNum) ;0x7FFFFFFF
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Axxi, Bing [Bot], Failires1337, kr1207077, teadrinker and 104 guests