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:
The Hard Way
Recommended by LinkedIn
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!