Keyboard Maestro can be scripted to create macros (see Create Text Expansions Macro and Create Text Expansion From Selection Macro for examples).
You can get info about macros from AppleScript, but it is in XML format, so it is a little hard to work with. However for this purpose, simply searching the XML from *getmacros with asstring" to “>UNICODE<” would almost certainly be sufficient to detect whether there was a macro already.