I am trying to execute Python scripts in terminal.
Running in Python shell it does what it is supposed to. It will run without error but nothing happens when executed in terminal.
Once this is figured out would there be a more useful way to get the program to enter the 'timeAide' & 'cancelSleep' strings into the terminal plus enter Mac password. I planned on importing 'pyautogui' to get all that portion done but is there something better.
#!/usr/bin/env python
#sleepAide: user enters a number to put the computer to sleep
#command for sleep: sudo systemsetup -setcomputersleep 60
#command to cancel sleep: sudo systemsetup -setcomputersleep Never .
#check python version in terminal: python --version
#shebang line: '#!/usr/bin/python3.6'
#type " 'nano' nameFile.py" in terminal to view code Ex: 'nano namefile.py'
class Sleep(object):
def __init__(self):
self.sleepAide()
def sleepAide(time):
timeAide = 'sudo systemsetup -setcomputersleep '
cancelSleep = 'sudo systemsetup -setcomputersleep Never'
time = int(input('In how many minutes would you like to sleep? '))
if time > 0:
print(timeAide+' '+str(time))
elif time == -1:
print(cancelSleep)
You are only declaring a class and methods. You need to instantiate the class for the __init__
function to be called. You can do so by adding the following at the bottom of the script, outside of the class definition:
Sleep()
There a couple of other problems.
self.sleepAide()
without a time
argument, but it doesn't look like you will need it since you collect it via input
self
in the sleepAide
definition but try to call it as if it were an instance methodI've made a couple changes below to get a working example:
class Sleep(object):
def __init__(self):
self.sleepAide()
def sleepAide(self):
timeAide = 'sudo systemsetup -setcomputersleep '
cancelSleep = 'sudo systemsetup -setcomputersleep Never'
time = int(input('In how many minutes would you like to sleep? '))
if time > 0:
print(timeAide+' '+str(time))
elif time == -1:
print(cancelSleep)
Sleep()
Run with the following:
$ python test.py
In how many minutes would you like to sleep? 10
sudo systemsetup -setcomputersleep 10
Keep in mind, this program doesn't actually execute the system commands, it just prints to the console. If you are looking to execute the commands, this post can help.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments