The idea is basically to create a hotkey that will execute the selected text as code, with the addition of a few desirable features like a function library and hotkeys to pause/suspend/exit.
Here's a superior version with HotKeyIt's DynaRun()
DynaRun(TempScript, pipename="") { static _:="uint" @:=A_PtrSize ? "Ptr" : _ If pipename = name := "AHK" A_TickCount Else name := pipename __PIPE_GA_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0) __PIPE_ := DllCall("CreateNamedPipe","str","\\.\pipe\" name,_,2,_,0,_,255,_,0,_,0,@,0,@,0) if (__PIPE_=-1 or __PIPE_GA_=-1) Return 0 Run, %A_AhkPath% "\\.\pipe\%name%",,UseErrorLevel HIDE, PID If ErrorLevel MsgBox, 262144, ERROR,% "Could not open file:`n" __AHK_EXE_ """\\.\pipe\" name """" DllCall("ConnectNamedPipe",@,__PIPE_GA_,@,0) DllCall("CloseHandle",@,__PIPE_GA_) DllCall("ConnectNamedPipe",@,__PIPE_,@,0) script := (A_IsUnicode ? chr(0xfeff) : (chr(239) . chr(187) . chr(191))) . TempScript if !DllCall("WriteFile",@,__PIPE_,"str",script,_,(StrLen(script)+1)*(A_IsUnicode ? 2 : 1),_ "*",0,@,0) Return A_LastError DllCall("CloseHandle",@,__PIPE_) Return PID } +^n:: BackupClip := ClipBoardAll Clipboard = Send ^c ClipWait, .5 If ErrorLevel { SoundPlay %A_Windir%\Media\Windows Ding.wav Return } ScriptSource := Clipboard Clipboard := BackupClip ;The commented-out lines are shortcuts that I like - for example, I can type "m var" instead of "MsgBox, %var%" to save time. ;ScriptSource := RegExReplace(ScriptSource, "m)^\s+") ;ScriptSource := RegExReplace(ScriptSource, "m)^s ", "Send % ") ;ScriptSource := RegExReplace(ScriptSource, "m)^m ", "MsgBox % ") ;ScriptSource := RegExReplace(ScriptSource, "m)^si ", "SendInput % ") ;ScriptSource := RegExReplace(ScriptSource, "m)^l(\d+)", "Sleep $1000") ;ScriptSource := RegExReplace(ScriptSource, "\bcb\b", "ClipBoard") ScriptName := SubStr(RegExReplace(ScriptSource, "[/\\*?<>]+", " "), 1, 200) DynaRun("#NoEnv`r`n`r`nMenu, Tray, Icon, Shell32.dll, 13`r`nMenu, Tray, Tip, % """ . ScriptName . """`r`nOnExit, OnExit`r`n`r`n" . ScriptSource . "`r`n`r`nExitApp`r`n`r`n^;::Pause`r`n`r`n^Del::`r`nExitApp`r`nReturn`r`n`r`nOnExit:`r`nMenu, Tray, NoIcon`r`nProcess, Exist`r`nGroupAdd, This, ahk_pid %ErrorLevel%`r`nWinHide, ahk_group This`r`nSoundPlay, %A_WinDir%\Media\Speech On.wav, Wait`r`nExitApp`r`nReturn", ScriptName) return
The script that's actually executed will look like this (with my preferences):
#NoEnv #Include C:\Documents\AutoHotkey\Lib.ahk ;location of my functions library Menu, Tray, Icon, Shell32.dll, 13 ;custom icon - computer chip Meny, Tray, Tip, <automatically generated name based on parameters> OnExit, OnExit ;commands go here ExitApp ^;::Pause ^Del:: ExitApp Return OnExit: Menu, Tray, NoIcon SoundPlay, %A_WinDir%\Media\Speech On.wav, Wait ;plays a sound when done ExitApp Return
For example, if I type the text "MsgBox, Test", and then select it and press ^+n, a msgbox will pop up. When I press ok, or when I press ^Delete, the script will exit and leave no trace. It's really handy for executing test code in the forums, or for quickly bringing autohotkey functionality into another program. For example, if I need to type a list of 100 numbers in notepad, I can just type "loop 100 / send %A_Index%. `n", select it, and execute it. The send command will overwrite the code I just typed with the numbers, leaving a clean list. It's also handy for testing out snippets of code - for example, testing the appearance of a gui without running the whole script.