Auto Rename Drive Images using Apps Script and Gemini AI

Introduction
In today’s fast‑paced digital workspace, a cluttered Google Drive can slow down collaboration and waste valuable time. Imagine a folder full of screenshots, scanned receipts, or product photos all bearing generic names like “IMG_1234.jpg.” Manually renaming each file to reflect its actual content is tedious, but with Google Apps Script and the new Gemini AI model, the process can be fully automated. This article walks you through setting up a script that reads an image, asks Gemini to generate a concise, descriptive label, and then renames the file instantly. By the end, you’ll have a reusable solution that turns chaotic image libraries into neatly organized collections, freeing you to focus on the work that truly matters.

Preparing the Environment
Before any code is written, you need a clean workspace in Google Drive and access to Apps Script. Create a new folder that will serve as the “watch folder” for incoming images. Open Google Apps Script (script.google.com), start a new project, and give it a meaningful name such as “Auto‑Rename Images.” In the script editor, enable the Google Drive API under Services so the script can read file metadata and perform rename operations. It’s also advisable to set the project’s OAuth scopes to include https://www.googleapis.com/auth/drive and https://www.googleapis.com/auth/script.external_request, ensuring the script can call external AI services.

Connecting to Gemini AI
Gemini, Google’s generative AI, can analyze visual content and return natural‑language descriptions. To use it, obtain an API key from the Google AI Studio console and store it securely in the script’s Properties Service (File → Project properties → Script properties). The following snippet illustrates a simple request:

  • Read the image as a Base64‑encoded string.
  • Send a POST request to https://generativelanguage.googleapis.com/v1beta/models/gemini-pro-vision:generateContent with the API key.
  • Parse the JSON response to extract the generated caption.

Because Gemini returns a full sentence, you’ll typically trim it to a short phrase (e.g., “Red sports car on highway”) and replace spaces with underscores for a file‑system‑friendly name.

Building the Renaming Logic
With the AI connection ready, the core of the script becomes a loop that processes each file in the watch folder. The workflow is:

  • List all files with MimeType.IMAGE that have not yet been renamed (you can tag processed files using a custom property).
  • For each file, download its binary content and call the Gemini function described above.
  • Construct a new filename: prefix + AI‑generated description + original extension. Ensure the name is unique by appending a timestamp if a clash occurs.
  • Apply the new name with file.setName(newName) and mark the file as processed.

Adding error handling—such as retries for network glitches or fallback to a generic name when Gemini returns an empty string—makes the script robust for long‑running batch jobs.

Deploying and Automating the Script
Once the logic is verified, schedule the script to run automatically. In Apps Script, open Triggers and create a time‑driven trigger (e.g., every 15 minutes) or a Drive trigger that fires when a new file is added to the watch folder. Test the trigger with a few sample images to confirm that names update as expected. For larger teams, publish the script as a Web App with appropriate permissions, allowing users to invoke the rename function on demand via a simple URL or a Google Workspace Add‑on.

Conclusion
Automating file renaming in Google Drive using Apps Script and Gemini AI transforms a chaotic collection of images into an instantly searchable library, saving countless manual hours. By preparing a secure script environment, linking to Gemini for intelligent image analysis, crafting a reliable renaming loop, and finally scheduling the process, you create a self‑sustaining workflow that scales with your data volume. This approach not only improves organization but also demonstrates the practical power of combining Google’s cloud scripting platform with its cutting‑edge generative AI. Implement the steps outlined above, and watch your Drive become a model of efficiency and clarity.

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Digital Malayali