Quicker way to open script file for editing

392 views Asked by At

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.

2

There are 2 answers

7
wOxxOm On

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.

1
Freddie On

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