[AutoHotkey] Execute the input string and replace it with the output

This AutoHotkey script provides a hotkey Alt + = that you can press when Listary search box is present. It executes the string you’ve input in Listary search box in a command prompt, and writes the output to the search box.

Example

AHK-Listary-Alt =-001

Type date /t in Listary’s search box, then press Alt + =.

The search box will display “Please wait …” first, then “17-10-29 Sun” when the command finished, with cursor at the end.

Remark

  1. Any newline characters at the end of the output are removed, keeping those at other positions if present.

  2. The command is excuted in the %TEMP% directory.

  3. The hotkey Alt + = is handled by AutoHotkey in Listary Options window as well, preventing this window from receiving it. To set it as a Listary shortcut in this window, press and hold =, then press Alt. However, it’s rarely useful to set such a shortcut that Listary won’t receive while the AutoHotkey script is running.

  4. The command is executed in a hidden cmd window. So make sure you type a command that will finish by itself, otherwise it cannot be ended by closing a window. Until the cmd.exe process is ended, AutoHotkey doesn’t handle any other Alt + = pressed because AutoHotkey is still running one. Kill the cmd.exe process to solve this. Restarting the AutoHotkey script makes Alt + = available again, leaves the cmd.exe process still running, and results in the content of system clipboard unrecovered because of earlier clearing before starting the command.

Code (AutoHotkey)

; By DiamondbacK <http://discussion.listary.com/t/3794>
; [Listary]
GroupAdd, Listary, ahk_exe Listary.exe ahk_class Listary_WidgetWin_0
GroupAdd, Listary, ahk_exe Listary.exe ahk_class Listary_WidgetWin_1
#IfWinExist ahk_group Listary
!=:: ; Execute the input string and replace it with the output.
  listaryEvaluate() {
    /*
      Make Listary the “Last Found” window, but WinWait doesn't work for
      launcher mode. I don't know why.
    */
    IfWinActive ahk_group Listary
    {}
    ControlGetText text, ListarySearchBox1
    ControlSetText ListarySearchBox1, Please wait ...
    clipBak := ClipboardAll
    Clipboard =
    RunWait %ComSpec% /c "%text%" | Clip, %A_Temp%, Hide
    ControlSetText ListarySearchBox1, % RTrim(Clipboard, "`r`n")
    SendInput {End}
    Clipboard := clipBak
  }
#IfWinActive