SuperTool script to bulk edit media descriptions

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

1 Like

let’s simplify the Script 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 missing, the script assigns the filename (without the extension) as the new description. The script also commits changes to update the database accordingly.

It is about “integrity” or “harmonization”? A rough read-through indicates that it over-writes Titles that were not blank.

“Harmonize media Titles with filenames, but contains a potentially destructive feature. It compares media Titles to filenames (excluding extensions) and overwrites any mismatched descriptions with the filename, even if the original Title was intentionally different. This can result in unintended loss of user-provided information. The script prints warnings for missing Titles and mismatches, but automatically applies changes that could erase valuable metadata. Users should exercise extreme caution when running this script, as it may irreversibly alter media Titles in the database.”

1 Like

Something you could add is…

if the Title is non-blank and different from the filename
  then if the Title is different than the filename + extension
    copy the title to an Attribute of the Media object
Only after that, overwrite the blanks and mismatches

Then you can safely overwrite old title and still maintain an old reference

As a matter of safety, it is better to share scripts with commit_changes set to False. After users validate with the proxy, they can re-run with the commit_changes set to True

1 Like

It’s a good idea to make the script more secure. Thanks!

1 Like

@emyoulation I’ve made some updates above in the script:

  1. description is updated, not it’s more detailed and includes Attention-message.
  2. commit_changes is disabled by default.
  3. “Old title” attribute is adding before title is changed.

The script is successfully tested.

1 Like

Linked in the SuperTool Scripts list on the Isotammi wiki page

Media Title and Integrity Checker : Urchello’s Bulk Media Title update. (For use after renaming media files on storage device and re-linking with Media Verify addon tool.) This script updates the media object’s Title to match the new filenames

1 Like

How about limiting the Scope option to the Selected objects record? They can always expand the scope to Filtered objects or All objects with a click on a radio button.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.