Automate VS Code, Google Chrome opening on Ubuntu Startup with Python

Automating the launch of frequently used applications can significantly enhance productivity and streamline your workflow. In this blog post, we will explore how to automate the opening of two popular tools, Visual Studio Code (VS Code) and Google Chrome, upon Ubuntu startup using Python. We will leverage Python’s subprocess and pathlib module to achieve this automation.

Prerequisites: To follow along with this tutorial, make sure you have the following:

Visit CodeClowns, to check out more such articles.

Step 1: Creating a Python Script

We start start by creating a Python script that will be responsible for opening VS Code and Google Chrome. Open your favourite text editor and create a new file, we can name it - startup_automation.py.

The Python Script

## startup_automation.py

from pathlib import Path
import subprocess

# VS Code working directory
dir = Path('/home/shunya/codeclowns/codeclowns-webapp')

def start(path):
    try:
        subprocess.Popen(['code', path])
        subprocess.Popen(['google-chrome-stable'])
        print("VS Code has been started.")
    except FileNotFoundError:
        print("VS Code is not installed or not in the system's PATH.")

# Call the function to start VS Code
start(dir)        

We start by importing the subprocess and pathlib module. The subprocess.Popen() function launches VS Code and Google Chrome opening commands.

The code command, is the command-line interface for starting VS Code. It checks if the code command is available in the system's PATH, and if it is, it opens VS Code.

The google-chrome-stable command, starts the Google Chrome browser. It checks if the google-chrome-stable command is available in the system's PATH, and if it is, it opens Google Chrome.

The are two ways, (you can choose whatever suits you) to ensure that our script runs automatically on Ubuntu startup.

Easy Way — Adding python script to Startup Applications

Follow the below steps:


  1. Save the startup_automation.py script.
  2. Open the Dash(super key) and search for “Startup Applications” to launch the Startup Applications Preferences window.
  3. Click on the “Add” button to add a new startup application.
  4. Provide a name for the startup application (e.g., “Startup Automation”).
  5. In the “Command” field, enter the path to the Python interpreter followed by the path to the startup_automation.py script (e.g., /usr/bin/python3 /path/to/startup_automation.py).
  6. Optionally, provide a description for the startup application.
  7. Click “Add” to save the changes.

The Hard Way

  1. Open a terminal on your Ubuntu system.
  2. Navigate to your home directory using the cd command:

cd ~        

3. Create a directory named .config if it doesn't already exist:

mkdir -p .config        

4. Change to the .config directory:

cd .config        

5. Create a directory named autostart if it doesn't already exist

mkdir -p autostart        

6. Change to the autostart directory:

cd autostart        

7. Create a new desktop entry file for your script using a text editor. For example, you can use the Nano editor:

nano myscript.desktop        

Replace myscript with a suitable name for your script.

8. In the text editor, enter the following content for the desktop entry file:

[Desktop Entry]
Type=Application
Exec=/usr/bin/python3 /path/to/your/startup_automation.py
Hidden=false
NoDisplay=false
X-GNOME-Autostart-enabled=true
Name[en_US]=Startup Automation
Name=Startup Automation
Comment[en_US]= Runs on OS startup [Automation]
Comment= Runs on OS startup [Automation]        

Make sure to replace /usr/bin/python3 and /path/to/your/startup_automation.py with the path of your local python installation and the actual path to your Python script, respectively.

9. Save the file and exit the text editor.

10. Finally, make the desktop entry file executable:

chmod +x myscript.desktop        

Conclusion:

Congratulations! You have successfully automated the opening of Visual Studio Code and Google Chrome on Ubuntu startup using Python. This will for sure enhance your productivity and have your essential tools ready as soon as you log in to your Ubuntu system. Feel free to explore further and customize the script to suit your specific needs and preferences. Happy automating!

To view or add a comment, sign in

More articles by Dev Ashish

Others also viewed

Explore content categories