Subroutine with optional parameters (=default value variables)

Hi KM community,
I'm just fiddeling around with a macro that should configure an application that I use often (but does not save its layout). I move the main window and do some clicking in it... nothing special. I'd like to have this happen in two (trigger-)cases, though:

  1. Whenever I start the app (trigger application launches)
  2. As a subroutine with parameters (e.g. window position)
    So here's my question: Is it possible to add parameters to the subroutine triggers but give them default values if the parameters are not available (e.g. the macro is triggered by another trigger) and (only) in these cases use these?

I actually know a functionality like that from PHP functions where you can just write something like

function dosomething($var1, $var2, $var3 = 'somevalue'){
    // Rest of function here...
}

In this example var3 would be optional as it has a default value which will be used if it is not set in the function's call.

If there is no simple way, any ideas how to workaround the problem (other than copy the whole macro for each trigger and maintain both versions in parallel)?

Thanks for your help in advance!

Sure. You can add an If/Else for each variable at the start of your subroutine. If the variable is empty, set it to something. Here's an example from my Prompt With List... on Steroids macro.

2 Likes

Sorry for replying much slower than you did... and thanks for your solution! Quite simple solution for the "optional parameter problem" I had.

I tried it in my environment and since I combined it with the application launches trigger, I had one more problem to solve: My application-setup of course needed the application to run and if not to start it but this would trigger the same macro again. It worked though to just use the deactivate macro group action for the corresponding macro group before the "activate MyApp" and activate it again afterwards.

I wonder why there is no deactivate macro (or trigger) action but just the groups? Maybe for safety to keep macros from reactivating manually deactivated macros... but the (de-)activate Macro Group action does not even allow a token/variable like %executingMacroGroup%. No problem to hardcode the Macro group in, but as soon as I put the macro into another group it won't act as it should anymore... we'll see when that might happen.

Well, wonder no longer - there is one. It's the Set Macro or Group Enable action described in the KM wiki action:Set Macro or Group Enable [Keyboard Maestro Wiki].

It looks like this:
image

so you can enable/disable/toggle individual macros and groups.

1 Like

Great and thanks, that happens if you are tied to one search term (I always searched for "activate" instead of "enable"). :face_in_clouds:

Thanks for helping me out to both of you! :smiley:

1 Like