Spacing, backslashes, and shell

Jun 3, 2010 at 9:14pm

Spacing, backslashes, and shell

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 file.mov /Applications/myfile/my spacy file.mov 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.

– Pasted Max Patch, click to expand. –
#50713
Jun 3, 2010 at 10:47pm

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.

lh

#181909
Jun 4, 2010 at 2:57am

“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…

#181910
Jun 4, 2010 at 9:39am

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.

lh

#181911
Jun 8, 2010 at 1:30pm

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! :)

#181912
Sep 1, 2010 at 9:51pm

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.

#181913
Sep 1, 2010 at 9:55pm

I just posted a patch that shows how to escape the Max tokens, here:

http://cycling74.com/forums/topic.php?id=27951

Cheers,
Phil

#181914
Jun 19, 2011 at 3:58pm

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 :-/

#181915
Jun 20, 2011 at 12:05am

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.

– Pasted Max Patch, click to expand. –

Hope this works for you… you’ll see how the sprintf implementation works better when using absolute paths…

Then just adapt the absolute path.

#181916
Jun 20, 2011 at 1:43am

@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.

#181917
Jun 20, 2011 at 2:01am

@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 /).

#181918
Jun 20, 2011 at 5:33am

@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 “/”.

#181919
Jun 21, 2011 at 12:44pm

Any luck Tim?

#181920
Jun 21, 2011 at 2:21pm

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 :)

#181921

You must be logged in to reply to this topic.