Spacing, backslashes, and shell

    Jun 03 2010 | 9:14 pm
    I know this has been covered here and there, but I have a long standing problem and I've finally reached my wits end and have not yet found a solution.
    How do I pass a file name with a variable number of spaces to a shell object so that I can, for example, copy to a folder with cp using absolute path names? So far I've been using sprintf's and regexp's but it's left me with a headache.
    Shell needs single backslashes in order to recognize file names with spaces but the moment I try to do something like cp /Applications/my spacy /Applications/myfile/my spacy Max surrounds it with quotes. I know why this happens, but is there any way to disable it in certain instances or is there any way around it.
    Normally if there's a set number of spaces I can use sprintf to do something like [sprintf %s %s %s] but if there's only two spaces or more than two it leaves me with a problem. Example of what I'm trying to do below.

    • Jun 03 2010 | 10:47 pm
      Have you tried [tosymbol] istead of escaping the spaces? I can't check currently but if you send the file paths with spaces through one it should treat each as a single symbol, enclosing it in "quotes" if needed.
    • Jun 04 2010 | 2:57 am
      "if you send the file paths with spaces through one it should treat each as a single symbol, enclosing it in "quotes" if needed."
      This is actually part of the problem I'm having. I can put it in quotes to pass it through sprintf as a single symbol, but then to get it into shell is impossible because you can't reinsert single backslashes for the file name spaces without Max turning the thing into a symbol with quotes.
      If I try using regexp to change spaces to include double quotes, then the space between the commands and the paths get garbled...
    • Jun 04 2010 | 9:39 am
      Try sending your target file and destination to a [sprintf] object formatted like this:
      [sprintf cp -v \"%s\" \"%s\"]
      This will force the escaping backslash and the "quote" if there are spaces in either of the paths or not and sending the output straight to [shell] should work just fine. No need to escape individual spaces at all.
    • Jun 08 2010 | 1:30 pm
      Hey I think this worked (at least it did in a very limited test, but that's more success than I've had)!!! I'm going to see if it applies to all the cases I've been working at and see if it does the trick.
      Thanks SO MUCH for the response! :)
    • Sep 01 2010 | 9:51 pm
      Can you post an example of this? I am having a similar issue from an external I built to find the my documents folder path. I tried the sprintf example above but it did not work for me.
    • Sep 01 2010 | 9:55 pm
      I just posted a patch that shows how to escape the Max tokens, here:
      Cheers, Phil
    • Jun 19 2011 | 3:58 pm
      I'm having this same problem, but Luke's [sprintf cp -v \"%s\" \"%s\"] isn't working for me ...
      Let's say I have this path:
      /Volumes/Macintosh HD/Users/Tim_Lloyd/Desktop/Space Test
      and I want to convert it to this:
      /Volumes/"Macintosh HD"/Users/Tim_Lloyd/Desktop/"Space Test";
      WITHOUT losing the ; at the end when I then combine it with:
      sox "Space Test.wav" "Space Test_44100.wav" rate 44100
      The resulting string has to have ALL but ONLY directories and filenames with spaces in the names enclose with "..." ... that's the bit that's confounding me :) Directories and files with no spaces in the name have to just remain as /name/... if that makes sense.
      Anyone have any tips?
      If I had any competence at all with a textual programming language, I wouldn't be trying to do this in max :-/
    • Jun 20 2011 | 12:05 am
      Let's see if we can't get you sorted out Tim.
      Instead of trying to do what you know is going to cause you the most trouble: namely cd and sox concatenated using ; let's cut out the middle man.... namely ; let's do this with an absolute yet "flexible" path in your file creation commands.
      Hope this works for you... you'll see how the sprintf implementation works better when using absolute paths...
      Then just adapt the absolute path.
    • Jun 20 2011 | 1:43 am
      @Tim Lloyd things would be easier if you add this sox utility to your /bin directory. then you can just open terminal and type "sox" to run it like other commands (like cd, cp, ls, ftp, etc). [shell] could execute the util regardless of the current working directory. just open terminal and type this (assuming sox is presently on your desktop):sudo cp ~/Desktop/sox /bin/sox hit enter and it will ask for your root password. then test it by typing "sox". if it does what its supposed to do (whatever that may be) you are done. if it complains about permissions, you will need to chmod and change the owner to root and group to wheel.
    • Jun 20 2011 | 2:01 am
      @driftpattern: problem is that the shell object doesn't retain the working directory... every command starts at / so you have to prepend cd ; sox rate 44100, but the ; makes things with spaces problematic so you'd be back to square one which is struggling with path names for the initial cd (otherwise you'd have to keep all of your files at /).
    • Jun 20 2011 | 5:33 am
      @cap10subtext: right. i was just adding that one can forgo the steps necessary to construct the path to sox by placing it in /bin and sox doesn't need to reside in the working directory or in "/".
    • Jun 21 2011 | 12:44 pm
      Any luck Tim?
    • Jun 21 2011 | 2:21 pm
      Yes! After playing around with Luke's [sprintf] and then finally realising why it works, and then your post which demos setting the input and output filepaths/names individually (instead of setting a single current directory) it's all working well :) ... then just a bit of faffing around before I remembered the symout argument for [sprintf] in order to retain folder/filenames with leading 0s ... and a discovery of [conformpath slash boot] instead of [regexp (:/) @substitute /] ... seems to be all working as of a few minutes ago!
      thanks a lot!
      ... I feel so out of practice and nooby with max now ... gonna take me a few days to remember everything :p
      @driftpattern - sox is already in /bin, but in order to process files from anywhere on my computer it seemed to require setting the current directory ... but it works if the in and output files are expressed as a full path rather than just a filenam ... just my confusion and ignorance about Terminal and [shell] showing :)
    • May 28 2015 | 1:54 pm
      Thank you community! Your contributions saved lots of time. Best to all,
    • Oct 02 2015 | 9:18 pm
      This is how I solved this problem
      It creates a new folder using the Windows version of [shell], [mxj DOShack]. It's taken directly from my work so just ignore the context objects.
    • Apr 18 2017 | 12:30 am
      After grappling with this problem for a bit today, I came up with this hacky solution: