DEV Community

Cover image for Automate Your Day with a Python Script
Snehal Rajeev Moon
Snehal Rajeev Moon

Posted on • Edited on

Automate Your Day with a Python Script

Hello Artisan,

I hope you are doing well, so today's blog is about creating a python script which is helpful in our day-to-day activity. However, to write a blog about the Python script had a little story behind it.

Somedays before I found myself in a situation where I had to maintain my daily activity logs in the system and because of my forgetfulness nature it often slipped through my mind and became hard to maintain, so I thought why not leverage Python's scripting capabilities which automates my task.

So I decided to write a script that will open a browser (the browser on which I work mostly) at a specified time with a designated URL, streamlining the log-keeping process effortlessly.

The best thing is that I have created one and whenever I start working I execute the script which will run in the background and open a browser at a specific time which is time-saving for me and now I am adding my log regularlyπŸ˜‚.

Motivated by this, I've embarked on creating a blog to share my Python script with you to automate your tasks too.

However, this script can be modified as per your needs. Like, if you want to promote breaks during extended work periods, you can add a YouTube link with your favorite song and it will open YouTube for you at scheduled intervals, offering a delightful respite from work.

So much of talking right!! πŸ˜„, let's discuss the requirements and coding practice.

Requirements:

  1. Make sure you have Python 3 installed in your system.
  2. Required module we need to install.
  3. schedule (used for scheduling the task)
  4. time (used for handling time-related task)
  5. webbrowser (provides a high-level interface to allow displaying web-based documents to users, with open() method it will open specified our browser).

Step 1: Make sure that your system has Python 3 installed in it if not you can download and install it in your system (https://www.python.org/downloads).

Step 2: After the installation is done install required modules using following pip3 command, make sure you have pip3.

 pip3 install schedule
 pip3 install time
 pip3 install webbrowser
Enter fullscreen mode Exit fullscreen mode

Step 3: Now create a file and save it as addActivityLog.py open and add the below given code save it.

import schedule
import time
import webbrowser

def open_browser_to_add_log():
   # name of the browser we need to open
    browser = webbrowser.get('chrome')

   # Replace 'https://google.com' with the URL you want to open
    url = 'https://google.com'
    browser.open(url)

def schedule_activity_logger():
    # Schedule the task to run every day at 6:20 PM IST
    schedule.every().day.at('18:00').do(open_browser_to_add_log)

    while True:
        schedule.run_pending()
        time.sleep(1)

if __name__ == "__main__":
    schedule_activity_logger()
Enter fullscreen mode Exit fullscreen mode
  • if __name__ == "__main__" this is the main function or we can say main block of the python script from where it starts execution.
  • schedule_activity_logger() is the name of the function where we have written our main logic, in python def is a keyword which used to define a function/method.
  • schedule.every().day.at('18:00').do(open_browser_to_add_log) this line is used to define that you need to run this script daily at 18:00 (06:00 PM - IST standard time), which calls another function open_browser_to_add_log which will open Chrome browser with a specific URL.

Step 4: The last step is to run the script, open a terminal, add the path where you have saved the above Python script, and run the script using the below command.

 python addActivityLog.py
Enter fullscreen mode Exit fullscreen mode

Conclusion, this Python script helps you to add a daily activity log by automating and overcoming forgetfulness. By scheduling browser openings at specified times, it offers a time-saving solution. Modify it to enhance your workflow, adding breaks with your favorite songs for a delightful workday experience. Hope this script proves as helpful to you as it has been for me!πŸ˜„πŸβœ¨

Top comments (0)