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

how to define if if

Contributor ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

Hi experts,

my script like this:

    var

          myDoc = app.activeDocument;

          mylink = myDoc.links.everyItem().getElements();

          for (var i=0; i<mylink.length; i++) {

              var ourlink = mylink;

                if (ourlink.name.match(/\d{4}/)) {

                      if (ourlink =1) {

                        alert("only one");

                            }

                      else {

                        alert("more than one");

                        }

                    }

                }

my goal:

if myDoc link.name.match(/\d{4}/)

and

if the link match(/\d{4}/) = 1

alert ("only one")

else

alert ("more than one")

this script works incorrectly.

could someone tell me how to define, please.

thanks

regard

John

TOPICS
Scripting

Views

595

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

Guide , Jan 19, 2019 Jan 19, 2019

Hi John,

Your goal remains unclear. It seems you want to detect whether some condition on link names —based on the regex /\d{4}/—is satisfied once, or more than once.

Then three outcomes are possible (0, 1, or >1) as shown below.

// Your regex means: "Does the string contain

// at least four successive digits?"

// ---

const RE = /\d{4}/;

var doc = app.properties.activeDocument,

    a = doc && doc.links.everyItem().name,

    i = a && a.length,

    x = 0;

while( i-- )

{

    if( RE.test(a) && x++ ) break;

}

aler

...

Votes

Translate

Translate
Guide ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

You are making an assignment on line 7

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
Contributor ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

thank you Pickory

how can fix it?

John

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
Participant ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

if (ourlink === 1)

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
Contributor ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

thank you Benreyn

but not working

John

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
Participant ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

What about trying adding any searched links that hit the match into an array and then check the array for the length to give you an alert? Might not be the best solution, I'm not quite sure I understand exactly what you are trying to accomplish. Se below:

EDITS MADE

  var

          myDoc = app.activeDocument;

          mylink = myDoc.links.everyItem().getElements();

          ourSearchedLinks = [];

          for (var i=0; i<mylink.length; i++) {

              var ourlink = mylink;

                if (ourlink.name.match(/\d{4}/)) {

                     ourSearchedLinks.push(myLink);

                }

           }

          if(ourSearchedLinks.length == 1) {

                  alert("only one");

               }

               else {

                   alert("more than one");

              }

         }

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
Contributor ,
Jan 18, 2019 Jan 18, 2019

Copy link to clipboard

Copied

thank you Benreny,

but it will alert twice which there are two match

"only one"

and

"more than one"

that is not my goal.

John

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
Guide ,
Jan 19, 2019 Jan 19, 2019

Copy link to clipboard

Copied

Hi John,

Your goal remains unclear. It seems you want to detect whether some condition on link names —based on the regex /\d{4}/—is satisfied once, or more than once.

Then three outcomes are possible (0, 1, or >1) as shown below.

// Your regex means: "Does the string contain

// at least four successive digits?"

// ---

const RE = /\d{4}/;

var doc = app.properties.activeDocument,

    a = doc && doc.links.everyItem().name,

    i = a && a.length,

    x = 0;

while( i-- )

{

    if( RE.test(a) && x++ ) break;

}

alert( ["None","Only One","More than One"] );

This code simply tells whether zero, one, or more strings (among the array of link names) match the condition.
But it's hard to guess what you're actually looking after…
@+
Marc

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
Contributor ,
Jan 19, 2019 Jan 19, 2019

Copy link to clipboard

Copied

LATEST

Thank you Marc

thank so much.

my goal finaly like this:

auto pdf name unfunction

I just want to get the link.name for pdf name, and if the link.name.match.\d{4} != 1 the pdf named by manual.

else named by auto.

but for now has two problems as my code which in the

auto pdf name unfunction

1. if the indesign file has two links even the links no match \d{4}, the script will pop up the ui

2. for auto pdf named, the script only can word on active document, not all the open docs.

thank you

regard

John

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