How to call a KM macro from a shell or Perl script


We are trying to use KM9 to automate an automated build system.

We use Perl as the main function to create an Ionic project. This works well, it initiates the Ionic project, pulls al the Cordova plugins in, creates the right files. However what it doesn't (and can't do), is setup Xcode correctly.

We need to use Xcode to add a Notification Service Extension for our Ionic client. There does not appear to be any way to use Xcode and any command line options to do this.

We have tried using AppleScript and Xcode is not a good citizen (and AppleScript is poor).

So we fired up KM9 and we can do just about everything quite easily. We can create the Service Notification Extension in Xcode with about 10 mins of work which is wonderful.

That's great, however we need to

  1. Execute the KM9 macro from Perl (or Shell or anything else we can use)
  2. Pass variables into the KM9 Macro as this is an automated job and it will be used for other projects. Hard coding two variables in is a no-no :slight_smile:

We have seen two Perl CPAN modules ( and simply doesn't work with KM9 and it seven years old. looks complex and we aren't sure if it will even do what we want it to do.

So, we asking for the advice of the forum. We know Perl and we know Bash very well, we are not experts in AppleScript or KM9. Searching the forum shows lots of code to execute shell scripts from KM but not so much the other way around.

Is there any easy way to do what we are after please? We need to execute a KM9 macro from the command line (or from Perl or Bash) and pass two parameters in.

Thanks in advance.


This action runs my Keyboard Maestro macro Python Test from Perl:


I think that's all you need.

You can get the code to run any macro in a variety of languages from this dropdown underneath the trigger settings:



Thanks for this. I'll try that out. I know osascript is available on the command line, so I can execute that from a pipe in Perl.

Now to find out how to pass variables :slight_smile:



Thanks for this. Much the same way as @mrpaslick BUT with the advantage of passing a parameter.

Much appreciated.


1 Like

Environment variables. From a longer Perl script of mine (where Keyboard Maestro variables are named TC_Something_or_Other):

my $voce = $ENV{KMVAR_TC_Voice};
my $highlight = $ENV{KMVAR_TC_Highlight};	# highlight color
my $src_str = $ENV{KMVAR_TC_Search_String};
my $StartFolder = $ENV{KMVAR_TC_Start_Folder};


Thanks for this. I have managed to get it all working now. It appears that osascript only allows one parameter to be easily passed otherwise you need a loop. It was no big deal to pass a single comma delimited argument which I then pass into KM, split it into the components and into variables and use that. After using Perl since 1990 (ish), regexp's hold no fear :slight_smile:

I have to say I'm impressed with KM and how it hangs together. I used to write languages (compiled and interpreted) many years ago for fun and profit, and it's been a long time since I have seen a visual language like KM work so well. It wasn't much slower dragging and dropping than writing it in Perl. I wasn't clear on a few things and need to look them up on the website, but it went well.

I like the group feature as I can record, group, deactivate and debug and repeat adding new things. It would be nice to have the degroup feature rather than having to drag the actions out of the group, but I can live with that.

My biggest problem was the way that Xcode interacts with the user, you have to double click a number of frameworks to add them and the only way to do that is to use the finder window and hope everything is in the right place. This is not a problem with KM, just that Xcode is designed to be used by a mouse and user and not to be automated.

Anyway, thanks for all the help,


Also just discovered that an extra space is passed at the end of the parameter passed in via osascript.


osascript -e 'tell application "Keyboard Maestro Engine" to do script "JambusterXCodeUpdates" with parameter "/Users/rwillett/Documents/src/Jambuster_Suite/BlackwallTunnel,Blackwall Tn"'

creates the parameter

"/Users/rwillett/Documents/src/Jambuster_Suite/BlackwallTunnel,Blackwall Tn " <-- Note the extra space.

I can easily get rid of this in the regular expression but I wanted to flag as

  1. Somebody else might come across it
  2. It might be a bug in osascript
  3. It might be a bug in KMV9.0.4

All the best


Agreed. It's become my user interface for all my Perl tools. I'd say it ought to baked into the OS but seeing how the OS has been baked lately, I'm glad Peter Lewis is still the chef.

Interesting note about the trailing space on the osascript parameter. Thanks for documenting that.