terminal command from Python in OS X - macos

Is there a good way to differentiate between OS implementation errors (I'm thinking about OSX's shipped version of python) vs a general error?
Is this raising an exception because it's clearly an Apple/OSX specific command and outside of normal script commands that subprocess.call expects?
If so, how should I get python to mimic the behavior of my typing that command out in terminal?
subprocess.call("defaults write com.apple.finder AppleShowAllFiles YES")

I use this command to edit plists via python (in this case, the Munki MangedInstalls.plist):
subprocess.call(' /usr/bin/defaults write /Library/Preferences/ManagedInstalls InstallAppleSoftwareUpdates 1 ', shell=True)
And this command to read the same plist:
os.popen(' /usr/bin/defaults read /Library/Preferences/ManagedInstalls InstallAppleSoftwareUpdates ').read()
(I realize this thread is a few months old but figured I would post this incase anyone is looking for this information in the future.)

Related

sublime text VS os terminal

I am an absolute beginner using (actually trying to) Mac High Sierra with Sublime3 and Python2.7. Just installed them.
When I test Sublime on terminal, here's what I get:
pc37:desktop flop$ one
-bash: one: command not found
I setup Sublime to write in Python and saved the file "one.py" on my desktop.
Does it come from Sublime or the OS?
I read a few debugging articles on the subject but didn't understand half of it... Didn't want to make the situation worst using wrong command lines.
Could someone help please? Thank you! Flo
You need to use valid Bash command syntax for executing Python scripts in Terminal:
python one.py
...or...
./one.py
Of course this requires you cd to ~/Desktop first.

Running Octave-cli on Iterm/Zsh/Omgzsh in Mac OSX

It feels impossible to get it to run on any custom terminal. I know that octave-cli.app is there but it always opens in the standard terminal. Any ways to execute octave scripts like a compiler (or) run it interactively like an interpreter from Iterm?
Using Mac OSX 10.9+
Edit:
I know how to export path variables. But having searched the web can't find a way to do it. Is it even possible? I even tried it using homebrew to no avail.
You can see the content of octave-cli.app, it's a script. Mine goes like this
open -a Terminal.app /usr/local/octave/3.8.0/bin/octave | logger 2>&1
It specify the terminal application used to open octave. This is the reason of your problem, as I think.
The solution is linking octave-cli in system path, better locates at "/usr/local/bin". like
ln -s /usr/local/octave/3.8.0/bin/octave-cli /usr/local/bin/octave-cli
Finally, octave can be accessed via any terminal(like iTerm) or shell(bash, zsh) by just type "octave-cli" command, which will be searched in system path and found to executed directly.

Is it possible to run a python program in the shell from the windows command prompt?

What I am trying to do is to make a start-up application in batch for a python program. At the moment, it launches into the windows command prompt (using python [ProgramName] %*), I don't mind this happening, but I would rather it launched into the IDLE shell instead. This is purely for appearance reasons (sorry, forgot the word that would fit best instead of 'appearance', I know it begins with a 't', but I'll edit it in when I remember), basically because it has some inbuilt colour schemes instead.
I have been Googling this for ages, but can't seem to find an answer, so any help is greatly appreciated.
Thanks in advance.
To start idle executing a file you can use:
python -m idlelib -r [ProgramFile]
^ not sure why this is -r but it executes a file.
^ start idlelib module
however this is not the true idle application but an instance of python running idlelib (may call the process python insstead of IDLE) so you can use -r flag with the IDLE executable instead:
/PATH/TO/IDLE.exe -r [ProgramFile]
I have not tested this on windows but was able to do equivalent thing on my mac.

How to disable stdin echo in windows console

I'm writting a Ruby program for windows, and I need to read user input from keyboard (stdin).
However, it's needed that the user key-presses are not automatically displayed in the windows console, and behave like "silent key presses"
This problem in Ruby over linux, can be solved using the "stty" linux command:
%x{stty -icanon -echo}
because it is the linux terminal who automatically outputs the user-keys into the terminal, so running the "stty" command tells the terminal to stop showing the user-key-presses.
But my program must run in windows, so I tried searching for a "stty" equivalent command for windows console, but still found nip...
?any suggestions, pointers ?
Look at Highline gem. To clarify, look at ask method and provide a block to silence it's output. It is well exemplified in their documentation

Start script on login

I have written a ruby daemon and I would like for it to run when I log in. It is normally run by going to the command line and calling ruby my_ruby_script.rb. How can I start my daemon on login? (Running 10.6 Snow Leopard).
There's an option to add applications etc that need to start at login, you could try writing a shell script or an apple script thing that launches terminal and runs ruby my_ruby_script.rb, or possibly even just add my_ruby_script.rb to this list after adding a #!/bin/env ruby line to the top of that file. http://support.apple.com/kb/HT2602?viewlocale=en_US gives precise instructions as to how to add an application to be started at login.
If you need to use AppleScript to actually start a terminal application (I believe this is not the case, but I am not in front of my mac now and hence can't test), just create an applescript file with something like
do shell script "ruby <path>/my_ruby_script.rb"
Hope this helps
As Panda said, add:
#!/bin/env ruby
to the begining of the file, and then you could add a reference to your file inside ~/.bashrc or ~/.profile or even /etc/profile , depending on your needs.
Check this out: https://stackoverflow.com/questions/3484429/profile-and-bashrc-doesnt-work-on-my-mac/3484472#3484472

Resources