MZ How to run Javascript when actor is next to act?

Sorry, wasn't sure how to phrase what I needed succinctly, but basically, I need to be able to run a small string of Javascript as an actor queues up to select their next action. Visustella lets you run script as an action begins, but I need it right as the command selection process begins.

To give more detail, my skill descriptions use icons to show what element said skill is. Now, there are various skills that use the actor's weapon to do them, so I had the idea to make it so that the displayed element icon changes depending on what the user's weapon's element actually is (my initial idea was to have an icon that meant "adopts the element of the user's weapon", and obviously I can just do that instead, but...). My process was going to be as follows:
1. run script as command selection begins to store the actor's ID in a variable (not terribly important for this specifically but it does tie in)
2. run script after that to identify the attack element of the actor's weapon and store it in another variable
3. run the second variable through a created text macro that returns a specific icon depending on the value of said variable (in battle only; outside of battle it'd display something else)
4. stick that macro in every relevant skill description

I know steps 3 and 4 would work, as I've done similar things in other areas of the game. But alas, I cannot act on those as I'm stuck at step 1.

Is there any recourse? I'm not averse to the use of other plugins, or to things that don't involve my original plan at all. I have a feeling I could honestly just use the macros by themselves to do all of this, but I wouldn't know how without any help.
Holy moly, never mind! I actually did it myself.

I'll go ahead and leave my code here, if anybody wants to do something similar. A few notes below, and they're important ones, so don't just grab and go, lol
if ($gameParty.inBattle()) {
if (BattleManager._subject.isActor()) {
myId = BattleManager._subject.actorId();
weapon = $gameActors.actor(myId).weapons()[0];
var element = 0;
for (var i = 0; i < weapon.traits.length; i++) {
if (weapon.traits.code == 31) {
element = weapon.traits.dataId;
break;
}
}}
if (element == 9) {return '\x1bi[76]';}
else if (element == 12) {return '\x1bi[77]';}
else if (element == 10) {return '\x1bi[78]';}
else if (element == 11) {return '\x1bi[79]';}
else if (element == 12) {return '\x1bi[90]';}
else {return '\x1bi[75]';}
} else {return '\x1bi[75]';}

1. first of all, credits to this thread for the base of the code I used for this, as well as ATT_Turan for alerting me to the existence of "BattleManager._subject" in another thread as well as Eli from Hakuen Studio for mentioning in another thread that strings work a bit differently from text boxes when it comes to text codes.
2. this might require Visustella's Message Core. It definitely requires a plugin for macros, I'm just saying Visustella Message Core is the only one I bothered to test since that's the one I'm using.
3. you'll want to go into that plugin's parameters, open up Text Code Replacements, then you'll want to make a new one. Name it whatever you'd like (I called it "WeaponElementIcon", but it really doesn't matter), and then paste this into the JS: Text parameter of that new text code.
4. this probably isn't optimized at all, I'll probably look into doing that on my own time. For now I'm just sharing my discovery.
5. this... might work outside of battle, too? If you look, I have it set to do separate things inside of battle and out (namely, display a default icon outside of battle), but maybe I don't need to. I'll have to look into that on my own time as well. EDIT: THIS IS NOT CORRECT. USE A DEFAULT ICON OUTSIDE OF BATTLE, LOL
6. THIS WILL PROBABLY BREAK IF YOU HAVE NO WEAPON EQUIPPED. Make sure you do one of the following if you don't feel up to altering this to make it work while unarmed:
a. just don't allow the player to be unarmed (possible with plugins)
b. don't let any skills with descriptions that use this text code be visible during battle unless they have a weapon (which kind of makes sense anyway)

Anyway, that's all I got! Sorry for the bother, lol

Original thread: https://forums.rpgmakerweb.com/threads/how-to-run-javascript-when-actor-is-next-to-act.167520/