rename a file by selecting enter instead of F2 Topic is solved
rename a file by selecting enter instead of F2
Hey guys,
I am a mac user trying to make the transition to windows. I am used to being able to press the enter key to rename a selected file. However, in windows this just opens the file. Is it possible to have a script that makes the enter key rename the file (maybe by sending F2) but only when a file is selected?
I am a mac user trying to make the transition to windows. I am used to being able to press the enter key to rename a selected file. However, in windows this just opens the file. Is it possible to have a script that makes the enter key rename the file (maybe by sending F2) but only when a file is selected?
Re: rename a file by selecting enter instead of F2
Code: Select all
#IfWinActive ahk_class CabinetWClass
$Enter::
ControlGetFocus, fc, A
Send % Instr(fc, "DirectUI") ? "{F2}" : "{Enter}"
Return
#IfWinActive
Re: rename a file by selecting enter instead of F2
Thank you very much for that! I tried it but it doesnt work as it should:
On desktop:
It doesnt trigger the F2 button when I select a file and press enter.
In file eplorer:
The F2 got triggered in explorer, which is good. But actually, I have F1 and F2 mapped to brightness controls for the display so it will toggle that instead. Is there a way to trigger not the hotkey F2 but the underlaying command that F2 triggers?
On desktop:
It doesnt trigger the F2 button when I select a file and press enter.
In file eplorer:
The F2 got triggered in explorer, which is good. But actually, I have F1 and F2 mapped to brightness controls for the display so it will toggle that instead. Is there a way to trigger not the hotkey F2 but the underlaying command that F2 triggers?
Re: rename a file by selecting enter instead of F2
Code: Select all
$F2::MsgBox, Test ; Use the "$" hotkey modifier
#IfWinActive ahk_exe explorer.exe
$Enter::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}"
Return
#IfWinActive
Re: rename a file by selecting enter instead of F2
I got it to work with the following code:
#IfWinActive ahk_exe explorer.exe
Enter::
send, {AppsKey}
Send m
return
Basically it simulates right clicking and pressing the 'rename' button and this works pretty good. However, now I stumble upon a little inconvenience, because after this I want to press the enter again to confirm the name change. Now of course it sends the same right click + m command whenever I press enter. Is there a way to make an exception of this code where enter behaves like a normal enter command whenever 'in the renaming process'? Is there a way to indicate / replace the code in the '#IfWinActive ahk_exe explorer.exe' line to make this exception?
Greets Jelle
#IfWinActive ahk_exe explorer.exe
Enter::
send, {AppsKey}
Send m
return
Basically it simulates right clicking and pressing the 'rename' button and this works pretty good. However, now I stumble upon a little inconvenience, because after this I want to press the enter again to confirm the name change. Now of course it sends the same right click + m command whenever I press enter. Is there a way to make an exception of this code where enter behaves like a normal enter command whenever 'in the renaming process'? Is there a way to indicate / replace the code in the '#IfWinActive ahk_exe explorer.exe' line to make this exception?
Greets Jelle
Re: rename a file by selecting enter instead of F2
Thanks a lot for this idea. I unfortunately cannot use the F2 button as it is already used by something elsemikeyww wrote: ↑22 May 2021, 08:53Code: Select all
$F2::MsgBox, Test ; Use the "$" hotkey modifier #IfWinActive ahk_exe explorer.exe $Enter:: ControlGetFocus, fc Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}" Return #IfWinActive
Re: rename a file by selecting enter instead of F2
Do you mean that your script uses F2, or some other program uses F2?
Re: rename a file by selecting enter instead of F2 Topic is solved
Here is one idea.
Code: Select all
#IfWinActive ahk_exe explorer.exe
Enter::
If !file := Explorer_GetSelection().1
Return
SplitPath, file, oldfn, dir
InputBox, newfn, Rename file, %oldfn%,, 400, 125
If (ErrorLevel || newfn = "")
Return
If Instr(FileExist(file), "D")
FileMoveDir, %file%, %dir%\%newfn%
Else FileMove, %file%, %dir%\%newfn%
If ErrorLevel
MsgBox, 48, Error, An error occurred when renaming the file.
Else MsgBox, 64, Success, Done!, 1
Return
#IfWinActive
Explorer_GetSelection() {
; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256
WinGetClass, winClass, % "ahk_id" hWnd := WinExist("A")
If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass")
Return
shellWindows := ComObjCreate("Shell.Application").Windows, sel := []
If !(winClass ~= "Progman|WorkerW") {
For window in shellWindows
If (window.HWND && hWnd = window.HWND && shellFolderView := window.Document)
Break
} Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
For item in shellFolderView.SelectedItems
sel.Push(item.Path)
Return sel
}
Re: rename a file by selecting enter instead of F2
Thanks!! But I get this error message. Any ideas?mikeyww wrote: ↑23 May 2021, 10:19Here is one idea.
Code: Select all
#IfWinActive ahk_exe explorer.exe Enter:: If !file := Explorer_GetSelection().1 Return SplitPath, file, oldfn, dir InputBox, newfn, Rename file, %oldfn%,, 400, 125 If (ErrorLevel || newfn = "") Return If Instr(FileExist(file), "D") FileMoveDir, %file%, %dir%\%newfn% Else FileMove, %file%, %dir%\%newfn% If ErrorLevel MsgBox, 48, Error, An error occurred when renaming the file. Else MsgBox, 64, Success, Done!, 1 Return #IfWinActive Explorer_GetSelection() { ; https://www.autohotkey.com/boards/viewtopic.php?style=17&t=60403#p255256 WinGetClass, winClass, % "ahk_id" hWnd := WinExist("A") If !(winClass ~= "Progman|WorkerW|(Cabinet|Explore)WClass") Return shellWindows := ComObjCreate("Shell.Application").Windows, sel := [] If !(winClass ~= "Progman|WorkerW") { For window in shellWindows If (window.HWND && hWnd = window.HWND && shellFolderView := window.Document) Break } Else shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document For item in shellFolderView.SelectedItems sel.Push(item.Path) Return sel }
- Attachments
-
- 2021-05-23 17_25_16-MacKeyboard.ahk.png (56.5 KiB) Viewed 1141 times
Re: rename a file by selecting enter instead of F2
Yep. You have to include the function in the script. Scroll down in the posted code.
Re: rename a file by selecting enter instead of F2
cool, it works!
Not exactly as i had in mind but cool that this works Thanks!
Not exactly as i had in mind but cool that this works Thanks!
Re: rename a file by selecting enter instead of F2
Thank you for this @mikeyww
Re: rename a file by selecting enter instead of F2
Can I add code to this script so that when it triggers the rename function it also pastes text too? This is the original code and the second is the code I tried to add to it. I'm trying to add the content of the clipboard + a default text to the file name.
Code: Select all
#IfWinActive ahk_exe explorer.exe
!d::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}"
Return
#IfWinActive
Code: Select all
#IfWinActive ahk_exe explorer.exe
!d::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" : "{Enter}", clipboard " - Test"
Return
#IfWinActive
Re: rename a file by selecting enter instead of F2
I got this to work.
Code: Select all
#IfWinActive ahk_exe explorer.exe
!d::
ControlGetFocus, fc
Send, % fc ~= "SysListView321|DirectUI" ? "{F2}" :
Send, % clipboard " - Test"
Return
#IfWinActive