Duplicating and Renaming a Macro With a Variable?

Do all these macros do the same things, just with different "base" directory and directory names for each project? If so, you might be better off having one macro that does "all the things" and your "project" macros call that with the appropriate parameters.

If you do want a macro for each then you could base an AppleScript on the following -- you can get the template macro's UUID from its AppleScript trigger and should be able to change the hard-coded values at the top of the script for some passed in from a previous Shortcuts action:

set newMacroName to "New Project Macro"
set projectName to "Project Number 3"
set projectBasePath to "~/Desktop/Project 3/"

tell application "Keyboard Maestro"
	set newMacro to item 1 of (duplicate macro id "E556A47F-ADF0-45C0-A080-4C7C66E3B741")
	set the name of newMacro to newMacroName
	
	-- Set the Project Name
	set AppleScript's text item delimiters to "TemplateProjectName"
	set theXML to every text item of (get xml of action 1 of newMacro)
	set AppleScript's text item delimiters to projectName
	set the xml of action 1 of newMacro to (theXML as text)
	
	-- Set the Project Name
	set AppleScript's text item delimiters to "TemplateBasePath"
	set theXML to every text item of (get xml of action 2 of newMacro)
	set AppleScript's text item delimiters to projectBasePath
	set the xml of action 2 of newMacro to (theXML as text)
end tell

Sample template you can use with the above (after you've changed the UUID to suit):

4 Finder Test.kmmacros (1.6 KB)

Macro image

...which will result in this new macro, in the same Macro Group folder as the template:

New macro image

1 Like