Action based on changing space

I have a macro that works continually on the frrontmost application. When I change to another space, I don't consider the previous application to be frontmost. Keystroke Maestro does (and allows many "frontmost" applications). Is there a way to redefine frontmost so it means frontmost only in the active space? Or, is it possible to Hide and Show an application when leaving or entering the space?