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

Adds TranspileOnly directive to print resulting AHK code instead of running it #116

Closed
wants to merge 5 commits into from

Conversation

bclehmann
Copy link

This is pretty simple, I modified a demo from the readme to get this:

from ahk import AHK
from ahk.directives import TranspileOnly
ahk = AHK(directives = [TranspileOnly])

ahk.mouse_move(x=100, y=100, blocking=True)

This code did not execute any AHK, instead printing it to stdout:

#NoEnv
#Persistent

CoordMode Mouse, Screen
MouseMove, 100, 100, 2
ExitApp

I'm unsure if you want directives like this to be mixed in with "real" AHK directives, so perhaps this is not an ideal implementation. Also if you like this idea it probably would make sense to have options to write the output to a file instead of stdout.

I look forward to hearing what you think, and thanks for the good work!

@bclehmann
Copy link
Author

bclehmann commented Mar 3, 2021

Looking at the test that failed I'm not sure that that was something I changed in this PR, although I'm by no means sure of that.

I'm noticing that it passed on basically the exact same code in the commit before (and the last commit reverts the file back to how it looks on master) so it may just be a flaky test?

@spyoungtech
Copy link
Owner

Thanks for this. This is a good idea I've been hoping to implement.

Though, directives are supposed to directly correlate 1-1 with a directive that exists in AutoHotkey. So, if I could request 1 change to this: that this be a keyword argument to the __init__ method of ScriptEngine and a standalone attribute be set on the class.

Thanks!

@bclehmann
Copy link
Author

Though, directives are supposed to directly correlate 1-1 with a directive that exists in AutoHotkey. So, if I could request 1 change to this: that this be a keyword argument to the init method of ScriptEngine and a standalone attribute be set on the class.

You got it. One issue remains, many scripts will do more than one thing, which means a script like this will print twice:

from ahk import AHK
ahk = AHK(transpile = True)

ahk.mouse_move(x=100, y=100, blocking=True)  # Blocks until mouse finishes moving (the default)
ahk.mouse_move(x=150, y=150, speed=10, blocking=True) # Moves the mouse to x, y taking 'speed' seconds to move

Result:

#NoEnv
#Persistent

CoordMode Mouse, Screen
MouseMove, 100, 100, 2
ExitApp

#NoEnv
#Persistent

CoordMode Mouse, Screen
MouseMove, 150, 150, 10
ExitApp

I'm not sure how to deal with this, because as far as I can tell you fire and forget AHK, you don't keep it around. In principle one could try and clean it up after the fact by removing duplicate directives and the ExitApp, but I'm not even sure if that is guaranteed to create a valid AHK script in all cases. For example, if one were to modify variables in one script, "glueing" those scripts together could create unintended results.

@spyoungtech
Copy link
Owner

spyoungtech commented Apr 1, 2021

which means a script like this will print twice:

Yeah, I think the result here would be intentional, since that's pretty much what is happening.

Also worth noting that there is logging setup to log the scripts as well in the current version:

import logging
logging.basicConfig(level=logging.DEBUG)
from ahk import AHK
ahk = AHK()
ahk.mouse_move(100, 100)

will produce logging something like:

DEBUG:ahk.script:Running script text: #NoEnv
#Persistent

CoordMode, Mouse, Screen
MouseMove, 100, 100, 2
ExitApp
DEBUG:ahk.script:Stdout: b''
DEBUG:ahk.script:Stderr: b''

Though, the action is actually taken.

Maybe this feature would be most useful by returning the script, rather than printing it, as there's currently no way of retrieving the script as a python string for every method.

So someone could do something like: print(ahk.mouse_move(100,100)) if they want it output to the console too.

I guess I wonder how people would intend to use this feature.

@spyoungtech
Copy link
Owner

Closing this as the project will have a completely new underlying mechanism in v1

@spyoungtech spyoungtech closed this Feb 5, 2023
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

Successfully merging this pull request may close these issues.

2 participants