Alright, here's what I got... it's not a flexible solution and it isn't even 100% accurate, although it should suffice for typical melee gameplay conditions, and it's the best I could do.
Unfortunately there's no simple built-in function to count the number of harvesters on a resource, and even though it's possible to access attribute values via triggers when (and only when) such attributes change, the harvester count shown in the unit info panel seems to be a special attribute that isn't detected by triggers. This is an area that Blizzard really ought to improve...
My solution consists of 2 behaviors, 18 validators for those behaviors (4 per worker, including MULE, to check harvesting status, plus two Combine validators - I tried to make a pun on "combine harvester" in the names, but couldn't think of anything funny), 1 trigger to apply the behaviors, and 4 functions to get harvesting information.
The first behavior, Harvester, is applied to harvesters and acts as a reference to the resource being harvested via the Unit Behavior Effect Unit function. The second behavior, Harvested, is basically the same, except it's applied to the resource from the harvester unit. The validators make sure that the behaviors are removed when necessary (I tried doing this through triggers at first, but it was much more convoluted – and probably also slower).
The Harvested behavior is allowed to stack up to 65,535 times (more than the amount of units that can be on the map at any given time, just in case you were worried), and the stack count simply gives you the number of harvesters via the Stack Count Of Behavior On Unit function.
To add a new harvest ability to this system, you'd have to:
- Duplicate the following validators, rename them and update the (Basic) Validator: Ability fields:
- Caster Is Gathering Target (Drone)
- Caster Is Returning Cargo (Drone)
- Target Is Gathering Caster (Drone)
- Target Is Returning Cargo (Drone)
- Add the first two to the Caster Is Harvesting validator, and add the last two to the Target Is Harvesting validator.
- Duplicate one of the Unit Uses Ability events from the trigger and update the Ability parameter.
This is assuming that no unit has multiple harvest abilities (otherwise you'd need more behaviors and more validators and more complicated triggers and functions).
A test map is attached to this post and if you want to check it out, there's a little debug inspector showing the number of harvesters on a resource. You can choose a different resource to inspect by pinging on it.