Hi Paolo,
Since Aaron's svg-script function creates an empty \markup, you can use the properties oddFooterMarkup and evenFooterMarkup of the \paper block to have your script embedded at the end of every page. Look:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\version "2.19.83"
svg-script = #(define-scheme-function (body) (string?)
(markup #:stencil (ly:make-stencil (list 'embedded-svg
(string-join (list "<script type=\"text/_javascript_\"><![CDATA["
body "]]></script>") "\n")) '(0 . 0) '(0 . 0))))
script-for-every-page = \svg-script
#"var flat = document.getElementById('flat');
flat.style.color = 'red';
function spin(what, amount) {
let angle = 0.0;
return function () {
const bbox = what.getBBox();
const xform = ['rotate(',
(angle = (angle + amount) % 360.0),
bbox.x + 0.5 * bbox.width,
bbox.y + 0.75 * bbox.height, ')'];
what.setAttribute('transform', xform.join(' '));
}
}
setInterval(spin(flat, 5), 100);"
\paper {
oddFooterMarkup = \script-for-every-page
evenFooterMarkup = \script-for-every-page
}
{ g'4 a' \tweak Accidental.output-attributes #'((id . "flat")) bes'2 \pageBreak
g'4 a' \tweak Accidental.output-attributes #'((id . "flat")) bes'2 }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Love what your script is doing so far!
Hope that helps,
Stefano