[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] "python myscript.py".unixCmd



Hi,


The String "python myscript.py“ is the command you want to execute with your system’s standard shell.
unixCmd is an instance method of the String class, documented here:
http://doc.sccode.org/Classes/String.html#-unixCmd

The method name suggests that it doesn’t work on windows.

A simple example might look like this:

In SuperCollider write:

"python ~/Desktop/hello_sc.py“.unixCmd;


The python script is expected to be sitting on the Desktop in this example, with a name of "hello_sc.py"
It could contain something simple like this:
#!/usr/bin/env python2
print("Hello SuperCollider")
print("This is the second Line")


If you want to get something back from your script, you can use .unixCmdGetStdOut or .unixCmdGetStdOutLines.
With the example script, the later would return an array of two elements (the two lines printed by the python script).

a = "python3 ~/Desktop/hello_sc.py".unixCmdGetStdOutLines;
a.postln; 
// -> [ Hello SuperCollider, This is the second Line ]

If you want to pass arguments into the script you could use string formating in SuperCollider (see http://doc.sccode.org/Classes/String.html#-format )

~myFirstArgument = 1234 // Whatever you want to pass in
"python3 ~/Desktop/hello_sc.py %".format(~myFirstArgument).unixCmd;

and on the python side you would use one of the several options to parse command line arguments.
Here’s a quick one:

#!/usr/bin/env python2 
import sys 
if len(sys.argv) > 1: 
print( "Your first argument was {}".format(sys.argv[1]) ) 
print("Hello SuperCollider“) 
print("This is the second Line")



I think that’s pretty much it. ._.

Best,
Eric



Am 03.01.2018 um 18:40 schrieb kewping@xxxxxxxxx:


Anyone have good example/tutorial for "python myscript.py".unixCmd

One of the reader said "Run python as a subprocess of SC, e.g. "python
myscript.py".unixCmd. "

This is because part of my code written in SC and python.




--
Sent from: http://new-supercollider-mailing-lists-forums-use-these.2681727.n2.nabble.com/SuperCollider-Users-New-Use-this-f2676391.html

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.birmingham.ac.uk/facilities/ea-studios/research/supercollider/mailinglist.aspx
archive: https://listarc.bham.ac.uk/marchives/sc-users/
search: https://listarc.bham.ac.uk/lists/sc-users/search/

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail