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.
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
"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.
lh
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 just posted a patch that shows how to escape the Max tokens, here:
Cheers,
Phil
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.
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 ; 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 /).
@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,
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.
After grappling with this problem for a bit today, I came up with this hacky solution:
Any idea how i could send this message to the shell object?
echo '{ "command": ["set_property", "percent-pos", 10] }' | socat - /tmp/mpvsocket
It works when i send it from a terminal window on a Mac. It does not work, when i send the following in Max in a message box with escaped characters to the shell object:
echo '{ \"command\": [\"set_property\"\, \"percent-pos\"\, 10] }' | socat - /tmp/mpvsocket
thanks in advance
It works now. The command is used to control an external video player (www.mpv.io). When the video player is started from the shell (via mpv /path/to/videofile.mpg --input-ipc-server=/tmp/mpvsocket) it does not receive any more messages. I had to run a separate Max app and send the commands to control the playback position to a second shell object.
semi-newb here trying to learn how to format messages for shell (to run sox commands)... anyone willing to share more patches that I can poke around and learn from? Thanks!
hi marc,
i have no experiences with SoX, but did you check these?
the shell objects help file at the github repository (pasted as a max patch)
the shell objects tools page
these threads in the forum:
... another shell / aka.shell question :)
shell 2013 (2017 update, Windows version)
a search for regex and shell will find some more
Hi Karl,
Thanks for the help. I'm not understanding the syntax, and can't chain together commands beyond:
cd ~/folder\; ls
Certain things seem to work randomly, and others don't work. I'll keep on reading through everything I find. Hoping to run sox commands on a folder of files provided by user.
Thanks again.
Marc
So, I've made some progress. I'm using Javascript to format and send strings to [fromsymbol] then to the [shell] object, and it's working.
the string in js is formatted like so:
cd ~/someChildFolder\\\; mkdir ~/someChildFolder/newFolder
in my case, the actual js line looks like this (using a receive called 'toShell')
messnamed("toShell", "cd ~/repos\\\; mkdir ~/repos/newFolder");
this is working, so I imagine I'll be able to get sox working from here.
I'm most comfortable manipulating strings in JS anyway, so I'm happy with that method.
Fingers crossed I can get sox running from here.
Thanks to all contributors.
:(
Although I got basic shell commands working, after trying many different things, like listing full paths, etc, I cannot get sox to work via the shell object.
If anyone out there has a working patch that uses shell and sox (or any other method for running sox commands from max) I'd really appreciate a share.
Thanks.
Can't You post one command that You tried sending to sox via shell ?
Maybe the problem is simply that shell needs full path to sox executable.
sox bla bla ... will not work
/Volumes/Hd-2/SOX/sox bla bla ... should
Although sox works great via terminal, sox will not respond to any messages, with or without paths, sent to sox via shell in max. I can provide many basic sox/shell examples, but I don't think it's necessary. Does shell / sox work for you?
I'm on OSX 10.11.6, latest version of sox, shell, and Max 7.3
//[message] ->[shell]
pwd
//returns user folder to max console
mkdir newfolder
//a new folder is created in user folder
//via terminal, in user folder,
sox
//info is returned in terminal window
play audioFile.wav
//sox plays audio file
sox audioFile.wav audioFile.wav newAudioFile.wav
//sox creates a new audio file as expected
//[message] ->[shell]
sox
//nothing returned in max console
play audioFile.wav
//nothing played
play ~Users/userFolder/audioFile.wav
//nothing played
sox audioFile.wav audioFile.wav newAudioFile.wav
//nothing happens
ETC
Yes, I tried sox 14.4.1 with Max 6.1.10 on Snow Leopard, latest shell version.
just sending sox message to shell outputs usual gibberish with instructions.
/Users/Mac/Downloads/sox-14.4.1/play /Volumes/BigHd/SOOOUNDS/DOOR.aif
Message above plays the file happily...
See I did not use "sox play" , but the alias play
But also
/Users/Mac/Downloads/sox-14.4.1/sox /Volumes/BigHd/SOOOUNDS/DOOR.aif -d plays the file ok
I hope that this info helps You
and the message :
/Users/Mac/Downloads/sox-14.4.1/sox /DOOR.aif /DOOR.wav
converted DOOR.aif into DOOR.wav on the root of the HD
Hmm. Thanks.
There must be an issue with my instal or system.
It is quite a difference if one uses terminal or shell.
I think shell can talk only to systemwide preinstalled
executables, and even than, in my experience one has to
allways provide full path to in / out.
Using cd , and then working in a directory does not allways work.
With all 3rd party executables, one has to provide full path,
does not matter if they are installed in /usr/local or whatever
place where Terminal can call executables directly,
shell can't.
It makes life easier if one avoids spaces and special chars
in paths.
Sometimes shell can get blocked, in most cases when one opens
Terminal at same time to test if things which seem not to work
in shell do work there.
Than shell stops working, one has to reinitialise it, usually
retyping shell into object does refresh it.
Another problem is using sudo, I have not yet found a way
to enter password once one gets asked to do so,
but one can provide password in advance, before running sudo command.
Example for user with password yyy :
echo 'yyy' | sudo -S mkdir ~/Desktop/TEST
Just to avoid confusion - that "|" before sudo is punctuation char, ascii 124
All that works for me on system 10.6.8 - 10.11.6 and max 4 - 7.
I don't have any use for higher systems, so I don't test stuff there
Thank you for your help.
I have successfully gotten shell to do somethings - like create folders, run 'say' TTS, etc. but unfortunately, no matter what I send to shell, sox won't do anything. (the full paths that you mentioned don't work for me.) :(
If it's possible to get sox working via shell, I can't figure it out, so I'll need to come up with another solution to batch convert audio files and batch concatenate audio files.
Thanks for the help anyways!
Not that I am so interested in sox, but it does interest me why should something work for me and not for someone else...
I installed max 7.3.4 on el capitan 10.11. 6, placed sox 11.4.2 on desktop
and sent few messages to shell - and it just works.
It is on older macbook from 2009, I started Max in both
32 and 64 bit mode, sox is working in both cases.
Try this simple patch, You should get output from sox telling about usage etc
If You get some output, then shell -> sox works, You just have to get Your syntax correct
Wow. I don't know why this works - but it does - and you fixed my issues.
Endless thanks.
I am glad You have some success.
Back to the problem - shell is really different than
terminal in many aspects, in the patch above conformpath
set to use slash and boot option, which removes "Macintosh Hd:/"
from the path and leaves plain "/" for the root of the hd fixes
path for shell.
Unfortunately if shell does not find something caused by path which it
does not like, it outputs no error ...
Thank you again for sharing your wisdom on this. I tried many different things for hours and could not make progress on sox, while other things worked.
With great respect to the developer of the shell external, it would be awesome if C74 built a 'shell' object with advanced features.