Attention, be careful! This script updates the description fields in media files. If you use custom descriptions, they will be overwritten. Do not run this script unless you fully understand its purpose.
Today I renamed hundreds of my media files and applied Media Verify tool to fix paths for all them. But descriptions were not changed. So I used this script to fix descriptions also. It uses path, extracts filename and overwrites media description:
[Gramps SuperTool script file]
version=1
[title]
Media Title and Integrity Checker
[description]
This Gramps SuperTool script checks and corrects the integrity of media records by ensuring that the media description matches the filename (excluding the extension). It iterates through all media objects in the database, extracts relevant metadata, and prints warnings if discrepancies are found. If a description is modified, the original value is preserved as an attribute named "Old title." The script then commits the changes to update the database accordingly.
### **Step-by-step functionality:**
1. The script retrieves the file path and current description of each media object.
2. If the description is missing, user is just informed in logs.
3. If the description does not match the filename, the script:
- Prints a warning.
- Updates the description to match the filename.
- Creates a new attribute named "Old title" and stores the previous description value.
4. The script applies changes to all media objects in the database.
5. All updates are committed automatically, modifying the stored data.
### **⚠️ Attention, be careful!**
This script updates the **description fields** in media records. If you use **custom descriptions**, they will be **overwritten**. A backup of the old description is saved as an attribute, but it is strongly recommended **not to run this script unless you fully understand its purpose.****strong text**
https://gramps.discourse.group/t/supertool-script-to-bulk-edit-media-descriptions/7026
[category]
Media
[initial_statements]
import os
def get_filename_without_extension(myPath):
return os.path.splitext(os.path.basename(myPath))[0]
print("-------------------Media Title and Integrity Checker----------------------")
[statements]
myPath = path.strip()
myDesc = desc.strip()
if not myDesc:
print(f"ID: {gramps_id}. Media '{myDesc}' (ID: {gramps_id}) has no desc.")
filename_without_ext = get_filename_without_extension(myPath)
if myDesc != filename_without_ext:
print(f"ID: {gramps_id}. Description '{myDesc}' != File name '{filename_without_ext}'")
# Create a new attribute
new_attr = Attribute()
new_attr.set_type("Old title")
new_attr.set_value(desc)
# Add the new attribute to media
obj.add_attribute(new_attr)
obj.desc = filename_without_ext
[scope]
selected
[unwind_lists]
False
[commit_changes]
False
[summary_only]
True