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

Stop a script if no active layer is found

New Here ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

I am trying to stop a script if no layer is selected.
I am finding it wierd why this command returns the first layer even if nothing is selected

alert (app.activeDocument.activeLayer.name);

I tried doing try catch instead of throwing an error when no layer is selected. But that also didn't work

 

Folks, I am new here. Please guide to help me understand better

TOPICS
Actions and scripting , Windows

Views

57

Likes

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 ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('targetLayers'));
r.putEnumerated(s2t("document"), s2t("ordinal"), s2t("targetEnum"));
if (!executeActionGet(r).getList(p).count) alert('no layers selected')

Likes

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 ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

@defaultseiiesa394z4 – It's a well-known bug when using DOM code, as the excellent script from @jazz-y shows, it takes the more esoteric AM code to overcome the issue.

Likes

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 ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

So since you asked about stopping the script. You can use jazz-y's script. Create a variable called run and set it to true. Create an if statement for all your routine code and have the condition be the run variable.  Then in jazz-y's script, change the run variable to false, if no layer is select. Tje script will then bypass all your code in the if statement and end.

Likes

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 ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

@Chuck Uebele  - A good point!

 

One could also use .confirm() if user action is required.

Likes

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
Engaged ,
Oct 20, 2022 Oct 20, 2022

Copy link to clipboard

Copied

Likes

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 ,
Oct 21, 2022 Oct 21, 2022

Copy link to clipboard

Copied

LATEST

@defaultseiiesa394z4 - So how did you go with the code and suggestions?

Likes

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