-6

Can I use AutoHotkey to create a shortcut to edit script files?

I'm aware that you can hit Context+E, or right-click and Edit. What would be faster would be say holding Ctrl+Shft and double-clicking file. A good precedence for this is how you can hold Alt and double-click a file to shortcut straight to the Properties dialog. Or holding Ctrl+Shft and double-clicking a folder to open in a new window.

This would obviously go for language files like AHK, BAT, VBS, REG files, etc.

Nathan
  • 149
  • 1
  • 10

2 Answers2

2

Use Shell.Application.Windows to get the currently focused file in an Explorer window (not desktop).

; Alt + single click opens the file in notepad
~!LButton up::openFilesInEditor()

openFilesInEditor() {
    static shellApp := comObjCreate("Shell.Application")

    mouseGetPos x,y, clickedWindow
    winGetClass clickedWindowClass, ahk_id %clickedWindow%
    if (clickedWindowClass != "CabinetWClass")
        return

    try {
        shellWindows := shellApp.Windows()
        loop % shellWindows.count
        {
            win := shellWindows.Item(A_Index-1)
            if (win.HWND = clickedWindow) {
                focusedFile := win.Document.FocusedItem.Path
                run notepad "%focusedFile%"
                return
            }
        }
    } catch e {
        ;traytip,,Error %e%
    }
}

To use Edit from the context menu replace run notepad with run *Edit

The code was tested on Windows 7 and 10.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • Thanks for the reply. I cant get to work though.Could it be an inconsitency with our headers? I have `#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. #Warn ; Enable warnings to assist with detecting common errors. SendMode Input ; Recommended for new scripts due to its superior speed and reliability. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.` – Nathan Jan 09 '17 at 01:38
  • @Nathan Are you trying to open a file on your Desktop? The code doesn't account for that. @wOxxOm Instead of checking for the window class inside the function, you could put `#IfWinActive ahk_exe explorer.exe` before the hotkey. This will make the script work on Desktop (at least in Win7) and also avoid calling the function if not needed. Also, no reason to continue looping once we've found the window. – MCL Jan 09 '17 at 15:10
  • @MCL, 1) desktop is not listed in `Shell.Application.Windows` so it won't help 2) currently the script allows alt-clicking in nonactive Explorer windows so ifwinactive won't help 3) thanks, added `return` once the window is found – wOxxOm Jan 09 '17 at 17:46
  • On my W7 machine, your code works on the Desktop (ahk_class Progman). – MCL Jan 10 '17 at 08:24
  • I can't imagine how the desktop might appear in ShellWindows collection. Maybe you are using classic theme? – wOxxOm Jan 10 '17 at 08:42
  • I am indeed using classic theme. Does it not work on your machine if you remove the check for `CabinetWClass`? – MCL Jan 10 '17 at 08:44
  • It doesn't work. I've also checked Shell.Application.Windows collection manually - it's empty when no Explorer windows are open. – wOxxOm Jan 10 '17 at 08:50
-1

Ctrl + Double Click or Ctrl + Enter does the second verb on the right click menu, which for VBS files is Edit.

Keshan Nageswaran
  • 8,060
  • 3
  • 28
  • 45
Freddie
  • 26
  • 2