Organize Your Screenshots on a Mac Automatically with python

Organize Your Screenshots on a Mac Automagically

Are you tired of having your desktop cluttered with screenshots? In this tutorial, I will show you how to use a Python script to automatically save your screenshots to a designated folder on your desktop. This will help keep your desktop organized and clutter-free.

Prerequisites

To follow this tutorial, you'll need:

  • A Mac

  • Python 3.x installed

  • Basic knowledge of Python programming

  • Terminal

Step 1: Change the Default Location of Screenshots

By default, screenshots on a Mac are saved to the desktop. However, we want to change this so that they are automatically saved to a designated folder. Here's how:

Open Terminal (you can find it in Applications/Utilities).

Type the following command and press enter:


defaults write com.apple.screencapture location ~/Desktop/Screenshots/

This will set the default location for screenshots to a folder named "Screenshots" on your desktop.

To apply the changes, you need to restart the SystemUIServer process. You can do this by running the following command:


killall SystemUIServer

This will restart the process responsible for the user interface elements in macOS, including the screenshot utility.

Step 2: Create a Python Script to Organize Screenshots

Now that we've changed the default location for screenshots, let's create a Python script that will move any new screenshots to a designated folder on your desktop.

Open Terminal and navigate to your desktop directory:


cd ~/Desktop

Use a text editor to create a new file and enter the following script:


import os
import shutil

desktop_path = os.path.expanduser("~/Desktop")
screenshots_dir = os.path.join(desktop_path, "Screenshots")
junk_dir = os.path.join(desktop_path, "Desktop Junk")
watched_folder = screenshots_dir

if not os.path.exists(screenshots_dir):
    os.makedirs(screenshots_dir)

if not os.path.exists(junk_dir):
    os.makedirs(junk_dir)

for filename in os.listdir(watched_folder):
    if filename.endswith(".png") or filename.endswith(".jpg"):
        src = os.path.join(watched_folder, filename)
        dst = os.path.join(screenshots_dir, filename)
        shutil.move(src, dst)
    elif filename != "Desktop Junk":
        src = os.path.join(watched_folder, filename)
        dst = os.path.join(junk_dir, filename)
        if os.path.isdir(src):
            shutil.copytree(src, dst)
            shutil.rmtree(src)
        else:
            shutil.move(src, dst)

This script moves any new screenshots taken to the "Screenshots" folder on your desktop, while any other files on the desktop are moved to a folder named "Desktop Junk". The watched_folder variable has been modified to point to the "Screenshots" folder, so any new screenshots taken will automatically be moved to the screenshots_dir directory.

Save the file as "file_organizer.py" on your desktop.

Step 3: Running the Script

To run the script, open Terminal and navigate to your desktop directory:


cd ~/Desktop

Then, run the following command:


python3 file_organizer.py

This will execute the script and move all your files to their designated directories.

Step 4: Automating the Script

To automate the script, you can set up a cron job to run it on a regular schedule. To

set up a cron job, open Terminal and type the following command:


crontab -e

This will open the crontab editor. To run the script every week, add the following line to the file:


0 0 * * 0 /usr/bin/python3 ~/Desktop/file_organizer.py

This will run the script every Sunday at midnight. Replace ~/Desktop/file_organizer.py with the path to your own file_organizer.py script.

Step 5: Creating an Alias to Run the Script

To make it easy to run the script whenever you want, you can create an alias. Here's how:

Open Terminal and type the following command:


nano ~/.bash_profile

This will open the nano editor. At the end of the file, add the following line:


alias orgdesktop='python3 ~/Desktop/file_organizer.py'

This creates an alias called "orgdesktop" that runs the script.

Save the changes and exit the editor by pressing Control+X, then Y, then Enter.

Reload your bash profile by typing the following command:


source ~/.bash_profile

This will update your terminal to include the new alias.

Now, whenever you want to organize your screenshots, simply open Terminal and type:


orgdesktop

This will execute the script and move all your screenshots to their designated folder.

And that's it! You now have a Python script that automatically organizes your screenshots, a cron job to run the script on a regular schedule, and an alias to run the script whenever you want. This will help keep your desktop organized and make it easier to find your screenshots when you need them.

Before:

After:

Why did the Python programmer quit his job? He didn't get arrays!

Did you find this article valuable?

Support Kyle Shelton by becoming a sponsor. Any amount is appreciated!