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
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
Copy link to clipboard
Copied
You are making an assignment on line 7
Copy link to clipboard
Copied
thank you Pickory
how can fix it?
John
Copy link to clipboard
Copied
if (ourlink === 1)
Copy link to clipboard
Copied
thank you Benreyn
but not working
John
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");
}
}
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
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"]
);
Copy link to clipboard
Copied
Thank you Marc
thank so much.
my goal finaly like this:
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
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