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?
Create new file in current explorer window?
-
- Posts: 463
- Joined: 24 Jan 2014, 22:28
Re: Create new file in current explorer window?
I wrote this a few years ago, apparently. It seems I didn't post it anywhere.
"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.
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")
}
For other Explorer windows, find the window in the shellWindows collection rather than using FindWindowSW().
sfv.Folder.Self.Path returns the folder's path.
-
- Posts: 463
- Joined: 24 Jan 2014, 22:28
Re: Create new file in current explorer window?
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?
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)
}
}
}
Re: Create new file in current explorer window?
You can't, afaik.... but how could I get things like "Save As.." dialogs?
What do you mean by "item"? If you mean "window object", just check the Hwnd property.I'm also unsure about how I could find out which item belongs to which explorer window.
- MilesAhead
- Posts: 232
- Joined: 03 Oct 2013, 09:44
Re: Create new file in current explorer window?
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
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
name on it and take the blame."
- MilesAhead
Re: Create new file in current explorer window?
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
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
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
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.Type: long*
A handle for the window matching the specified search criteria, or NULL if no such window was found.
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
Some code re. truncation and sign-extension: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.
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA