First Python Project! Custom MacOS Notification Program
Tags: python
Published: 2021-07-30 18:45:07

As a beginner, I've been learning Python on Udemy, but procrastination is always a problem. I find that I was often taking breaks on my phone to check a mobile game I play.

The game has a standard energy bar that most mobile games have, where players spend energy points to make moves. This game in particular gave the player +1 energy every 6 minutes. With what I learned so far, I thought to myself, what if I had a variable that held the energy number and then had it notify me when my bar is nearly full? That way I would have less of an excuse to check my mobile game.

My checklist ended up being something like:
* have user input current energy
* add 1 energy every 6 minutes
* max energy is 70
* notify when energy is at 50, 60, and 70
* get time stamp for notifications
* work with MacOS Notification Center

I begin with the current_energy variable.
```
current_energy = int(input("What is your current energy? "))
```
Then, I needed to add 1 energy every 6 minutes. In my search to figure out how to get what I wanted, I found the schedule and time modules. These were super useful and had the documentation I needed down to days, hours, and minutes. It also showed examples of how to define a job. I setup if/elif statements at 50, 60, and 70 for the current_energy.
```
def energy_adder():
    global current_energy
    current_energy = current_energy + 1
    if current_energy >= 70:
        print(current_energy)
    elif current_energy == 60:
        print(current_energy)
    elif current_energy == 50:
        print(current_energy)
    else:
        print(current_energy)
```
paired with:
```
schedule.every(6).minutes.do(energy_adder)
```
I Googled how to make a custom MacOS notification. It involved using import os. I defined a new job for when the current_energy hit 70.

```
def energy70():
    title = "Your energy bar is at 70"
    message = "Your energy bar is FULL!"
    command = f'''
    osascript -e 'display notification "{message}" with title "{title}"'
    '''
    os.system(command)
```
I repeated this and changed some words for 50 and 60 with energy50() and energy60(). Soon, I had a program that would ask me the current energy I had in game, then every 6 minutes it would add an energy point to the energy bar variable. I set it so that if it hit 50, it would notify me. Same for 60 and then 70, which meant it was full. I also had it set back to 70 again instead of going to 71, since that was the maximum in game. 
```
def energy_adder():
    global current_energy
    current_energy = current_energy + 1
    if current_energy >= 70:
        energy70()
        current_energy = 70
        print(current_energy)
    elif current_energy == 60:
        energy60()
        print(current_energy)
    elif current_energy == 50:
        energy50()
        print(current_energy)
    else:
        print(current_energy)
```

I also had other ideas for notifications that I implemented as well:
* In-game currency event that refreshes every day at midnight
* Calendar of prizes that refreshes every day at 7pm
* Quests that refresh every day at noon

An example of one notification was this:
```
def job():
    title = "Objectives Refreshed at Noon"
    message = "Remember to do your objectives!"
    command = f'''
    osascript -e 'display notification "{message}" with title "{title}"'
    '''
    os.system(command)
```
paired with:
```
schedule.every().day.at("12:00").do(job)
```
The test notifications looked like this: 
![Screencap of example MacOS Notifications](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uw1hrgu0lnyqvgkj61qv.png)

Finally, I used [PyInstaller] (https://www.pyinstaller.org/) to wrap up my program as a simple executable to run in the terminal.

My code is on [GitHub] (https://github.com/amanda-mei/gamebarnotifier) , for all you beginner Python students and mobile gamers out there. 

Hopefully this will solve my issue with picking up the phone! Still, I'm pretty proud that I learned quite a bit about breaking down the goals and looking for what I needed with Python on this first project.