Shortly rename recordsdata in Google Drive with Apps Script and Google Gemini AI. The script will routinely rename the recordsdata with a descriptive title primarily based on the picture content material.
Do you might have picture recordsdata in your Google Drive with generic names like IMG_123456.jpg
or Screenshot.png
that provide no context about what the picture is about? Wouldn’t or not it’s good should you had an assistant that might take a look at these photos and routinely counsel descriptive filenames for the photographs?
Rename Information in Google Drive with AI
Effectively, you should utilize Google’s Gemini AI and Google Apps Script to routinely rename your recordsdata in Google Drive in bulk with a descriptive title primarily based on the picture content material.
The next instance makes use of Google’s Gemini AI however the steps could be simply tailored to OpenAI’s GPT-4 Imaginative and prescient or different AI fashions.
To get began, open script.new
to create a brand new Google Script and copy-paste the next code snippets within the editor. You may additionally wish to allow the Superior Drive API from the Google Script editor below the Assets menu.
1. Get the listing of recordsdata in a folder
Step one is to get the listing of recordsdata in a folder. We’ll use the Drive.Information.listing
methodology to get the listing of recordsdata in a folder. The search question incorporates the mimeType
parameter to filter the outcomes and solely return Drive recordsdata which can be picture codecs supported by the Google Gemini AI.
const getFilesInFolder = (folderId) => {
const mimeTypes = ['image/png', 'image/jpeg', 'image/webp'];
const { recordsdata = [] } = Drive.Information.listing({
q: `'${folderId}' in dad and mom and (${mimeTypes.map((sort) => `mimeType='${sort}'`).be a part of(' or ')})`,
fields: 'recordsdata(id,thumbnailLink,mimeType)',
pageSize: 10,
});
return recordsdata;
};
2. Get the file thumbnail as Base64
The recordsdata returned by the Drive.Information.listing
methodology comprise the thumbnailLink
property that factors to the thumbnail picture of the file. We’ll use the UrlFetchApp service to fetch the thumbnail picture and convert it right into a Base64 encoded string.
const getFileAsBase64 = (thumbnailLink) => {
const blob = UrlFetchApp.fetch(thumbnailLink).getBlob();
const base64 = Utilities.base64Encode(blob.getBytes());
return base64;
};
3. Get the urged filename from Google Gemini AI
We’ll use the Google Gemini API to investigate the visible content material of a picture and counsel a descriptive filename for the picture. Our textual content immediate appears one thing like this:
Analyze the picture content material and suggest a concise, descriptive filename in 5-15 phrases with out offering any clarification or extra textual content. Use areas for file names as a substitute of underscores.
You’d want an API key you can generate from Google AI Studio.
const getSuggestedFilename = (base64, fileMimeType) => {
strive {
const textual content = `Analyze the picture content material and suggest a concise, descriptive filename in 5-15 phrases with out offering any clarification or extra textual content. Use areas as a substitute of underscores.`;
const apiUrl = `https://generativelanguage.googleapis.com/v1beta/fashions/gemini-pro-vision:generateContent?key=${GEMINI_API_KEY}`;
const inlineData = {
mimeType: fileMimeType,
knowledge: base64,
};
const response = UrlFetchApp.fetch(apiUrl, {
methodology: 'POST',
headers: {
'Content material-Sort': 'utility/json',
},
payload: JSON.stringify({
contents: [{ parts: [{ inlineData }, { text }] }],
}),
});
const knowledge = JSON.parse(response);
return knowledge.candidates[0].content material.components[0].textual content.trim();
} catch (f) {
return null;
}
};
4. Robotically rename recordsdata in Google Drive
The ultimate step is to place all of the items collectively. We’ll get the listing of recordsdata in a folder, fetch the thumbnail picture of every file, analyze the picture content material with Google Gemini AI, and rename the file in Google Drive with the urged filename.
const renameFilesInGoogleDrive = () => {
const folderId = 'Put your folder ID right here';
const recordsdata = getFilesInFolder(folderId);
recordsdata.forEach((file) => {
const { id, thumbnailLink, mimeType } = file;
const base64 = getFileAsBase64(thumbnailLink);
const title = getSuggestedFilename(base64, mimeType);
Drive.Information.replace({ title }, id);
});
};
Google Scripts have a 6-minute execution time restrict however you possibly can setup a time-drive set off in order that the script runs routinely at a selected time interval (say each 10 minutes). You may additionally lengthen the script to maneuver the recordsdata to a unique folder after renaming them in order that they aren’t processed once more.
The total supply code is accessible on GitHub