Copy link to clipboard
Copied
As a newb to scripting, I am struggling with conditional error handling.
The script that I would like to error check is only intended to be run on a document that has multiple layers (not background layer + layers if that makes sense). It should only run if the document index is the second doc open ( app.documents[1] ).
The script closes the temporary working file down without saving after it is duplicated to the target document ( app.documents[0] ), so I don’t wish to accidentally run the script on a file that does not meet the following conditions as work may be lost.
I’d like to cancel the script with an alert if any of the following criteria are met:
You mean you didn't know how to do this:
$.level = 0; if ((doc = documents).length > 1 && (aD = activeDocument) == doc[1] && aD.artLayers.length > 1 && (function(){end = true; try{end = !aD.backgroundLayer}catch(err){} return end})()) {
alert('Your code!')
}
else alert('The End!')
Copy link to clipboard
Copied
$.level = 0; if ((doc = documents).length > 1 && (aD = activeDocument) == doc[1] && aD.layers.length > 1 && (function(){end = true; try{end = !aD.backgroundLayer}catch(err){} return end})()) alert('Your code!') else alert('The End!')
Copy link to clipboard
Copied
Thank you Kukurykus I know just enough to be dangerous, which sadly is not enough to make your code work when I try to paste in my existing code. I was hoping that it would just be enough to replace alert('Your code!') – however it was not that simple… I’m sure that I am making a rookie mistake.
Copy link to clipboard
Copied
You mean you didn't know how to do this:
$.level = 0; if ((doc = documents).length > 1 && (aD = activeDocument) == doc[1] && aD.artLayers.length > 1 && (function(){end = true; try{end = !aD.backgroundLayer}catch(err){} return end})()) {
alert('Your code!')
}
else alert('The End!')
Copy link to clipboard
Copied
No, I didn’t – but now that you have handed me a nice easy line 2 alert('Your code!') I can easily plug in the existing code and now have a usable result. Thank you!
Copy link to clipboard
Copied
Thank you Kukurykus, it was hard to choose between your code and r-bin’s as they both work for the task at hand. I thank you both for helping out a scripting newb!
Copy link to clipboard
Copied
var Doc = activeDocument; // or set as needed
var ok = true;
var background_present = true;
try { Doc.backgroundLayer; } catch (e) { background_present = false; }
//Doc only has Background layer
if (background_present && Doc.artLayers.length == 1 && Doc.layerSets.length == 0) ok = false;
//Doc has Background layer + 1 or more layers
if ((background_present && Doc.artLayers.length > 1) || Doc.layerSets.length > 0) ok = false;
//Doc has only 1 layer
if (!background_present && Doc.artLayers.length == 1 && Doc.layerSets.length == 0) ok = false;
//Only one document open
if (app.documents.length < 2) ok = false;
//or current document is not the second document opened ( app.documents[1] )
if (app.documents.length > 1 && activeDocument != app.documents[1]) ok = false;
if (ok)
{
alert("OK");
}
else
{
alert("STOP");
}
Copy link to clipboard
Copied
Loll, didn't my code work?
Copy link to clipboard
Copied
One layer and one group.
Your code gives OK, my gives Fail.
Copy link to clipboard
Copied
Stephen_A_Marsh didn't specified that, but it's enough to change layers to artLayers in my code or you find something else:
$.level = 0; if ((doc = documents).length > 1 && (aD = activeDocument) == doc[1] && aD.artLayers.length > 1 && (function(){end = true; try{end = !aD.backgroundLayer}catch(err){} return end})()) alert('Your code!') else alert('The End!')
Copy link to clipboard
Copied
So now I have to mark one answer as correct, when both are excellent answers! And I thought that it was tough working out the script syntax...
Copy link to clipboard
Copied
You do not consider the presence of LayerSets in your original conditions. In my example, this is taken into account. They should not be.
Copy link to clipboard
Copied
Agreed, although that is debatable… I’ll keep this in mind when I am testing and assigning the correct answer.
I did not include it because Layer Sets/Groups should not exist in this workflow, however that does not mean that somebody may create them. If the layer set does not interfere with the stack and mean/average blending then it would be OK to pass it through.
The workflow goes like this:
1) A base image document is the first image open
2) A third party script called from an extension panel automatically creates a new document with different image variations stacked as layers, this should be the second document open.
3) The script that I am hacking together from DOM and AM code will then merge all of the stacked layers into a smart object, apply mean blend to the image stack to average the layer variations, duplicate the smart object layer to the base image document and finally close the temporary smart object document without saving.
This is why I required specific error handling conditions to try to ensure that only the temporary image created from the extension panel is closed without saving.
However in hindsight, perhaps I could have just used or added part of the filename for my error checking condition, as the filename of the temporary unsaved document created from the panel contains both a static and variable naming component.
Copy link to clipboard
Copied
Surely everything can be done quite simply. And no matter the number of open documents and their order. But for this we need to know the whole process that your scripts execute.
Copy link to clipboard
Copied
Thanks r-bin!
Well, as a beginner my script is of course a mess and mass of code hacked together, some DOM and some AM. It works and does what I am looking for, thanks to you r-bin and others such as Kukurykus. I can post it if you wish, however it will likely make you cringe.
The “base image” could be any image, there is nothing special or unique about the image in filename, metadata or anything else that I can think of. In my workflow this is the first image opened, however I guess it does not have to be. I would likely be working one image at a time.
The “layered stack of images” is an unsaved document that is created on the fly using a third party extension panel. This file is a temporary means to an end in my use, however it could be saved for later reference if required. This does have a filename pattern that is predictable, such as baseimagefilename_VariantStack with the blue text being the variable filename of the original image and the red text being a static string added by the 3rd party extension panel. Again in my workflow this is the second image open, however I guess it does not have to be. I would probably be working one image at a time.
More information on the 3rd party panel below, I am not sure if it is possible to inspect or modify the underlying panel code:
http://www.moderncolorworkflow.com/free-resources
http://www.moderncolorworkflow.com/wp-content/uploads/stuff/free/PPW_5_CC_052718.zip
Copy link to clipboard
Copied
Too much text... ))
Do you want to say that you want to dock the work of some extension with your homemade script?
I do not use extensions and can not advise you anything in this case.
Copy link to clipboard
Copied
Surely everything can be done quite simply. And no matter the number of open documents and their order. But for this we need to know the whole process that your scripts execute.
Thank you for the generous offer!
You helped me in another topic r-bin, which is where the code in question originated... I’ll make a new post there:
Copy link to clipboard
Copied
Excellent, thank you r-bin – I could easily and successfully change your code alert("OK"); on line 28 and replace it with my working code!