Run Python script on Mac terminal

D01001010

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)
Daniel Corin

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to run this script in mac terminal?

how to run matlab script in mac terminal

Combine consecutive commands into script and run in MAC terminal

Python script failing to run on Mac

Run a python script from the python terminal in Python

How can I run python in terminal on a mac?

Unable to run python files in terminal on Mac

run a python script in terminal without the python command

Open the terminal in python and run a python script

Execution python script in terminal or pvpython in mac

How to run a python script in a terminal upon login?

How to run python script on terminal (ubuntu)?

Run $Path command in Terminal in a python script

How to run a terminal command inside a python script?

Run a Python script when opening Terminal(xterm)

Trying to run my python script in terminal

How to run a shell script on a Unix console or Mac terminal?

How to run this simple Perl CGI script on Mac from terminal?

Run Python script on Terminal and keep using the terminal afterwards

How to run Python script with VBA on a Mac?

Setting up XAMPP to run python script on mac

Run a python file multiple times from Mac terminal

Python program fails only if run (via ssh) from a mac terminal

Why tkinter not working properly for python when file run with terminal in mac

Can't run a python program in terminal? [Mac OSX]

How to run python script on terminal without using python3?

Fully clear terminal on Mac OS X without exiting Python script

How to run python script with library inside Kernell terminal?

Run a shell script from terminal with string and python list argument