Double-Quote Workarounds in Max/MSP (part 1)
Recently, I was in the process of building a data parsing mechanism for a Max/MSP sampler I’m currently working on (hopefully to appear shortly on this site)!
In so many words, I essentially needed to send a command to [shell], receive the data, and then make slight adjustments to make it readable and useable in the future functions of the patch. In this case, I was specifically after the duration of a YouTube video based on HTML metadata without the use of the YouTube API (to reduce calls and keep the patch easy to share without the need for users to generate their own key).
The bash code is simple, but it requires the use of double quotes to execute properly. It is certainly possible there is another way to access this data from the immense collection that is the full HTML file of a url, but for the sake of this post and solution, I will stick with the current limitations to show a workaround in Max.
If you’ve read my previous blogposts, you’ll notice that I have spent time trying to parse and sort data in Max/MSP - most recently, getting commas OUT of strings copied from a .txt file.
Max was of little help, and in that case I turned to the [text] object to (indirectly) parse commas out. Part of the problem is that the comma is a reserved character in Max - working with it directly does not go as planned as its role in the software’s structure is preordained. Something similar occurs with double quotes.
The problem is this message:
curl -s <YouTube video url> | grep -o ‘lengthSeconds”:”[0-9]*'
Normally, I would prefer to send this as a simple message object to the [shell] external, making it launch from terminal. Although, in Max, when you put a text such as this into a message object, the two double quotations surrounding the colon disappear when the message object is instantiated.
There are two solution to this problem. In this case, the code will still work with the inclusion of a backspace ( \, escape character) placed before each double quotation -
curl -s <YouTube video url> | grep -o ‘lengthSeconds\”:\”[0-9]*’
-or-
…this command can be saved in a .txt file and accessed in the [shell] external by changing the message box’s contents to read: path/to/the/file/ bash text.txt, which preserves the double quotations as they never actually touch the message box in Max.
Accessing commands via text files seems cumbersome, but it allows for some greater flexibility when interacting with the [shell] external object since Max has a process in place for removing quotations or not wishing to parse commas in an expected fashion.