0

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)
Community
  • 1
  • 1
D01001010
  • 3
  • 1
  • 3

1 Answers1

0

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.

  • You call self.sleepAide() without a time argument, but it doesn't look like you will need it since you collect it via input
  • You do not pass self in the sleepAide definition but try to call it as if it were an instance method

I'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.

Daniel Corin
  • 1,735
  • 2
  • 12
  • 24