I think you want the method
g.getScript(). Look at its docstring in LeoPyRef.leo to see how to use it.
BTW, here is how I found it. I remembered that Leo scripts get run by writing a file called ScriptFile.py. So I searched for that using the Nav tab. The first thing that came up was in executeScriptHelper, so I looked at that hit. Its parent is the command execute-script. That seemed promising. Looking in
execute-script , I noticed a line script = g.getScript(c, p or c.p, useSelectedText=useSelectedText). So I searched for getScript, and that seems to be what you want.
This is typically how I find relevant code in Leo's code base. Others probably have their own ways, and mine is probably not the most "efficient" way to go about it. The Nav tab makes it feasible and convenient. It's a brilliant feature. The plugin's docstring says it was created by Ville M. Vainio, and I applaud him for it.