Macro: Bulk Rename Macros w/ Search and Replace

Here is a semi functional macro I whipped up to quickly add and remove text from macro names. It's missing a ton of functionality but it does what I needed it to do. I'll post an updated version when I make improvements.

image

Requires:

  • Keyboard Maestro 8
  • Smile companion osax

As @JMichaelTX always says:

Carefully review the Release Notes and the Macro Actions
Make sure you understand what the Macro will do.
You are responsible for running the Macro, not me. :wink:

Download:[KM] Bulk Rename Macros with Search and Replace.kmmacros (30.5 KB)

The relevant AppleScript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set inst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
	set searchTerm to getvariable "Local__Search" instance inst
	set replaceTerm to getvariable "Local__Replace" instance inst
end tell

tell application "Keyboard Maestro"
	set macroList to every macro whose selected is true
	repeat with aMacro in macroList
		set oldName to name of aMacro
		set newName to change searchTerm into replaceTerm in oldName with regexp without case sensitive
		set name of aMacro to newName
	end repeat
end tell

2 Likes

Just tried it in KM 9.0.4. Doesn't work. I guess I have to manually remove " alias" from macros created by @DanThomas's Make Macro Alias macro. :pensive:

It is failing because it uses the satimage 0SAX.
This is easily fixed using the KM v8 Regex Replace command.
I’ll take a look at it later today.

Replace the AppleScript in the above Macro with this:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set inst to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
  set searchTerm to getvariable "Local__Search" instance inst
  set replaceTerm to getvariable "Local__Replace" instance inst
end tell

tell application "Keyboard Maestro"
  set macroList to every macro whose selected is true
  repeat with aMacro in macroList
    set oldName to name of aMacro
    
    ### Next statement requires Satimage.osax -- not compatible with Mojave+ ###
    --set newName to change searchTerm into replaceTerm in oldName with regexp without case sensitive
    
    ### Use KM Engine Replace Command ###  @JMichaelTX 2020-01-14
    set newName to my kmReplace(searchTerm, replaceTerm, oldName)
    
    set name of aMacro to newName
  end repeat
end tell

on kmReplace(pFindStr, pReplaceStr, pSourceStr)
  tell application "Keyboard Maestro Engine"
    set newStr to search pSourceStr ¬
      for pFindStr ¬
      replace pReplaceStr ¬
      regex true ¬
      without case sensitive
  end tell
  return newStr
end kmReplace
1 Like

@cfriend @JMichaelTX

thank you both for a very useful macro.

For the benefit of forum members, I would just like to mention that to add a prefix, type:
search: ^
replace: just type prefix