1) I don't know why you're using a "while = {}" loop to spam but one single ship each time.
2) Why don't you use an encompassing "while = {}" loop to actually have a looped action, like:
(just guessing because of my general programming skills; never tried this on a mod)
Code:
while = {count = 10 create_fleet = {}}
3) You want to add a limiting trigger (number < 10). If you just want to build 10 ships out of 0, you could study events that spawn fleets, rather.
If you really need to increment a variable and test its value, I guess that you could create a local variable. Look at the wiki (set_variable) for the syntax and possible operations.
Apparently, the Swarm is considered a country. Perhaps testing a country type instead of an ai type would work.
For both issues, check on_action_events.txt (e.g.). Good luck.