Skip to main content
Participating Frequently
July 30, 2024
Answered

Get metadata using extendscript from raw file(.cr2,.cr3 etc) without opening the raw file

  • July 30, 2024
  • 5 replies
  • 1236 views

This works no problem with a jpeg however the same method doesn't work with a camera raw file.

https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-print-dpi-resolution/m-p/10502704#M250043

I have also tried this but it only shows the first part of the metadata. I really don't know what to do.  Essentially I just want to check if there is a rating greater than 0 in the metatdata of files in a folder without opening/closing each RAW file individually as it takes a lot of time. Any help would be much appreciated.

if (ExternalObject.AdobeXMPScript === undefined) {
    ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
}

// Function to load XMP metadata from raw file
function loadXMPMetadata(rawFilePath) {
    var xmpFile = new File(rawFilePath);
    if (!xmpFile.exists) {
        alert("Raw file does not exist: " + rawFilePath);
        return;
    }

    // Read the XMP data from the file
    xmpFile.open('r');
    var xmpData = xmpFile.read();
    xmpFile.close();
    alert(xmpData)
    // Create an XMPMeta object from the data
    var xmp = new XMPMeta(xmpData);
    var serializedXMP = xmp.serialize()
    alert(serializedXMP)
}

 

This topic has been closed for replies.
Correct answer Stephen Marsh

This does it for a folder of CR3 images:

 

/*
Get XMP Rating Metadata for CR3 Files in Folder.jsx
v1.0 - 31st July 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-metadata-using-extendscript-from-raw-file-cr2-cr3-etc-without-opening-the-raw-file/td-p/14770220
*/

#target photoshop

if (ExternalObject.AdobeXMPScript === undefined) {
    ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
}

try {
    var theFolder = Folder.selectDialog("Select a folder...");
    if (theFolder) {
        var files = theFolder.getFiles(function (file) {
            return file instanceof File && /\.cr3$/i.test(file.name);
        });

        if (files && files.length > 0) {
            var theFiles = [];
            for (var i = 0; i < files.length; i++) {
                var theRating = getXMPRating(files[i]);
                theFiles.push(files[i].name + " - Rating: " + theRating);
            }
            alert("Files containing rating metadata:\n" + theFiles.join("\n"));
        } else {
            alert("No supported files found in the selected folder!");
        }
    } else {
        alert("No folder selected!");
    }
} catch (e) {
    alert("An error occurred: " + e.message);
}


