Code: Select all
Run, %A_WinDir%\explorer.exe /select`,"%vPath%"
Code: Select all
Run, %A_WinDir%\explorer.exe /select`,"%vPath%"
You might find the function below does what you want:
Code: Select all
; Select a file in a Windows Explorer window that begins with the specified name.
; Accounts for "Hide extensions for known file types" setting in Folder Options.
; References: https://docs.microsoft.com/en-us/windows/win32/shell/shell
; https://docs.microsoft.com/en-us/windows/win32/shell/shellfolderview-selectitem
; https://autohotkey.com/board/topic/89713-jumping-to-selected-files-in-windows-explorer/?p=568216
; https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa752043(v=vs.85)
ExplorerSelect(hwnd, Name) {
for window in ComObjCreate("Shell.Application").Windows
if (window.hwnd = hwnd)
for item in window.Document.Folder.Items
if (InStr(item.Name, Name) = 1 || InStr(Name, item.Name) = 1)
return window.Document.SelectItem(item, 1|4|8|16)
}
Hi Lee,
Code: Select all
#IfWinActive, ahk_class CabinetWClass
#t:: ;explorer - create new text file and open it with Notepad2
#IfWinActive, ahk_class ExploreWClass
#t:: ;explorer - create new text file and open it with Notepad2
vPathNotepad2 := "C:\Program Files\Notepad2\Notepad2.exe"
vNameNoExt := "New Text Document"
vDotExt := ".txt"
vPath := ""
WinGet, hWnd, ID, A
for oWin in ComObjCreate("Shell.Application").Windows
{
if (oWin.HWND = hWnd)
{
vDir := RTrim(oWin.Document.Folder.Self.Path, "\")
;if !DirExist(vDir)
if !InStr(FileExist(vDir), "D")
{
oWin := ""
return
}
Loop
{
vSfx := (A_Index=1) ? "" : " (" A_Index ")"
vName := vNameNoExt vSfx vDotExt
vPath := vDir "\" vName
if !FileExist(vPath)
break
}
;create a blank text file (ANSI/UTF-8/UTF-16)
;FileAppend,, % "*" vPath
FileAppend,, % "*" vPath, UTF-8
;FileAppend,, % "*" vPath, UTF-16
break
}
}
oWin := ""
if FileExist(vPath)
Run, "%vPathNotepad2%" "%vPath%"
return
Yes. Like this:Lee James wrote: ↑28 Jan 2021, 12:36I am extremely grateful to you both for your help and would not want to waste your time. However, I'm afraid this is all too confusing for my simple brain! I just use AHK for very basic key assignments. I'm not a programmer, can't dig into code, combine scripts, and have have no idea what stuff like 'calling a class' means! Sorry I'm sorry, please consider me a total dummy.
The original script did almost exactly what I wanted, I would just like it to select the new file, instead of opening it. Could this be very easily changed?
Original script:Code: Select all
#IfWinActive, ahk_class CabinetWClass #t:: ;explorer - create new text file and open it with Notepad2 #IfWinActive, ahk_class ExploreWClass #t:: ;explorer - create new text file and open it with Notepad2 vPathNotepad2 := "C:\Program Files\Notepad2\Notepad2.exe" vNameNoExt := "New Text Document" vDotExt := ".txt" vPath := "" WinGet, hWnd, ID, A for oWin in ComObjCreate("Shell.Application").Windows { if (oWin.HWND = hWnd) { vDir := RTrim(oWin.Document.Folder.Self.Path, "\") ;if !DirExist(vDir) if !InStr(FileExist(vDir), "D") { oWin := "" return } Loop { vSfx := (A_Index=1) ? "" : " (" A_Index ")" vName := vNameNoExt vSfx vDotExt vPath := vDir "\" vName if !FileExist(vPath) break } ;create a blank text file (ANSI/UTF-8/UTF-16) ;FileAppend,, % "*" vPath FileAppend,, % "*" vPath, UTF-8 ;FileAppend,, % "*" vPath, UTF-16 break } } oWin := "" if FileExist(vPath) Run, "%vPathNotepad2%" "%vPath%" return
Code: Select all
#IfWinActive, ahk_class CabinetWClass
#t::
vNameNoExt := "New Text Document"
vDotExt := ".txt"
vPath := ""
WinGet, hWnd, ID, A
for oWin in ComObjCreate("Shell.Application").Windows {
if (oWin.HWND = hWnd) {
vDir := oWin.Document.Folder.Self.Path
Loop {
vSfx := (A_Index=1) ? "" : " (" A_Index ")"
vName := vNameNoExt vSfx vDotExt
vPath := vDir "\" vName
if !FileExist(vPath)
break
}
FileAppend,, % vPath, UTF-8-RAW
break
}
}
oWin.Refresh()
for Item in oWin.Document.Folder.Items
if (Item.Path = vPath)
oWin.Document.SelectItem(Item, 3|4|8)
oWin := ""
return
Code: Select all
For item in oWin.Document.SelectedItems
oWin.Document.SelectItem(item.Path, False)
oWin.Document.SelectItem(vPath, True)
Thank you for your kind advice on the desktop or the explorer.mikeyww wrote: ↑19 Dec 2021, 07:15#IfWinActive controls where the hotkey works. Without it, the hotkey works everywhere. It's OK to sneak a look at the documentation! You actually can dig into the code a bit. https://www.autohotkey.com/docs/commands/_IfWinActive.htm
Code: Select all
For item in oWin.Document.SelectedItems
oWin.Document.SelectItem(item.Path, False)
oWin.Document.SelectItem(vPath, True)
Code: Select all
If hWnd(dir := A_ScriptDir)
SoundBeep, 1500
Else Run, % "explorer.exe /select,""" newestFile(dir) """"
newestFile(dir) {
Loop, Files, %dir%\*.*
If (A_LoopFileTimeModified > newestTime)
newestTime := A_LoopFileTimeModified, sub := A_LoopFilePath
Return sub
}
hWnd(dir) {
; https://www.autohotkey.com/boards/viewtopic.php?p=422958#p422958
For Window in ComObjCreate("Shell.Application").Windows
Continue
Until (Window.document.Folder.Self.Path = dir && hWnd := Window.hwnd)
Return hWnd
}
It seems there is the only way that to use Run to select the new text file.
Code: Select all
Run, % "explorer.exe /select,""" path """"
Code: Select all
F3::
oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0)
; VT_BYREF := 0x4000, VT_I4 := 0x3
oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1)
Loop, Files, %A_Desktop%\*.*
oWin.Document.SelectItem(A_LoopFilePath, False)
oWin.Document.SelectItem(newestFile(A_Desktop), True)
SoundBeep, 1000
Return
newestFile(dir) {
Loop, Files, %dir%\*.*
If (A_LoopFileTimeModified > newestTime)
newestTime := A_LoopFileTimeModified, sub := A_LoopFilePath
Return sub
}
Works perfect,Thanks.mikeyww wrote: ↑20 Dec 2021, 10:13Code: Select all
F3:: oWindows := ComObjCreate("Shell.Application").Windows, VarSetCapacity(hWnd, 4, 0) ; VT_BYREF := 0x4000, VT_I4 := 0x3 oWin := oWindows.FindWindowSW(0, "", SWC_DESKTOP := 8, ComObject(0x4003, &hWnd), SWFO_NEEDDISPATCH := 1) Loop, Files, %A_Desktop%\*.* oWin.Document.SelectItem(A_LoopFilePath, False) oWin.Document.SelectItem(newestFile(A_Desktop), True) SoundBeep, 1000 Return newestFile(dir) { Loop, Files, %dir%\*.* If (A_LoopFileTimeModified > newestTime) newestTime := A_LoopFileTimeModified, sub := A_LoopFilePath Return sub }
Users browsing this forum: Bing [Bot], Google [Bot], peter_ahk and 402 guests