Jump to content

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

Execute AHK code dynamically!


  • Please log in to reply
54 replies to this topic
Raccoon
  • Members
  • 178 posts
  • Last active: Oct 06 2014 05:58 PM
  • Joined: 02 Jan 2008
Now that we have dynamic functions, it feels right to add this into AHK. :)
Posted Image

Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #ahk

JoeSchmoe
  • Members
  • 304 posts
  • Last active: Feb 28 2013 05:39 PM
  • Joined: 17 Feb 2008
What do you mean, Raccoon?

Raccoon
  • Members
  • 178 posts
  • Last active: Oct 06 2014 05:58 PM
  • Joined: 02 Jan 2008
I'm just curious how if/when calling commands dynamically will be added as a built-in feature, and how it might improve or affect performance. AHK is after all one giant wrapper and interpreter, so it seems a fitting feature.
Posted Image

Need help right away? Get live support on IRC.
Already have an IRC client installed? /join #ahk

JoeSchmoe
  • Members
  • 304 posts
  • Last active: Feb 28 2013 05:39 PM
  • Joined: 17 Feb 2008
Ah, I see. What I find really helpful is being able to put simple AHK code into variables and then pass the variables around my script. I get a lot of mileage out of that. If one of the programmers implemented (Chris/Lexikos/Titan) implemented dynamic commands in a way that is similar to how dynamic functions are implemented, it wouldn't help me much, because I'd still need to have code that parses the variable and figures out whether to call it as a command or function, etc. My code also removes comments, substitutes in variables, and handles little multi-line scriptlets (it does simplified versions of all of these... it's definitely NOT a full AHK interpreter. But pretty much all I ever want to do is call a subroutine or a function, so this doesn't bug me). Further, it does it all in the context of the enclosing script, so that you can, for example, Gosub to any subroutine in your script! For a programmer to put that type of functionality into AHK would, I think, take a major rewrite, since so much of the processing of the code is done when AHK first starts up. I'm personally pretty happy with what I've already got.

That being said, I do want to emphasize that the code I posted is not documented or made into a nice slick library. I basically just ripped it out of my script and pasted it in.

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
Is there a way to modify this to make a dynamic settimer, but with a dynamic label?

JoeSchmoe
  • Members
  • 304 posts
  • Last active: Feb 28 2013 05:39 PM
  • Joined: 17 Feb 2008
Hi, Fry,

Yes, if I've understood your question, it should be able to handle it as is. I just tested it in the script that I have always running.

To see if I've understood you correctly, let me describe what I did to test it. In the script that I have always running, I have the ability to call up a dialog box that will accept a string from me that it will execute dynamically. In this script, I have a subroutine called Hello with the following text
Hello:
ToolTip, % "Hello. " A_Sec
Return
I also have a variable called "dynlab" with text "Hello."

The functionality that I think you are looking for is implemented using a function called ActiveText. ActiveText needs a list of global variables that it will substitute the values in for in dynamic commands. This list is called VariableNames and is near the top of the code. I added "|dynlab" to this list (it's pipe delimited) and made sure that Jonnycmds was included (all I needed actually was just the short subroutine for settimer).

After that, called up the dialog box and told it to execute the string "settimer, %dynlab%". Sure enough, up popped an annoying little tooltip saying hello and telling me the time. To get rid of it, I called up the dialog box and issued "settimer, %dynlab%, off" and then "tooltip, off."

That being said, since I haven't documented the code or tested how well it works outside of my script, if you are going to use it, you should really read through the top code segment I included to get a feel for it before trying it. If you'd like I can try to package it up nicely for you, but it might take a week or so. However, if I've understood your question, it should handle the functionality you want as is.

JoeSchmoe
  • Members
  • 304 posts
  • Last active: Feb 28 2013 05:39 PM
  • Joined: 17 Feb 2008
I just realized that I may have misunderstood your question.

My base code allows you to call code that is stored in a string dynamically. The ActiveText() function allows variable substitutions to be applied as the code is being called dynamically. Because of this, the subroutine that is called by SetTimer can be determined when the string is dynamically executed rather than when the string is originally constructed.

Tricks like this have been really helpful for me, but I'm beginning to think that you probably want the routine to be determined later on, when the timer is executed.

I think that this can be accomplished by storing the name of the subroutine in a global variable called "Current_Sub_For_Dyn_Sub" and using the following code:
SetTimer, Dyn_Sub

Dyn_Sub:
Gosub, %Current_Sub_For_Dyn_Sub%
Return
I'm too lazy to type it out, but you could, of course, make the code more stable by calling "If (IsLabel(Current_Sub_For_Dyn_Sub))" and using namespaces so that there isn't any conflicts with the global variables.

Another interesting idea: since Lexikos and Chris have implemented dynamic functions, you could also store the name of a function (as well as the name of global variables that hold arguments to the functions) in global variables as well. You could even have a global function that determines whether it should call the function or the subroutine.

Of course, the above subroutine can be called dynamically from a string using the code in this thread, or it could be called directly from your .AHK file as normal.

Fry
  • Members
  • 885 posts
  • Last active: Jan 17 2011 09:57 PM
  • Joined: 01 Nov 2007
Thanks for your input, JoeSchmoe, however, i dont think this is what i want.

Heres an example of a table of data.

F1|8|353
F4|6|642
F3|6|431

It gets parsed, and each column has its own variable.

I need to dynamically create a label, according to the amount of tables.

So using previous table, it would look something like this.

DynLabContents = Send,%Col1%|Send,%Col2%
execute("settimer",DynLabContents,ParseTime)

It makes labels according to the list, First two columns are keys to be sent. Last column is the timer delay.

I hope this gives you a good idea

JoeSchmoe
  • Members
  • 304 posts
  • Last active: Feb 28 2013 05:39 PM
  • Joined: 17 Feb 2008
Ahhh... Yeah, I figured that it couldn't be that easy.

I can think of a couple of cludgey workarounds, but nothing that I think that you couldn't think of yourself.

Maxmax14
  • Members
  • 23 posts
  • Last active: May 08 2011 02:52 PM
  • Joined: 30 Mar 2008
Hello,
Link to download "Execute.AHK" is not working anymore
Can someone send me "Execute.AHK"
Thank you
MaxMax14