Easy Way to Convert Variables to Local Variables?

Is there an easy way to append Local__ to all variables in a given macro? I think @DanThomas was working on a plist editor to do something similar way back when, but it was never in a state he was pleased with.

I don’t know of an easy way – like find and replace – but the new Editor scripting support may make this a lot easier. I want to make this change as well, so I’ll be investigating it soon.


That's what I'd like, too.

First glance, it looks very doable!

Looks like we can get and set the Action xml, and here’s a sample xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">

As you can see, the variable name is clearly laid out in


But that is a simple case. It will be more challenging in Actions where the variable is used.

@peternlewis, can you offer us any guidelines or rules for identifying variable names in the XML in all Actions?

1 Like

In case anyone else would like to work on this, here's my proof-of-concept script to change the variable name in the first Action:

### REQUIRES Satimage.osax ###

tell application "Keyboard Maestro"
  set oMacro to item 1 of (get selected macros)
  set oAction to item 1 of actions of oMacro
  set actXML to xml of oAction
  set actXML2 to change "TEST__" into "Local__" in actXML
  set xml of oAction to actXML2
end tell


After I figure out the KM object model using Script Debugger, I’ll probably switch to using JavaScript for Automation (JXA), since it can read, write, and handle objects much better than AppleScript.

Not really, no, variable names could be anywhere in the XML, including in tokens. And things that have the same name as a variable might not be a variable. For example:


The TEST__StartTime could be a variable name, but it could also be plain text, or it could be plain text you are putting into a variable and then later using as a variable name.

And Keyboard Maestro uses lots of different keys and tokens what could be the same as your variable names, so just searching and replacing the XML will work only if you are 100% sure that your variable name is unique to you. So sure, change all the TEST__StartTime to Local__StartTime, I'm sure TEST__StartTime never appears anywhere else. But if you have a variable called UseFormat and you search and replace that to Local__UseFormat, then you are going to end up in problems.

My first stumbling block on this task:

I figured that. My above script was just as I stated: proof-of-concept.

I figured we would need to examine specific keys for specific Actions -- although that makes it very tough.

It may come down to asking for User OK for each individual change -- I don't know yet. Still testing and designing.

For my personal use, I'd guess that 99% of my macros all use a hopefully unique prefix for all variables used in that macro. So that should make it easy and safe to change macros that use a variable prefix.

Is there any way you could add scripting script to give us a list of variable names in a Macro, or even better, the list in each Action?

No. I can't even imagine how I would do that in general.

I can second Peter’s response - that’s unlikely to happen. When I was working on Search & Replace for action XML, I learned how hard this task would be.

That’s one of the reasons I always, always, always prefix variables with something fairly unique, because it’s easy to search for, and even replace if necessary.

With that said, if you know the names of your variables, and the names are unique and not likely to be duplicated by XML tags, then you could use them for a search and replace loop.

Just duplicate the macro first, in case you mess everything up. :slight_smile:

1 Like