Photoshop XMP to Camera Raw?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

AndyConnor

Photoshop XMP to Camera Raw?

Post by AndyConnor »

I am hoping that I can get some help on two questions related to a script I am trying to write. It's a small tweak on this (dated) code found on the web (http://www.lodgephoto.com/blog/photosho ... script/17/ ... script/17/).

I want to be able to apply the same Camera Raw adjustments to all files in a directory. I know that I can batch process them by opening multiple files from Adobe Bridge, but that's not feasible on my computer when I am talking about processing 200+ raw files from my D800 that are 50MB each. This processing is part of a larger script that I am writing that I just want to "set and forget" overnight.

Here is my code, very minor changes from the site linked above:

Code: Select all// Save current dialog preferences
var startDisplayDialogs = app.displayDialogs;

// Don't display dialogs
app.displayDialogs = DialogModes.NO;

// Constants
var FILE_TYPE = ".nef"; // The type of files that this script works on -- you can change
var SEARCH_MASK = "*" + FILE_TYPE; // Image file filter to find only those files
var XMP_TYPE = ".xmp"; // File extension for XMP output files
var MAC_XMP_TYPE = "XMPT"; // Macintosh file type for .xmp files
var MAC_XMP_CREATOR = "8BIM"; // Macintosh file creator for .xmp files

// Set of exception strings for error handling
var X_NOINPUT = "noInput";
var X_BADDOC = "badDoc";
var X_WERROR = "writeError";
var X_CERROR = "closeError";

try {
    // Ask user for input folder
    var inputFolder = Folder.selectDialog("Select a folder to process");
    if (inputFolder == null){
        throw X_NOINPUT;
    }
   
    // get all files in the input folder
    var fileList = inputFolder.getFiles(SEARCH_MASK);

    // Open each file in turn
    for (var i = 0; i < fileList.length; i++) {
        // Only want to open non-hidden files (and no folders)
        if ((fileList instanceof File) && (fileList.hidden == false)) {
   
        // Open the file in Photoshop
        var docRef = open(fileList);
        if (docRef == null){
            throw X_BADDOC;
        }

        if (docRef.xmpMetadata != null) {
            // Document has XMP metadata, so process it
            docRef.xmpMetadata.rawData.replace("<crs:Exposure2012>0.00</crs:Exposure2012>","<crs:Exposure2012>-1.00</crs:Exposure2012>");

            var inFileName = fileList.name; // Filename without path

            // Replace the file's current extension (e.g. .jpg) with .xmp
            var outFileName = inFileName.toLowerCase(); // Output file is lower case
            outFileName = outFileName.replace(FILE_TYPE, XMP_TYPE); // Make .xmp
            var outputXMP = new File(inputFolder + "/" + outFileName);

            // Open file for writing. Mac type and creator are ignored on other systems
            outputXMP.encoding = "UTF-8";
            var success = outputXMP.open("w", MAC_XMP_TYPE, MAC_XMP_CREATOR);
            if (!success){
                throw X_WERROR;
            }
           
            // Write out the meta data
            success = outputXMP.writeln(docRef.xmpMetadata.rawData);

            if (!success){
                throw X_WERROR;
            }

            // Close the file
            success = outputXMP.close();

            if (!success){
                throw X_CERROR;
            }
        }

        // Close the Photoshop file
        docRef.close(SaveOptions.DONOTSAVECHANGES);
        }
    }
}

catch (exception) {
    // Show degbug message and then quit
    alert(exception);
}

finally {
    // Reset app preferences
    app.displayDialogs = startDisplayDialogs;
}



I have two questions.

1. Basically the code works, it creates an XMP file for each of my .NEF files. This contains all of the data I would expect - the problem is that when I open the .NEF file in Camera Raw the contents aren't recognised. I've visually checked the .XMP that this code produces against a .XMP file that Camera Raw produces and they are completely different. What am I missing? Why is there such a difference in the content of the file.

2. I started trying this at work today, and using CS5.5 the line of code

Code: Select alldocRef.xmpMetadata.rawData.replace("<string1>","<string2>");

worked perfectly and my .XMP file included the correct values. At home tonight using CS6, I noticed that the strings I was search for were different so changed them to suit. Now the files being written don't include the changed values and I am pretty confused.


Any thoughts or suggestions would be gratefully received.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Paul MR

Photoshop XMP to Camera Raw?

Post by Paul MR »

Wow that must be very slow!

If you have Photoshop CS4 or better, it would be best doing something like this and it does not open any raw files ...

Code: Select all#target Photoshop
app.bringToFront();
main();
function main(){
var sourceFolder = Folder.selectDialog('Please select the source folder');
if(sourceFolder == null) return;
var fileList = sourceFolder.getFiles("*.nef","*.cr2");
for(var a in fileList){
    setMetadata(fileList[a]);
    }
alert("All done");
}

function setMetadata(Rawfile){
var Name = File(Rawfile).name.replace(/\.[^\.]+$/, '');
var file = File(Rawfile.path + "/" + Name + ".xmp");
try{
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
  if(file.exists){
     file.open('r');
     file.encoding = "UTF8";
     file.lineFeed = "unix";
     file.open("r", "TEXT", "????");
     var xmpStr = file.read();
     file.close();
     }else{ var xmpStr='';}
     var xmp = new XMPMeta( xmpStr );
     //set values as required
    xmp.deleteProperty(XMPConst.NS_CAMERA_RAW, "Exposure");
    xmp.setProperty( XMPConst.NS_CAMERA_RAW, "Exposure", -1 );
    /*************** etc
    xmp.deleteProperty(XMPConst.NS_CAMERA_RAW, "Brightness");
   xmp.setProperty( XMPConst.NS_CAMERA_RAW, "Brightness", 30 );
    xmp.deleteProperty(XMPConst.NS_CAMERA_RAW, "Saturation");
    xmp.setProperty( XMPConst.NS_CAMERA_RAW, "Saturation", 8 );
    xmp.deleteProperty(XMPConst.NS_CAMERA_RAW, "Shadows");
    xmp.setProperty( XMPConst.NS_CAMERA_RAW, "Shadows", 1 );
    xmp.deleteProperty(XMPConst.NS_CAMERA_RAW, "Sharpness");
    xmp.setProperty( XMPConst.NS_CAMERA_RAW, "Sharpness", 25 );
    xmp.deleteProperty(XMPConst.NS_CAMERA_RAW, "Clarity");
    xmp.setProperty( XMPConst.NS_CAMERA_RAW, "Clarity", 15 );
    xmp.deleteProperty(XMPConst.NS_CAMERA_RAW, "HighlightRecovery");
    xmp.setProperty( XMPConst.NS_CAMERA_RAW, "HighlightRecovery", 11 );
    */
     file.open('w');
     file.encoding = "UTF8";
     file.lineFeed = "unix";
     file.write( xmp.serialize() );
     file.close();
     }catch(e){alert(e+"-"+e.line);}
}
AndyConnor

Photoshop XMP to Camera Raw?

Post by AndyConnor »

Thanks, Paul - I will try that. Appreciate you taking the time to respond.

Yes, the first attempt is pretty slow - but the whole script is going to be pretty intensive. Opening the raw files one-by-one is the least of my worries right now!
AndyConnor

Photoshop XMP to Camera Raw?

Post by AndyConnor »

Works a treat, you are a legend - thanks heaps!