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: 6483
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: 6483
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

Return to “Ask For Help”

Who is online

Users browsing this forum: effel, flyingDman, Getfree, Godvicien, Google [Bot], Odlanir and 207 guests