Skip to content

Instantly share code, notes, and snippets.

@richardsun29
Last active April 14, 2019 08:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save richardsun29/a9a5a402c85df7f50283 to your computer and use it in GitHub Desktop.
Save richardsun29/a9a5a402c85df7f50283 to your computer and use it in GitHub Desktop.
add support for Desktop and ahk_class variation
; This is part of my AutoHotKey [1] script. When you are in Windows Explorer it
; allows you to press Ctrl+Alt+N and type a filename, and that file is created
; in the current directory and opened in the appropriate editor (usually
; [gVim](http://www.vim.org/) in my case, but it will use whatever program is
; associated with the file in Windows Explorer).
; This is much easier than the alternative that I have been using until now:
; Right click > New > Text file, delete default filename and extension (which
; isn't highlighted in Windows 7), type the filename, press enter twice.
; (Particularly for creating dot files like ".htaccess".)
; Credit goes to aubricus [2] who wrote most of this
; [1]: http://www.autohotkey.com/
; [2]: https://gist.github.com/1148174
; Only run when Windows Explorer or Desktop is active
; Ctrl+Alt+N
#IfWinActive ahk_class CabinetWClass
^!n::
#IfWinActive ahk_class ExploreWClass
^!n::
#IfWinActive ahk_class Progman
^!n::
#IfWinActive ahk_class WorkerW
^!n::
; Get full path from open Explorer window
WinGetText, FullPath, A
; Split up result (it returns paths seperated by newlines)
StringSplit, PathArray, FullPath, `n
; Get first item
FullPath = %PathArray1%
; Clean up result
FullPath := RegExReplace(FullPath, "(^Address: )", "")
StringReplace, FullPath, FullPath, `r, , all
; Change working directory
if WinActive("ahk_class Progman") or WinActive("ahk_class WorkerW") ; Desktop
SetWorkingDir, %UserProfile%\Desktop
else ; Windows Explorer
SetWorkingDir, %FullPath%
; An error occurred with the SetWorkingDir directive
If ErrorLevel
Return
; Display input box for filename
InputBox, UserInput, New File (example: foo.txt), , , 400, 100
; User pressed cancel
If ErrorLevel
Return
; Create file
FileAppend, , %UserInput%
; Open the file in the appropriate editor
; Run %UserInput%
Return
#IfWinActive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment