###Bug Report: Keyboard Maestro Does NOT Error if Macro Does NOT Exist in AppleScript
This command should fail if the Macro does NOT exist:
editMacro macroName
Running Keyboard Maestro 7.3.1 (7.3.1) on macOS 10.11.4
To add to my frustration, AFAIK there is no way to test for whether or not a Macro exists, when you have only the Macro name. And, I donβt see a way to get the Macro UUID from a Macro Name.
This script does NOT return an error, although it should if the Macro does NOT exist:
tell application "Keyboard Maestro Engine"
set macroName to getvariable "SCPT__MacroName"
end tell
set macroName to "My XXX BAD Macro" ### this macro does NOT exist
if (macroName β "") then
--βββββββββββββββββββββββββββββββββββββββββββ
try --=========== TRY ==============
tell application "Keyboard Maestro"
log macroName
set existsMacro to exists macroName
editMacro macroName
return "OK"
end tell
on error --======= ON ERROR =========
return "[ERROR] Script could NOT Open this Macro: " & macroName
end try --======== END TRY ==========
--βββββββββββββββββββββββββββββββββββββββββββ
else
return "[ERROR] KM Variable 'SCPT__MacroName'is empty/missing, so Macro can NOT be edited."
end if
Funny that you are mentioning this. I came across the same issue a couple of days ago while writing the EagleFiler Capture macro.
My clumsy workaround is to get β via UI scripting β the value of the name field of the macro opened by the editMacro command and check it against the should-be name.
So, I definitely agree: it should return an error.
Thanks. Iβm looking for a simple doesMacroExist(macroNameStr) test.
I could just search the plists file for the macro name, but I thought you might have a more elegant solution. Since the Keyboard Maestro Macros.plist file is very large (4MB on my Mac), Iβd like a quick, efficient solution.
tell application "System Events"
tell its application process "Keyboard Maestro"
tell its window "Keyboard Maestro Editor"
tell its group 1
tell its splitter group 1
tell its scroll area 3
tell its text field 1
if its value β (macroTemplateName & space & "copy") then
wrongMacroAlert()
error number -128
else
set its value to newMacroName
end if
end tell
end tell
end tell
end tell
end tell
end tell
end tell
on error
renamingAlert()
error number -128