• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Error handling - Background or single layer

Community Expert ,
Jan 21, 2019 Jan 21, 2019

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:

  • Doc only has Background layer
  • Doc has Background layer + 1 or more layers
  • Doc has only 1 layer
  • Only one document open or current document is not the second document opened ( app.documents[1] )
TOPICS
Actions and scripting

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jan 22, 2019 Jan 22, 2019

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!')

Votes

Translate

Translate
Adobe
LEGEND ,
Jan 21, 2019 Jan 21, 2019

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!')

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2019 Jan 22, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2019 Jan 22, 2019

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!')

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2019 Jan 22, 2019

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2019 Jan 24, 2019

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2019 Jan 22, 2019

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");

    }

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2019 Jan 22, 2019

Copy link to clipboard

Copied

Loll, didn't my code work?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2019 Jan 22, 2019

Copy link to clipboard

Copied

One layer and one group.
Your code gives OK, my gives Fail.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 22, 2019 Jan 22, 2019

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!')

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2019 Jan 22, 2019

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...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2019 Jan 22, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2019 Jan 22, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2019 Jan 22, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2019 Jan 22, 2019

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

https://cc-extensions.com/products/ppw/

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
Jan 22, 2019 Jan 22, 2019

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 24, 2019 Jan 24, 2019

Copy link to clipboard

Copied

LATEST
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:

Move unsaved SO to another doc retaining SO

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 22, 2019 Jan 22, 2019

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines