Copy link to clipboard
Copied
Hello JS guru,
My task is to rename 3 types of pdf files which are in the same folder. Each of them has a unique word: 117.00, FREIGHT and CREDITED respectively. If I have one file from each type my script works, but if I have more than one files from each type it does not work.
Example: if have a pdf file with a unique word "117.00" than all is good and my script will SaveAS this file and create as a aresult a pdf file with name: "1_URA_117.pdf", but if I have 2 or more files with a unique word "117.00", than my script will just ignore these extra pdf files, but I need "2_URA_117.pdf", "3_URA_117.pdf" and so on.
Where is the mistake here?
for (var p = 0; p < this.numPages; p++)
{
var numWords = this.getPageNumWords(p);
for (var i=0; i<numWords; i++) {
var ckWord = this.getPageNthWord(p, i, true);
if ( ckWord == "117.00") {
for (var m = 1;p < this.numPages; m++)
{
this.saveAs(this.path.replace(this.documentFileName, m + "_URA_117.pdf"));}
}
else if (ckWord =="FREIGHT") {
for (var m = 1;m < this.numPages; m++){
this.saveAs(this.path.replace(this.documentFileName, m + "_URA_FREIGHT.pdf"));}
}
else if (ckWord =="CREDITED") {
for (var m = 1;m < this.numPages; m++){
this.saveAs(this.path.replace(this.documentFileName, m + "_URA_CREDITED.pdf"));} }
else {
}
}
}
Hi,
Here is a script which will generate a renamed copy of all your files.
if (isNaN(global.count1)) {
global.count1=1;
global.count2=1;
global.count3=1;
}
var found=0;
for (var p=0; p<this.numPages; p++) {
var numWords=this.getPageNumWords(p);
for (var i=0; i<numWords; i++) {
var ckWord=this.getPageNthWord(p, i, true);
if (ckWord=="117.00") {
this.saveAs(this.path.replace(this.documentFileName, global.count1+"_URA_117.pdf"));
global.count1+=1;
found++;
break;
} else i
...
Copy link to clipboard
Copied
Hi,
I think your loops are not correct, you are starting with a loop
for (var p = 0; p < this.numPages; p++)
which is fine, as this will loop for each page in the document
but then you have
for (var m = 1;p < this.numPages; m++)
this will loop forever, as m and p are not the same, so increasing m will never increase p so p will never be more that this.numPages.
but I am also curious as to why you are looping pages there, you are already in a loop of all pages, so why do you need to loop all pages, get a word and loop all pages again?
Copy link to clipboard
Copied
Thanks for a quick respond.
I am new to JS, so I can do a lot of mistakes)
So, your recommendation is to delete this row?
for (var m = 1;p < this.numPages; m++)
Copy link to clipboard
Copied
You must use counters for the different types of files.
Copy link to clipboard
Copied
What kind of function is that?
Copy link to clipboard
Copied
E.g.:
count1++;
Copy link to clipboard
Copied
Bernd,
I try to implement your advice, but it does not work: not sure what should I change? 🙂
Sorry for asking, but I cannot understand.
for (var p = 0; p < this.numPages; p++)
{
var numWords = this.getPageNumWords(p);
for (var i=0; i<numWords; i++) {
var ckWord = this.getPageNthWord(p, i, true);
if ( ckWord == "117.00") {
for (var count1 = 1;count1 < this.numPages; count1++)
{
this.saveAs(this.path.replace(this.documentFileName, count1 + "_URA_117.pdf"));}
}
else if (ckWord =="FREIGHT") {
for (var count2 = 1;count2 < this.numPages; count2++)
{
this.saveAs(this.path.replace(this.documentFileName, count2 + "_URA_FREIGHT.pdf"));}
}
else if (ckWord =="CREDITED") {
for (var count3 = 1;count3 < this.numPages; count3++)
{
this.saveAs(this.path.replace(this.documentFileName, count3 + "_URA_CREDITED.pdf"));} }
else {
}
}
}
Vielen Dank.
Copy link to clipboard
Copied
Hi,
Here is a script which will generate a renamed copy of all your files.
if (isNaN(global.count1)) {
global.count1=1;
global.count2=1;
global.count3=1;
}
var found=0;
for (var p=0; p<this.numPages; p++) {
var numWords=this.getPageNumWords(p);
for (var i=0; i<numWords; i++) {
var ckWord=this.getPageNthWord(p, i, true);
if (ckWord=="117.00") {
this.saveAs(this.path.replace(this.documentFileName, global.count1+"_URA_117.pdf"));
global.count1+=1;
found++;
break;
} else if (ckWord=="FREIGHT") {
this.saveAs(this.path.replace(this.documentFileName, global.count2+"_URA_FREIGHT.pdf"));
global.count2+=1;
found++
break;
} else if (ckWord=="CREDITED") {
this.saveAs(this.path.replace(this.documentFileName, global.count3+"_URA_CREDITED.pdf"));
global.count3+=1;
found++;
break;
}
}
if (found) break;
}
As these are global variables, you will have to quit Acrobat to reset them, else the values will be incremented.
But you can reset them from the console with one of these scripts:
// Delete global variables
delete global["count1"];
delete global["count2"];
delete global["count3"];
// Or
for (var i=1; i<=3; i++) delete global["count"+i];
// Or
for (var i in global) {
for (j=1; j<=3; j++) if (i=="count"+j) delete global[i];
}
@+
Copy link to clipboard
Copied
Be aware that a script can't rename a file. It will only save a copy of it under the new name.