How do I use parameters in a subroutine?

I'm trying to refactor a macro so that it uses a subroutine. Then I can reuse that subroutine. Unfortunately the documentation for subroutine https://wiki.keyboardmaestro.com/Subroutines doesn't mention anything. (A side note, IMO not having examples in the wiki severely limits the utility of the wiki for a new user)

I've searched through here and the answers are way above my current skill level. Can somebody give me a simple example of the code for a subroutine (e.g. executing a shell script) where the calling macro has passed several parameters into the subroutine)?

Well, I agree with that, but the KM forums are really where you should go to took for examples.

I'm searching the forums right now to see if I can find a good thread for you to read.

The 12th post in this thread is a good explanation of subroutines with examples.

But maybe more examples are needed. Subroutines are really handy for me. At least 10% of my macros are subroutines, I think.

Here's a totally contrived example that lets you input a shell command name, options for that command, and a folder path. The subroutine then runs the command with the specified options for the specified folder, and feeds the result through wc to return a count of lines in the output. That value is then returned to the main macro.

β€”β€” . test macros . β€”β€” Macros.kmmacros (7.4 KB)

Main macro screenshot

Subroutine screenshot

As I said, it's totally contrived and you'd get meaningless results (or an error) if you fed it a command that didn't generate line-oriented output and accept a folder path as input. But it demonstrates sending multiple variables to the subroutine, as well as returning a value from a subroutine.

-rob.

1 Like

Here's an example. I'm not really sure what kind of example you are looking for. This macro is a subroutine which will execute a shell script, passing the names of two files as local variables, and then returning the results. If this isn't what you want, give me more information and I can try again.

Subroutine: Find Difference between two files Macro (v11.0.3)

Subroutine- Find Difference between two files.kmmacros (2.6 KB)

OK, however I view forum answers as potentially being less reliable than a similar answer in the wiki. Anybody can answer in the forum. A poor or incorrect suggestion may be picked up, but it may also not be. A similar example in the wiki is far more likely to be corrected.

I did search the forum before asking, and that was the reason for requesting a simple example. I found several very comprehensive responses that were way too complex for a new user: e.g. being examples using AppleScript etc.

Keyboard Maestro looks like a very good product and is obviously loved by its users. I've bought a licence as proof of my belief in its value. However, the learning curve is very steep. I come from a software engineering background, and I view the documentation (wiki, tutorials) as seen from the perspective of a new user as being inadequate. As it stands, the wiki is merely an incomplete reference manual. The product has been around for some time, so I'm surprised so little effort has been expended on improving the documentation.

Sorry if this is a bit of a rant.

That's perfect, Rob, thanks.
I've found nothing about how to refer to parameters in subroutines, so this example is great.

That's fine.
All I needed to know was how to refer to the parameters. There's nothing in the wiki that I can find.