It can but you need to utilize
Secure Templates, either a SecureActionButtonTemplate if the actionbar swap is meant to happen on a click,
or a SecureStateHeaderTemplate if the swap is meant to happen on some condition (only conditions supported by the macro system are valid).
It's going to be a bit uphill for a new programmer as these while simple in concepts are not very beginner friendly constructs.
Edit: I should tell you upfront that some of the things you want to do are not possible (just looking at events).
You cannot take action through code when you're silenced or a cast succeeded or a cast failed etc.
That falls under automation and Blizzard has specifically disabled the ability for addons to make such decisions.
Only conditions supported by secure frames are these:
Conditionals