I tried to check how I could see if legend is showing or not, but couldn't figure out how it is being hidden by Show Legend() option. Show properties didn't help either.
So only thing that easily came to my mind is to get script of the graph builder as string and then check if it has Show Legend(0) in it... this won't work if user has preferences changed so you will have to check for them also. Something like this might work (and there hopefully is easier/more robust way to do this):
Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
gb = dt << Graph Builder(Variables(X(:Sex), Y(:Height), Group X(:Age)), Elements(Box Plot(X, Y)));
change_legend_state = function({gb_ref}, {Default Local},
legend_pref = Char(Get Platform Preference(Graph Builder(Show Legend)));
gb_script = Char(gb_ref << Get Script());
If(Contains(legend_pref, "Show Legend(0)") & !Contains(gb_script, "Show Legend(1)"),
gb_ref << Show Legend(1);
,
gb_ref << Show Legend(0);
);
);
wait(1);
change_legend_state(gb);
wait(1);
change_legend_state(gb);
-Jarmo