function getXMPRating(file) {
    try {
        var xmpFile = new XMPFile(file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
        var xmp = xmpFile.getXMP();
        if (xmp.doesPropertyExist(XMPConst.NS_XMP, "Rating")) {
            return xmp.getProperty(XMPConst.NS_XMP, "Rating");
        }
    } catch (e) {
        // If there's an error, return 0 (unrated)
        return 0;
    }
    // Default to 0 if no rating found
    return 0;
}

 

5 replies

Legend
July 31, 2024

Why not use Bridge for this? That way you can read both the RAW file and the XMP sidecar together.

JibbletonAuthor
Participating Frequently
July 31, 2024

Building a specific script that runs only in photoshop. I haven't used bridge in years to be honest. Maybe I could have done something with it but it's done now. 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 31, 2024

This does it for a folder of CR3 images:

 

/*
Get XMP Rating Metadata for CR3 Files in Folder.jsx
v1.0 - 31st July 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/get-metadata-using-extendscript-from-raw-file-cr2-cr3-etc-without-opening-the-raw-file/td-p/14770220
*/

#target photoshop

if (ExternalObject.AdobeXMPScript === undefined) {
    ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
}

try {
    var theFolder = Folder.selectDialog("Select a folder...");
    if (theFolder) {
        var files = theFolder.getFiles(function (file) {
            return file instanceof File && /\.cr3$/i.test(file.name);
        });

        if (files && files.length > 0) {
            var theFiles = [];
            for (var i = 0; i < files.length; i++) {
                var theRating = getXMPRating(files[i]);
                theFiles.push(files[i].name + " - Rating: " + theRating);
            }
            alert("Files containing rating metadata:\n" + theFiles.join("\n"));
        } else {
            alert("No supported files found in the selected folder!");
        }
    } else {
        alert("No folder selected!");
    }
} catch (e) {
    alert("An error occurred: " + e.message);
}


function getXMPRating(file) {
    try {
        var xmpFile = new XMPFile(file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
        var xmp = xmpFile.getXMP();
        if (xmp.doesPropertyExist(XMPConst.NS_XMP, "Rating")) {
            return xmp.getProperty(XMPConst.NS_XMP, "Rating");
        }
    } catch (e) {
        // If there's an error, return 0 (unrated)
        return 0;
    }
    // Default to 0 if no rating found
    return 0;
}

 

JibbletonAuthor
Participating Frequently
July 31, 2024
Stephen Marsh
Community Expert
Community Expert
July 31, 2024

You're welcome!

Stephen Marsh
Community Expert
Community Expert
July 31, 2024

@Jibbleton 

 

This code does it for a single image, without opening the image:

 

/*
Based on:
https://community.adobe.com/t5/photoshop-ecosystem-discussions/how-to-read-xmp-metadata-in-ps-using-javascript/m-p/12966451
*/

#target photoshop

var theFile = File.openDialog();
var ns = "http://ns.adobe.com/xap/1.0/";
ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
xmpMeta = new XMPFile(theFile.fsName, XMPConst.FILE_XMP, XMPConst.OPEN_FOR_READ).getXMP();
var theValue = Math.abs(xmpMeta.getProperty(ns, "Rating"));
alert("Rating: " + theValue + " stars");

 

You would just need to adapt this to loop over the files in the folder, rather than selecting a single image.

Stephen Marsh
Community Expert
Community Expert
July 31, 2024

For a CR2 file with no sidecar, Exiftool reports the following:

 

[IFD0]          XResolution                     : 72
[IFD0]          YResolution                     : 72
[IFD0]          ResolutionUnit                  : inches

 

For an ORF file, there is main image info (IFD0) and thumbnail info (IFD1):

 

[IFD0]          XResolution                     : 314
[IFD0]          YResolution                     : 314
[IFD0]          ResolutionUnit                  : inches

[IFD1]          XResolution                     : 72
[IFD1]          YResolution                     : 72
[IFD1]          ResolutionUnit                  : inches

 

For an ARW file, again, there is main image info (IFD0) and thumbnail info (IFD1):

 

[IFD0]          XResolution                     : 350
[IFD0]          YResolution                     : 350
[IFD0]          ResolutionUnit                  : inches

[SubIFD]        XResolution                     : 350
[SubIFD]        YResolution                     : 350
[SubIFD]        ResolutionUnit                  : inches

[IFD1]          XResolution                     : 72
[IFD1]          YResolution                     : 72
[IFD1]          ResolutionUnit                  : inches

 

More here:

 

https://exiftool.org/#groups

 

https://exiftool.org/#tagnames

 

https://exiftool.org/TagNames/

 

https://exiftool.org/TagNames/EXIF.html

 

Stephen Marsh
Community Expert
Community Expert
July 30, 2024

What do you see in File > File Info > Raw Data?

 

Something like this?

 

<tiff:XResolution>300/1</tiff:XResolution>
<tiff:YResolution>300/1</tiff:YResolution>
<tiff:ResolutionUnit>2</tiff:ResolutionUnit>

 

Keep in mind that this isn't coming from the raw file, it is coming from the ACR workflow options.

 

Are there XMP sidecar files for the raw files? These would be faster to process. Edit: It looks like there isn't any useful info in the XMP sidecar file.

 

One could argue that for a raw file, this is meaningless. It may be at a default value such as 72 ppi, or some other value set by the camera or hard-coded by the manufacturer. Different raw file formats may or may not contain this metadata or may contain conflicting metadata.

 

I would probably use ExifTool for this task.

JibbletonAuthor
Participating Frequently
July 31, 2024

Thank you so much for responding. I ended up falling asleep here in the EU. I don't think I was clear in my question and I apologise. I wanted to get the metadata of raw file so that I can get the star rating. The side car files only appear after I open the file and do something with it manually. I really need to use photoshop as this code I'm trying to make is part of a larger script with photoshop. It's the only part I haven't got working.

Stephen Marsh
Community Expert
Community Expert
July 31, 2024

Ah, the link that you posted was about resolution, I wasn't sure why you used the term "a rating greater than zero".

 

This wouldn't exist in the raw file as Adobe treat that as read-only, it would be in the XMP sidecar file.

 

Are you saying that your camera or some other software that you use upstream has a rating feature that directly rates a raw image outside of Photoshop, Bridge etc – without using a sidecar? If so, can you provide a sample of such as raw file?