sebinside / HotkeylessAHK

Using the power of AutoHotkey - without hotkeys!
MIT License
85 stars 8 forks source link

Support for Function Variables #11

Open GForceWeb opened 2 years ago

GForceWeb commented 2 years ago

Love what you've put together and am making good use of it with my streamdeck for a variety of different tasks.

One thing that comes up while building out my functions is that I'll often have a bunch of very similar functions where I'll have different streamdeck buttons to perform the same action with a slightly different output (ie. editing a specific field but with a different value).

I could simply my AHK code considerably if I could reuse the same AHK function across multiple streamdeck buttons and just subbing out the variable.

So you could call something like

http://localhost:42800/send/EscTest?tag=test

and then in AHK use that tag variable

EscTest(tag:=0) {
    ; Do something with the variable
    MsgBox %tag%
}

If you try this currently you just get 0 returned because the URL parameter isn't passed. I did also try using the full function call as you'd use in AHK but that doesn't work either

http://localhost:42800/send/EscTest(test)

Any way we could get this working to pass variables to the AHK functions?

GForceWeb commented 2 years ago

In playing around with this myself I found a way to get this working (at least enough for what I currently need). Rather than using URL query parameters (which I couldn't work out how to pass through to the AHK side) I instead use a pipe in my command and then separate the command from the args using string split.

It's almost certainly not the best way to do this and currently only works to pass a single argument (I expect that this is because the args variable is passed as a string in the %fn%(args) line so commas don't separate multiple arguments) but this works for me for now.

lib.ahk

RunClient() {
    shell := ComObjCreate("WScript.Shell")
    server := "curl http://localhost:42800/subscribe -m 25"

    ; Go in subscriber mode and wait for commands.
    ; You can trigger these commands by calling "localhost:42800/send/commandNameGoesHere"
    Loop {
        exec := shell.Exec(ComSpec " /C " server)
        command := exec.StdOut.ReadAll()

        ; Reset variables
        Out1 = 
        Out2 = 
        ; Separate arguments
        StringSplit, Out, command,|
        command := Out1
        args := Out2

        ; Special case: kill. Reserved to terminate the script.
        if(command == "kill") {
            Run curl ""http://localhost:42800/kill""
            Exit
        } else {
            ; Calls a custom defined function in any included script.
            ; Does ignore wrong calls (not defined functions).
            fn := Func(command)
            if(fn != 0) {
                %fn%(args)
            }
        }
    }
}

So I can hit http://localhost:42800/send/TestCommand|value and have the message box output value

TestCommand(var:="") {
    MsgBox %var%
}
hayden-greener commented 6 months ago

Hey there, I'm not sure if you're still following or working on AutoHotkey-related projects, but I wanted to let you know that I have a active pull request that includes a significant rewrite of the HotkeylessAHK project. The main changes include, migrating the codebase to AutoHotkey v2, as well as adding support for function parameters.

While I don't have complete feature parity with all the possible parameter types you can pass to a function, I've implemented support for strings and variables, for the time being, this is sufficient for my needs.

I wanted to share this with you in case you're interested in reviewing the changes or providing any feedback. Please let me know if you have any questions or suggestions.

name := "GForceWeb"
Class PersonalFunctions {

    HelloWorld(string, name)
    {
        msgbox "String: " . string . "`nVariable: " . name
    }

image image

GForceWeb commented 5 months ago

Heya @itslightmind , I still my own solution for this every day. I haven't looked into migrating my own AHK code to V2 as yet though.

It's great to see your pull request, those improvements look great. Hopefully, the repo owner will pop their head up and consider merging that in.

If they don't and you decide to fork the project instead let me know. I'll be keen to use it once I find the time to port my stuff to V2

hayden-greener commented 5 months ago

Sounds great @GForceWeb , will try to keep you updated!