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

Check if the color sampler is present

Advocate ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

I tried this solution but it does not work

Where am I doing wrong

var doc = activeDocument; 

try{ 

   if (activeDocument.colorSamplers.length > 0)

   {alert ("color sampler/s exist/s")};

   

     } 

catch(){ 

       {alert ("color sampler/no exist")};

    

     }

TOPICS
Actions and scripting

Views

875

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

Community Expert , Dec 03, 2017 Dec 03, 2017

Why do you use the try-clause?

As long as a file is open you should be able to use the »else« of the »if«.

if (activeDocument.colorSamplers.length > 0) {alert ("color sampler/s exist/s")}

else {alert ("color sampler/no exist")};

Votes

Translate

Translate
Adobe
LEGEND ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

Simple activeDocument.colorSamplers.length is sufficent.

But if you want try/catch worked you have to add missing value inside rounded brackets, for ex. catch(err)

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
Advocate ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

tried it too but it does not work?

var doc = activeDocument;

try{

   if (activeDocument.colorSamplers.length > 0)

   {alert ("color sampler/s exist/s")};

  

     }

catch(e){

       {alert ("color sampler/no exist")};

   

     }

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 ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

Why do you use the try-clause?

As long as a file is open you should be able to use the »else« of the »if«.

if (activeDocument.colorSamplers.length > 0) {alert ("color sampler/s exist/s")}

else {alert ("color sampler/no exist")};

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
Advocate ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

I thought the best way was to try

now I learned another thing

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 ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

To use a try clause in a case like this it should include something that is possible only if the desired »thing« exists.

In this case you could define a variable from the first ColorSampler which would throw an error if no ColorSamplers exist.

#target photoshop

var doc = activeDocument;

try {

var theColorSampler = doc.colorSamplers[0];

alert ("color sampler/s exist/s")

} catch (e) {

alert ("color sampler/no exist")

};

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 ,
Dec 03, 2017 Dec 03, 2017

Copy link to clipboard

Copied

LATEST

Alternative way )

alert( has_property("document", "colorSamplerList") )

function has_property(class_name, prpr_name)

    {

    try

        {

        var r = new ActionReference();

        r.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID(prpr_name));

        r.putEnumerated( stringIDToTypeID( class_name ), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));

        if (executeActionGet(r).hasKey(stringIDToTypeID(prpr_name))) return true;

        return false;

        }

    catch (e) { alert(e); }

    }

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