Good catch. I don't think there is actually anything wrong with the Ruby script. There are some confusing interactions between Max, Java (ajm.ruby is Java), and Ruby. By the time we get to the Ruby script, the "\r" in "\relative" is being interpreted as a "return feed" character rather than a separate "\" and an "r". So this is a much deeper problem.
A potential workaround is to add this line to the top of the notate def:
str.gsub!("\r", "\\r")
But this will only fix the specific problem you mentioned. I think you will run into further issues if you need to use a backslash for other LilyPond commands. Probably the best solution for now is to use the extra backslash like you are doing.
Perhaps a regexp object could be used in the Max patch to replace "\" with "\\" but I can't seem to get that to work. I'll investigate if I can make some adjustments to ajm.ruby to prevent this, but the obvious solution (replace "\" with "\\" before passing things to Ruby) does not work either! Argh! The problem may ultimately lie with how textedit/Max handles backslashes.
And you are right, there's no particular reason to use Ruby for this, I was just testing out my ajm objects when I made this patch. Terminal scripting could work too.
RE: escaping backslashes
Good catch. I don't think there is actually anything wrong with the Ruby script. There are some confusing interactions between Max, Java (ajm.ruby is Java), and Ruby. By the time we get to the Ruby script, the "\r" in "\relative" is being interpreted as a "return feed" character rather than a separate "\" and an "r". So this is a much deeper problem.
A potential workaround is to add this line to the top of the notate def:
str.gsub!("\r", "\\r")
But this will only fix the specific problem you mentioned. I think you will run into further issues if you need to use a backslash for other LilyPond commands. Probably the best solution for now is to use the extra backslash like you are doing.
Perhaps a regexp object could be used in the Max patch to replace "\" with "\\" but I can't seem to get that to work. I'll investigate if I can make some adjustments to ajm.ruby to prevent this, but the obvious solution (replace "\" with "\\" before passing things to Ruby) does not work either! Argh! The problem may ultimately lie with how textedit/Max handles backslashes.
And you are right, there's no particular reason to use Ruby for this, I was just testing out my ajm objects when I made this patch. Terminal scripting could work too.