You can employ mzScript for this. Set up your mzScript rc to have a varfile and a scriptfile.. In the varfile have this:
TrayTicker 0
... then in the script have the following (note the pointy brackets in the gotoif lines):
*Script bang !IconAddedToTray
*Script gotoif ("%{TrayTicker}" = "0") lbl_noresize
; put your taskbar resizing routine here
*Script exec !VarSet TrayTicker 1
*Script exec goto end
*Script label lbl_noresize
*Script exec !VarSet TrayTicker 0
*Script ~bang
*Script bang !IconRemovedFromTray
*Script gotoif ("%{TrayTicker}" = "1") lbl_noresize
; put your taskbar resizing routine here
*Script exec !VarSet TrayTicker 0
*Script exec goto end
*Script label lbl_noresize
*Script exec !VarSet TrayTicker 1
*Script ~bang
Finally, to call the !Bangs defined in the script, in your systray.rc file (or step.rc if you're not shooting for OTS2 compliance) use these settings:
SystrayOnAdd !IconAddedToTray
SystrayOnDel !IconRemovedFromTray
Basically how this works is it has a variable it uses to judge whether it's an odd or even icon being added.. For odd icons (ie, the 1st, 3rd, 5th, etc) it simply increments the variable and does no resizing. For even icons (ie, the 2nd, 4th, 6th, etc) it decrements the variable back to 0 and resizes the taskbar..
When an icon is removed from the tray it checks the variable - if it's 1 (ie, if an odd icon has been added most recently) then it knows it'll have to resize the taskbar.. if it's 0 (ie, if an even icon has been added most recently) it knows it can just remove the icon and there'll still be the odd icon left so the taskbar doesn't have to be resized.
I haven't tested this but it's pretty rudimentary, I see no reason why it wouldn't work apart from the fact that systray2 seems to sometimes have troubles with the OnAdd/OnDel commands upon recycle.. That's not something that would have anything to do with this scripting though, that's just a shortcoming in systray2. You can get around this by adding this line to your script:
*Script start !NetReloadModule systray2-2.0
...but if your system tray is hooked into an LsBox that won't work, cause when you re-load the tray it will unhook it from its associated box. As long as your tray isn't mounted in an LsBox you should be good to go with that script though :)