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.
----------begin_max5_patcher---------- 762.3ocyWEsaaBCE8YxWABoIsollfsiaf9zplzdaOUsmVllHfahm.aD1oKsU 8ee3qIsztj.IqgBJAvWLliO2iO19gANdykqYJO2Kc+gqiyCCbbfPl.NUkc7x hVGmFofp4IX+QN+2dCsORyVqgv4EbgdSz7Hc7RtXwuJXwZaiSQi7G5RIglKj IlyX+Q9t+r5UtQJzJ98LSsQ3xGaCKVkIWoSYZ3i6WqxhnLnxdWUviR27k4IP rRDdNJz64FgK1zFHSrGGLvbZXK6yYLkJZA6e5zw4tmeq63uqXEpwQER8RLZ7 2j2xYpwWkHmyb+ZYarz8KWStNtfwDWKhxu22GMJSV9dWkmmxiizboPMN6txh M+VavPJWvhkqD.Pv6l2C.dexE.iSBwvE7gR7npn1P56xY1V2y6EsSy4jfslS vGQNYG5vn7blHoQg3DhUOhNNg3aFeL8MSitC9PACLuwUwzaUv8A0d4JX35D+ KLWPgz2Y1hdpYKsTcW1bYZi5GbHAzOguyLBte6wsGdLbJXEMsRgAWPAcJQlW vTLgFFOTCYXKXHXP06Wc5o1qN8iN01YErEr04tiNa7GGc1m1mrzGDjXBPqAj iSWR2IcN7U+OLcJgdx0o6QgNq7vpRg6NTkJx5BhCsygdAsGIUIA.zn9MIUom ZkpZIKsYeSpOvdjiblD79zmyiDKNTc4odBkpwuy7L+9rZ0bklqWoYkEmUcTd ayS2Lw5ORC5WiqonNh.Gc1kMY.ZIJqocuy+C2UqyyNccCqpytGLfiBIuuqfg 71ofRJj42vSedpgRPUs01mvz1r2sCpBfQXgf7YJpFcb.NPdacQBM1Ag2B1Q2 q1INfVS7W1qUxUEwa9tU1XtOC3DVoIi.lpnVcLaGtVkVxSRXh5apNimjKK0P OQYaMEzVHY1YSiXhzoPxXV0LMg6VZB2FZB0+Rclcx6h5LLQZUtaZ2xSz9GlL KztVZYGXh1sXpU4NTmpmZkW.sak33Vj5ncrUPabmn8OH8+XhWV3wA+U31IVY -----------end_max5_patcher-----------
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.
"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…
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.
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! :)
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.
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 :-/
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.
----------begin_max5_patcher---------- 939.3ocyYtrTaCCEFdcxSgFOC6.rt3qcGyvhtncUocSaGFGGQhK1VdrTfDX3 cuwR1PL3PTfX4vh.QVR4nOct7eHONdj0D1RJ2B7EvuAiF833QijCUMvn52Ox JKZYbZDWNMqXVVFMWXcp5YB5RgbbNCHlmvAEkrIQSRWAlxnbffwZlYxT47XS 92YnvlAugkKxixnxGcQYRTZySxWjkjmRExOUzFSmm7fb5H74vWlKagnYxMiV TR4qszHQBK+5RZrPcLcbpVH.gBp9Er9EveaVUjHddR9rstBjqbMX4hpVySiG W8xoeR.Jo28rxa4cQr.iPr2d1ccjmYnyg3nmQ47nYz2bzs+EKcw5GZ+8n3jb AiOG70Ks+ImVxsiJYh4Xj87Ho2DHKJeEfWDES6hS6Klv6ClPuuiEJvWRGO2N crTajXUAUMcKq2ymKvai8hPT3O3.ycQ0G5pyEKEcfxvADkXU.WH7.PxV6kav mBj4z6Wil2vwhxj7tPHBNf44TmaGTnto4Zs.kG2GNReKfhOmll1EnP8Inv5. JhJhSCmsSAVShxmogSW8dRb6CVtNM4prIrNwItOwoVwtXOuCVra8d4FZZLRF PLVWMg.QG.N1dy5IPB3fIcQQmAO1FGFbvisq2SBoOX4CofR5rtfoaeVUdGvD ohCI38HS466V1ZCcf8RIGYw4a.wEfytC7G0OVmva9qNFpKt6YB0PejjiDWYL MxuWoW2JzunnHMIV5lvsOoy9V7MQJzOTQEhJKnGw3szbUR10eKksZp8kT9sB Vg8Op5mAbEk+rJxzjbZLaQtXSmoVsPiN97HcUhrqqWiIHUzc+z0x8Q20gCm+ wGUbvtpNPj3PwFS0Ah2.8uMn9L6fgluYBWSn23iIcnFKjZ4XFuu.emiuzw0L A64OLh78gGeLoNQJAhG.85dgG8AP3vvgP7sWfITeu+5nc8T3.62+5n4rkZKj FTFIn.GGDD1INgGsEqIt0hD8Lup5cKRbKZsgvi2r6JmRRnem7TtCRMuu56hR tiUi2Fxb1hx3lO+mSZAdoWzoqoTRtryjVyB1ZVySlNkluoTlrjoEr0WE0lwV tz02pBz1pPFzpfZXUtFkT95vIngu8bz5xyr1jqt1jI8mzwlPlkSZ4PgMpMoU FJeyhIrN1ji4C6P6xlLa5opuIfcyIWyF1gzJmoQ4jVk6Hl8pCB0JtyvN4gZc 64a1qOcJtX35c9Zc6I0YYxJdg5ji5Up+5eVomfkvOgUs9MOM9+PZDct0 -----------end_max5_patcher-----------
Hope this works for you… you’ll see how the sprintf implementation works better when using absolute paths…
Then just adapt the absolute path.
@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.
@driftpattern: problem is that the shell object doesn’t retain the working directory… every command starts at / so you have to prepend cd
@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 "/".
Any luck Tim?
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 :)
Thank you community! Your contributions saved lots of time.
Best to all,
Forums > MaxMSP