Search code examples
autohotkey

How can I go about handling this raw arguments string and assoicate it with an action?


This is not concerning a programming error I am having but a design issue that I cant seem to get to the bottom of.

I wrote a AHK program that uses Search Everything, to execute small AHK programs/Utilities. On the search bar I can optionally type >: and everything after it will also be passed to the program to be run, as a raw string, At this point the program and everything are eliminated out of the equation.

HERE is a image to show what I am talking about, in this image when I execute Insert Frame.ahk without "Operator" >:, a single frame gets inserted, but with >:17 then 17 frames. >: is a feature I defined in everything, everything after it does not affect the search result.

The targeted AHK script, via Passing parameters to a script receives the arguments as a single raw string, up to now, I have only been passing a single argument, so its been straight forward, inside Insert Frames.ahk:

If (A_Args [1] = ""){  ;If not argument sent in, do the default action
    ClipPaintAutoAction("AnimInsertFrame", "1")  ;Insert 1 frame     This is a function that speaks to the animation softwares automation interface
    ExitApp
    }
Arg1 :=  A_Args[1]
ClipPaintAutoAction("AnimInsertFrame", Arg1) ;Insert nth Frames      This is a function that speaks to the animation softwares automation interface
ExitApp

I have been using this utility for less than a month and I was surprised, at how effective it has made me, so I have started using it in more software/packages and with that I expanded from passing 1 parameter\value to 3,

So Insert Frame.ahk can now accept up to 3 parameters, >:17,SpliTrack,ActiveLayer

I really wish to avoid making the command bar interface rigid by only accepting full parameter names, I am really trying to make the following entries equally valid:

  • >:17,SplitTrack,ActiveLayer
  • >:17,st,Alayer
  • >:17,s
  • >:17,,a

In the Insert Frame.ahk script:

;---------- Various examples of what the script recieves at run time -------------------------
    A_Args[1] := "17,SplitTrack,ActiveLayer"
    A_Args[1] := "17,st,alayer"
    A_Args[1] := "17,s
    A_Args[1] := "17,,a"

Arguments := Split(A_Args[1], ",") 

;----------  Various Post Split\Cleanup Examples ----------------------
    Arguments := ["17", "SplitTrack", "ActiveLayer"]
    Arguments := ["17", "st", "alayer"]
    Arguments := ["17","s", ""]
    Arguments := ["17", "", "a"]

;----------  Action to take ----------------------
    Do Action 0  --> If Arguments  = "" then do default  <---- This one I know how to perform :D
    Do Action 1  --> Insert 17 frames only
    Do Action 2  --> Insert 17 frames and SplitTrack only
    Do Action 3  --> Insert 17 frames and ActiveLayer only
    Do Action 4  --> Insert 17 frames and SplitTrack and ActiveLayer

In the script there can only ever be 5 possible actions to take, I am struggling with how I can logically identify the state of Arguments object and do its relative action, since Arguments variable can take quite a few different forms. I don't see how I can anticipate it or perform logical matching operations

I use command line tools and PowerShell heavily and this is something they excel at. I am hoping someone that is familiar with how they work can share some insight with me.

Any help would be greatly appreciated!


Solution

  • Since the condition for executing an action is only required that the Arguments is not empty. In AutoHotKey, a variable is treated as TRUE if it contains any value other than FALSE/ 0/ "".

    So, there are many ways to achieve your goal. For me, the first thing that comes to mind when I meet the if a is true, do a; if b is true, do b situation, will be organizing them into a Map object, but in your case, 2 simple array will works fine too.

    This is an example for Array: Using For-loop

    Arguments  := ["17"   , "SplitTrack", "ActiveLayer"]
    ActionFunc := [Action1,   Action2   ,    Action13  ] 
    
    For Arg in Arguments 
        !Arg ? default_Action() : ActionFunc[A_Index]()
    

    ! equal to not, so, !Arg means that the value of Arg is one of FALSE/ 0/ "".

    Learn more about the For-loop.