Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to call AHK built-in functions that don't use ()? #2

Closed
ssweber opened this issue Aug 23, 2023 · 3 comments
Closed

How to call AHK built-in functions that don't use ()? #2

ssweber opened this issue Aug 23, 2023 · 3 comments

Comments

@ssweber
Copy link

ssweber commented Aug 23, 2023

Hi. I love the simplicity of what you've created here.

I might have missed it, but is there anyway to call build-ins that don't use parenthesis?

Currently I'm just creating my own functions to pass along:

self.ahk = Script('''
WinActivate(title) {
    WinActivate, % title
}
Click(options) {
    Click, % options
}
''')

So I then do:
ahk.f('WinActivate', self.title)
or
ahk.f('Click', f"{x} {y}")

Thanks!

@CodeOptimist
Copy link
Owner

CodeOptimist commented Aug 23, 2023

Hi. I love the simplicity of what you've created here.

Thank you! The simplicity is the killer feature IMO. 😄

Currently I'm just creating my own functions to pass along

You have it correct. 😉

A little ironic since we're wrapping commands and this is ahk unwrapped 😅, but it's to a minimal degree at least 😁. I've also found that I often end up slightly embellishing them past simple wrappers, per-project, anyway. Users are also welcome to create themselves a re-usable "library" of course, or as-they-go. Thankfully I don't think it would be too long.

Also thank you for pointing out something that needs explicitly added to the documentation. 👍

@ssweber
Copy link
Author

ssweber commented Aug 23, 2023

Awesome. I think it's best to keep these simple. You might consider adding a short example of passing multiple args (which took me a second to figure out as well 😄 ):

return self.ahk.f("WinActive", "Window Name", "Window Text Here")

I've already wrapped MouseGetPos to return via Jxon_Dump

MouseGetPos() {
    MouseGetPos, OutputVarX, OutputVarY, OutputVarWin, OutputVarControl, 1
    result := [OutputVarX, OutputVarY, OutputVarWin, OutputVarControl]
    return Jxon_Dump(result)
}
@dataclass
class MouseGetPosResult:
    x: int
    y: int
    window: str
    control: str

    def __post_init__(self):
        self.x = int(self.x)
        self.y = int(self.y)
def mouse_get_pos(self):
    res = self.ahk.f("MouseGetPos")
    return MouseGetPosResult(*json.loads(res))

And the useful FindText:

class Finder:
    def __init__(self):
        self.ahk = Script(
            """
#Include ./lib/findtext.ahk
#Include ./lib/jxon.ahk

Search(window_title, search_arg, find_all) {
    WinGetPos, pX, pY, pW, pH, %window_title%
    if (result := FindText(outX, outY, pX, pY, pX+pW, pY+pH, 0.05, 0.05, search_arg,,find_all)) {
        response := Jxon_Dump(result)
        return response
    } else {
        response := Jxon_Dump("")
        return response
    }
}
"""
        )

    def __call__(self, window_title, needle: str, find_all: bool = False):
        # validate/transform inputs
        find_all = 1 if True else 0
        result = self.ahk.f("Search", window_title, needle, str(find_all))
        return json.loads(result)

finder = Finder()

@ssweber ssweber closed this as completed Aug 23, 2023
@CodeOptimist
Copy link
Owner

CodeOptimist commented Aug 23, 2023

You might consider adding a short example of passing multiple args

Whoops! Superb suggestion.

#Include ./lib/findtext.ahk
#Include ./lib/jxon.ahk

Love seeing things I've never tested (#Include) just work. 😂 (But of course they do, it's just AutoHotkey.)

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants