Best way to pass a parameter to and return values from a macro

I am writing a reusable macro which takes a CSV line and returns the column values.

At the moment I use global variables for both. Example:

 %variable%appLine%

is the input, then I call Execute macro and I receive the return values in

 %variable%appName%
 %variable%appViewLeft%

and

 %variable%appViewRight%

This sort of works, but I have two questions:

  1. Is there a more robust way? (I.e. telling me via synatx errors if I use wrong names)
  2. In the return I would love to have %variable%appViewLeft% and %variable%appViewRight% as a indexable array - something like %variable%appView[1]%

Can I somehow do this?

Have you looked at Keyboard Maestro Subroutines and the Return Result action ?

As you probably know, Keyboard Maestro Variable Arrays do provide index references, custom delimiters, etc.

manual:Variable Arrays [Keyboard Maestro Wiki]

2 Likes

I agree with Rob regarding subroutines. Having the parameters be defined in the action is nice.

As for returning multiple values, the "Return from Subroutine" action only allows you to return one value, but as Rob said, you could use a KM Indexed variable method to return multiple values. You could also return a JSON value.

But there's another option too: You could have the macro return Instance variables with the results. You mentioned using global variables as input parameters for your subroutine, but Instance variables are better because they go away when the macro(s) are done.

So in the same light, you could return the results in Instance variables, if that works better for you.

Feel free to ask questions.

1 Like

Thanks for these pointers! Will look into them.

I did not know about them. They look very promising and might help to simply my CSV handling immensely! (At the moment I parse the CSV lines via cut commands in Shell actions.)

Cool. I just found the Instance Variables in the manual. This might be a good option for me, so I don't have to change the current logic too much...

1 Like