Grok automated user-defined tag creation

Started by ww408, March 12, 2025, 01:06:25 PM

Previous topic - Next topic

ww408

I put Grok to the test and I asked him to make me a script that would automatically add new tag properties to ExifTool. I showed him https://exiftool.org/config.html. He got it right on his first try. The script is below. It's specific to Dublin Core.

I thought to myself, why not make it even more automatic and just make exiftool automatically create and add tags that don't exist when running normal cmds, e.g. exiftool -xmp-dc:title=<text> -xmp-dc:whatever=<text> will result in the immediate creation of whatever and whatever will be added to the file along with the specified value. Grok said we could either mod exiftool, itself, or make a wrapper script.

Any opinions on which route I should go? How does Phil feel about adding this, or something similar, to exiftool. Due to the potential for errors that this introduces (e.g. when you make a typo), the output for a such a cmd could be XMP-dc:whatever didn't exist and was created. Press u to undo.

#!/bin/bash

# Check if a tag name was provided
if [ -z "$1" ]; then
    echo "Usage: $0 <tagname>"
    echo "Example: $0 ISBN"
    exit 1
fi

# Set variables
CONFIG_FILE="$HOME/.ExifTool_config"
TAG_NAME="$1"
NAMESPACE="Image::ExifTool::XMP::dc"

# Function to check if a string exists in a file
contains() {
    grep -q "$1" "$2"
}

# If the config file doesn't exist, create it with the tag
if [ ! -f "$CONFIG_FILE" ]; then
    cat <<EOF > "$CONFIG_FILE"
%Image::ExifTool::UserDefined = (
    '$NAMESPACE' => {
        $TAG_NAME => { Writable => 'string' },
    },
);

1; # End of config file
EOF
    echo "Created $CONFIG_FILE with $TAG_NAME in XMP-dc namespace."
else
    # Check if the XMP-dc section exists
    if contains "'$NAMESPACE'" "$CONFIG_FILE"; then
        # Check if the tag already exists
        if contains "$TAG_NAME =>" "$CONFIG_FILE"; then
            echo "Tag $TAG_NAME already exists in $CONFIG_FILE."
            exit 0
        else
            # Append the tag before the closing parenthesis of the XMP-dc section
            sed -i "/'$NAMESPACE' => {/ a\        $TAG_NAME => { Writable => 'string' }," "$CONFIG_FILE"
            echo "Added $TAG_NAME to existing XMP-dc section in $CONFIG_FILE."
        fi
    else
        # Append the entire XMP-dc section before the final "1;"
        sed -i "/1; # End of config file/i\
    '$NAMESPACE' => {\n\
        $TAG_NAME => { Writable => 'string' },\n\
    }," "$CONFIG_FILE"
        echo "Added XMP-dc section with $TAG_NAME to $CONFIG_FILE."
    fi
fi

# Make sure the file is valid Perl (ends with "1;")
if ! tail -n 1 "$CONFIG_FILE" | grep -q "^1;"; then
    echo "1; # End of config file" >> "$CONFIG_FILE"
fi

echo "Done! You can now use: exiftool -XMP-dc:$TAG_NAME=\"value\" file.jpg"

Usage: <script name> <desired tag name>

It creates/modifes an exiftool config file in the /home/<username> directory.

Also, for those who are interested, I just had Grok make me a very useful extension for itself:

Chrome extension that lets you add keywords and annotations to your Grok convos