Or to put it another way: why might you do so? I wonder what the context is.
I agree with @Nige_S that try is indeed probably the most straightforward way of handling a potential error, although I also wonder whether it might possibly suit you to call a macro that is always active and get it to deal with further conditions/routing.
Yes, I can do it this way. I was mostly just curious about approaches to my question, but it's not a big deal.
FWIW, groups have XML properties that could be useful here. For example, here's what the available application xml property looks like for the group I mentioned:
The context is running a macro via applesrcipt where the target macro is dynamic. If I were selecting it and/or running it manually, I would definitely follow your advice!
Right, yes... So of course the macro ID would not always be the same.
Again then I wonder whether it might be worth thinking about calling a globally accessible macro each time and using it to handle routing to the relevant macro. Is it possible to pass in parameters when calling a macro from Applescript? If so, the target ID could be passed as a parameter, and the global, handling macro could test the availability of the end macro.
Ignore me if that's convoluted, impossible or otherwise unsuitable... I just wonder what is possible.
Oh, FFS... I brain-pharted and only looked at the macro -- this is, as you rightly pointed out, a group property.
So you can get available application xml, parse that to make a list of bundle IDs, then see if that list contains the front app's ID. Demo -- replace the UUID in line 1 with your group of interest:
set groupUUID to "F8125C71-707B-42BC-A257-D607BECDDAC9"
tell application "Keyboard Maestro"
set theXML to available application xml of macro group id groupUUID
end tell
considering case
if theXML contains "<string>Included</string>" then
set AppleScript's text item delimiters to "<key>BundleIdentifier</key>"
set appList to every text item of theXML
set appList to rest of appList
set AppleScript's text item delimiters to {"<string>", "</string>"}
repeat with eachItem in appList
set contents of eachItem to text item 2 of (get contents of eachItem)
end repeat
else
--do nothing
end if
end considering
tell application (path to frontmost application as text)
set frontAppBundleID to id
end tell
if appList contains frontAppBundleID then
display dialog "Macros are available"
else
display dialog "No macros :-("
end if
The downside of this over the simple try method is, of course, that the KM Editor must launch if it isn't already running.
That is what I had in mind in concept when I said "or even identify the app, etc" but that is some slick parsing. I spent some time with your example Applescript and learned a few new tricks. Thanks for sharing Nige.