In the first script you’ll get a window handle. Since it’s a string, the easiest way would be to save it to your disc and load it in another script.
I’m not sure about the compare_legend method. Could you explain this use case a bit more?