ahk icon indicating copy to clipboard operation
ahk copied to clipboard

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

Open bclehmann opened this issue 4 years ago • 4 comments

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 avatar Mar 03 '21 03:03 bclehmann

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?

bclehmann avatar Mar 03 '21 03:03 bclehmann

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!

spyoungtech avatar Mar 27 '21 16:03 spyoungtech

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.

bclehmann avatar Mar 27 '21 21:03 bclehmann

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 avatar Apr 01 '21 03:04 spyoungtech

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

spyoungtech avatar Feb 05 '23 06:02 spyoungtech