Skip to main content
Stephen Marsh
Community Expert
Community Expert
November 5, 2023
Answered

Conditional Check for Camera Raw Doc, Saved Doc or Unsaved Doc

  • November 5, 2023
  • 2 replies
  • 169 views

The background for this post started here:

 

 
 
I have a script working for Saved/Unsaved docs, but when it comes to an open doc rendered from a raw file via Adobe Camera Raw, I am having problems with adding a third condition and then having all three test documents successfully parsed.
 
I have gone "back to basics" many times, but keep hitting problems.
 
So before I even start comparing Saved (backing path exists) vs Unsaved (no backing path) vs Raw opened via ACR, I just wanted to first determine what is a rendered raw file.
 
This could be done via metadata, but isn't bulletproof as standard files can contain CRS metadata. So basing this test of filename seems to be better, but not bullet proof. Combining a conditional check for both filename extension and CRS metadata would be best.
 
So, my first hurdle is the following, compare the following simplified example, the second alert works as expected:

 

alert(activeDocument.name);
if (activeDocument.name.match(/\.(RAF|CR2|CR3|NRW|ERF|RW2|NEF|ARW|RWZ|EIP|DNG|BAY|DCR|RAW|CRW|3FR|K25|KC2|MEF|DNG|CS1|ORF|ARI|SR2|MOS|CR3|GPR|SRW|MFW|FFF|SRF|KDC|MRW|J6I|RWL|X3F|PEF|IIQ|CXI|NKSC|MDC)$/i)) {
    alert("Camera Raw Base File...");
}

 

However, when I use .fullName instead of .name – the second alert fails.

 

alert(activeDocument.fullName);
if (activeDocument.fullName.match(/\.(RAF|CR2|CR3|NRW|ERF|RW2|NEF|ARW|RWZ|EIP|DNG|BAY|DCR|RAW|CRW|3FR|K25|KC2|MEF|DNG|CS1|ORF|ARI|SR2|MOS|CR3|GPR|SRW|MFW|FFF|SRF|KDC|MRW|J6I|RWL|X3F|PEF|IIQ|CXI|NKSC|MDC)$/i)) {
    alert("Camera Raw Base File...");
}

 

Why? What am I missing? The regular expression match should work either way!

 

After answering this basic question, my next challenge is the conditional check for Saved, Unsaved and Raw file...

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

I think that I have solved it, I should have had the try/catch wrapped around the conditionals, not inside them!

 

if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
var crsMeta = xmp.getProperty(XMPConst.NS_CAMERA_RAW, "Version");

try {
    app.activeDocument.path;
    if ((/\.(RAF|CR2|CR3|NRW|ERF|RW2|NEF|ARW|RWZ|EIP|DNG|BAY|DCR|RAW|CRW|3FR|K25|KC2|MEF|DNG|CS1|ORF|ARI|SR2|MOS|CR3|GPR|SRW|MFW|FFF|SRF|KDC|MRW|J6I|RWL|X3F|PEF|IIQ|CXI|NKSC|MDC)$/i).test(activeDocument.fullName) === true && crsMeta !== undefined) {
        alert("Camera Raw Base File:\r" + activeDocument.fullName);
        // Do stuff for raw renders...
    } else if (activeDocument.path) {
        alert("Previously Saved:\r" + activeDocument.path.fsName);
        // Do stuff for saved files...
    }
} catch (e) {
    alert("Unsaved!");
    // Do stuff for unsaved files...
}

 

2 replies

Stephen Marsh
Community Expert
Community Expert
November 5, 2023

OK, so now to move onto the conditional check.

 

I have tried putting the various conditions in different order, so what am I missing?

 

Hopefully it's clear that I am trying to test 3 separate conditions and then "do something" based on each condition:

 

if ((/\.(RAF|CR2|CR3|NRW|ERF|RW2|NEF|ARW|RWZ|EIP|DNG|BAY|DCR|RAW|CRW|3FR|K25|KC2|MEF|DNG|CS1|ORF|ARI|SR2|MOS|CR3|GPR|SRW|MFW|FFF|SRF|KDC|MRW|J6I|RWL|X3F|PEF|IIQ|CXI|NKSC|MDC)$/i).test(activeDocument.fullName) === true && activeDocument.path) {
    alert("Camera Raw Base File:\r" + activeDocument.fullName);
    // Do stuff for raw renders...
} else if (activeDocument.path) {
    alert("Previously Saved:\r" + activeDocument.path.fsName);
    // Do stuff for saved files...
} else {
    try {
        app.activeDocument.path;
    } catch (e) {
        alert("Unsaved!");
        // Do stuff for unsaved files...
    }
}

 

I think that the problem is with the try/catch. I have tried to create an object out of a try/catch to use in a conditional, but have not had any luck. I'm sure that this is a basic logic issue, however, I just can't work out where I am going wrong and have been spinning my wheels for hours.

 

Once I have the conditional working, I'll put it into a while loop to process and close each open document.

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertAuthorCorrect answer
Community Expert
November 10, 2023

I think that I have solved it, I should have had the try/catch wrapped around the conditionals, not inside them!

 

if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
var crsMeta = xmp.getProperty(XMPConst.NS_CAMERA_RAW, "Version");

try {
    app.activeDocument.path;
    if ((/\.(RAF|CR2|CR3|NRW|ERF|RW2|NEF|ARW|RWZ|EIP|DNG|BAY|DCR|RAW|CRW|3FR|K25|KC2|MEF|DNG|CS1|ORF|ARI|SR2|MOS|CR3|GPR|SRW|MFW|FFF|SRF|KDC|MRW|J6I|RWL|X3F|PEF|IIQ|CXI|NKSC|MDC)$/i).test(activeDocument.fullName) === true && crsMeta !== undefined) {
        alert("Camera Raw Base File:\r" + activeDocument.fullName);
        // Do stuff for raw renders...
    } else if (activeDocument.path) {
        alert("Previously Saved:\r" + activeDocument.path.fsName);
        // Do stuff for saved files...
    }
} catch (e) {
    alert("Unsaved!");
    // Do stuff for unsaved files...
}

 

Stephen Marsh
Community Expert
Community Expert
November 5, 2023

OK, I have a work-around using .test rather than .match

 

alert(activeDocument.fullName);
if ((/\.(RAF|CR2|CR3|NRW|ERF|RW2|NEF|ARW|RWZ|EIP|DNG|BAY|DCR|RAW|CRW|3FR|K25|KC2|MEF|DNG|CS1|ORF|ARI|SR2|MOS|CR3|GPR|SRW|MFW|FFF|SRF|KDC|MRW|J6I|RWL|X3F|PEF|IIQ|CXI|NKSC|MDC)$/i).test(activeDocument.fullName) === true) {
    alert("Camera Raw Base File...");
}

 

Curious minds would still like to know why, but I'm not going to lose any sleep over this!

 

So now I can move onto the contitional testing!