Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Custom: ahk() - Dynamically run AHK code in PowerShell


  • Please log in to reply
2 replies to this topic
Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

First off, I'm not sure where to post this as the Scripts Custom subforum is gone. Hopefully the mods would move this to correct forum(in any case it should not be here)

 

After downloading Git the other day, I discovered Windows PowerShell. I've heard about it but I was not aware that it comes with the OS. After playing with it for a couple of days, I came up with this function. This not written in AHK but rather in PowerShell scripting language(the original version uses both AHK and PowerShell but I rewrote it to optimize the purpose and reduce dependencies). This allows the user to dynamically run AHK code from within the console.

 

How it works:

It's basically HotKeyIT's DynaRun converted to a PowerShell script.

 

Remarks:

Tested on Windows 7 Ultimate(x86) using AHK v.1.1.09.02(Unicode) and PowerShell 2.0. I have not tested this with the ANSI version of AHK. See below for workaround.

 

Usage:

Copy the function and paste it in your PowerShell Profile. Start PowerShell, then type your AHK code:

PS C:\> $code =
>@" ; the @" ... "@ is sort of PowerShell's version of continuation section
>Gui, Add, Edit, w300 r5
>Gui, Add, Button, w88 h26, OK
>Gui, Show
>return
>GuiClose:
>ExitApp
>"@
>

 

Then call the function:

PS C:\>ahk $code ; pass the variable containing the AHK code(string)

 

That's itwink.png

 

Function: (Note: This is not written in AHK)

function ahk($script)
{
    $pipeName = "AHK_" + [System.Environment]::TickCount
    $pipeDir = [System.IO.Pipes.PipeDirection]::Out
    $maxNum = [Int]254
    $pipeTMode = [System.IO.Pipes.PipeTransmissionMode]::Message
    $pipeOptions = [System.IO.Pipes.PipeOptions]::None
    
    $ahkPath = [Environment]::GetFolderPath("ProgramFiles") + "\AutoHotkey\AutoHotkey.exe"
    
    $pipe_ga = new-object System.IO.Pipes.NamedPipeServerStream($pipeName, $pipeDir, $maxNum, $pipeTMode, $pipeOptions)
        
    $pipe = new-object System.IO.Pipes.NamedPipeServerStream($pipeName, $pipeDir, $maxNum, $pipeTMode, $pipeOptions)
    
    if ($pipe_ga -and $pipe) {
        Start-Process $ahkPath "\\.\pipe\$pipeName"
        $pipe_ga.WaitForConnection()
        $pipe_ga.Dispose()
        $pipe.WaitForConnection()
        $script = [char]65279 + $script
        $sw = new-object System.IO.StreamWriter($pipe)
        $sw.Write($script)
            
        $sw.Dispose()
        $pipe.Dispose()
    } else { Write-Host "Operation cancelled: Failed to create named pipe" }
}

For the ANSI version of AHK:

Replace this line:

$script = [char]65279 + $script

with:

$script = [char]239 + [char]187 + [char]191 + $script

 

Credits:

- Thanks to HotKeyIT for DynaRun

- Thanks to Lexikos for http://www.autohotke...through-a-pipe/

 

I haven't written any PowerShell script nor have any knowledge of .NET so pardon any mistakes in implementation, code, etc. Comments are very much welcome.wink.png

 

In action:

PS_AHK.png



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011

very nice, and i def think it belongs here



Coco
  • Members
  • 697 posts
  • Last active: Oct 31 2015 07:26 PM
  • Joined: 27 Jul 2012

@guest3456: Thankshappy.png