In your script you can assign the active window name to %N. The system variable for the active window is %H. So at the time the graph window in question is the active window you can assign %N to %H, i.e.,%N=%H;
Incidentally, the name and label of a window is also stored as a property of the page object. To get the current label of the current worksheet or graph:
%L=page.label$;
(Note a $ suffix is necessary since this property is a text property.)
If you wanted the label of your %N window, but it was no longer active, you could use:
%L=%N!page.label$;