And the best solution (if it works):

You could even define a shortcut with an automation "when my iDevice connects to any wifi network" with actions:

get wifi name copy it into a file (in Shortcuts folder)

This shortcut will run in background.

Your script, when it needs this wifi name, should only read this file (in external Shortcuts folder ) and should get the current wifi name.

To be tested, obviously

Édit: automation works, at each network update, not tested with a file but with clipboard