Multiple documents while running javascript

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Post Reply
OregonJohn
User
Posts: 14
Joined: Mon Jul 13, 2009 12:27 pm

Multiple documents while running javascript

Post by OregonJohn »

Hope someone can help.

I have javascript code to apply a chosen comment to all or some pages of a document.

When I use it to duplicate a comment on, say, a 500 page document it can take 10 minutes to finish.

While it is running I can open a new document but cannot do anything with it, or the one for which javascript is running.

Is there anything I can do so I can run the javascript code but also work on another document?

Below is the script. Any other ideas on improving it would be nice. I added a pop up when finished to show elapsed time in minutes. For anyone who doesn't know, use Ctrl-j to open a javascript console, paste all of the following into it then click the Run button.

Code: Select all

/*
Title: COPY ANNOTATION TO USER DEFINED PAGE RANGE
Author:  John Statler
Purpose: Prompt user to enter required page
         range to copy annotation to other pages, e.g. 1-5,7,9-20
*/

//Get user response


try{
	var annt = this.selectedAnnots[0]
	var props = annt.getProps();
}catch(e){
	app.alert("You have not selected an annotation to duplicate");
}

var pagesAll = ""
var annt = this.selectedAnnots[0]
var props = annt.getProps()
var anntPage = this.pageNum + 1


var cResponse = app.response({
cQuestion: "Enter in the pages where you wish to repeat the comment,\n" +
	"      e.g.     1,5,10-19,25\n" +
	"All pages are chosen by default.\n\n" +
	"For large documents it may take a second to do 8 pages.\n" +
	"So 120 pages would take 15 seconds to finish.\n\n" +
	"For vertical stamps, first rotate the document, apply stamp, then rotate back.",
cTitle: "Print",
cDefault: "1-" + this.numPages,
cLabel: "Pages:"
});

if ( cResponse == null) {
    app.alert("No pages entered");
} else {
   var d1 = new Date();
   var strInput = cResponse;
   var strChar;
   var arPrint = new Array(10);
   var arCount = 0;
   arPrint[arCount] = "";

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

  strChar = strInput.substr(i,1);

    //Check character and form page group
    if (IsInteger(strChar) == 0){
        arPrint[arCount] = arPrint[arCount] + strChar;
    }

    if (IsDash(strChar) == 0){
        arPrint[arCount] = arPrint[arCount] + strChar;
    }

    if (IsComma(strChar) == 0){
        arCount++;
        arPrint[arCount] = "";
    }

}

 for (i=0;i<(arCount+1);i++){

     if (arPrint[i].indexOf("-") > 0){
            var dashPos;
            dashPos = (arPrint[i].indexOf("-"));

            var pageStart = arPrint[i].substr(0,dashPos);
            var pageEnd = arPrint[i].substr(arPrint[i].indexOf("-")+1,
                          (arPrint[i].length - dashPos+1));

            this.print({
                bUI: false,
                nStart: pageStart - 1,
                nEnd: pageEnd - 1,
                bSilent: true
                });
				var pagesAll = pagesAll + range(Number(pageStart),pageEnd - pageStart + 1) + ",";

     } else {
            this.print({
                bUI: false,
                nStart: arPrint[i] - 1,
                nEnd: arPrint[i] - 1,
                bSilent: true
                });
				pagesAll = pagesAll + arPrint[i] + ",";
     }
 }
}

pagesAll = pagesAll.replace(/,\s*$/, "");
var page = pagesAll.split(',')
for (var a in page)
{
	var page1 = page[a]
	props.page = page1 - 1
	if (page1 != anntPage) {
	this.addAnnot(props)
	}
}

var d2 = new Date();
var SecsElapsed =  (d2- d1)/1000;
var SecsElapsed2 = SecsElapsed.toFixed(0);
var MinsElapsed2 = SecsElapsed2/60;
app.alert("Minutes elapsed: " + MinsElapsed2.toFixed(2));

 function range(start, count) {
      return Array.apply(0, Array(count))
        .map(function (element, index) {
          return index + start;
      });
    }

function IsComma(strChar){

    if (strChar == ",") {
        return 0;
    } else {
        return -1;
    }
}

function IsSpace(strChar){
    if (strChar == " ") {
        return 0;
    } else {
        return -1;
    }
}

function IsDash(strChar){
    if (strChar == "-") {
        return 0;
    } else {
        return -1;
    }
}

function IsInteger(strChar){
    if (strChar >=0 || strChar <= 9) {
        return 0;
    } else {
        return -1;
    }
}
Thanks in advance. John.
User avatar
Paul - Tracker Supp
Site Admin
Posts: 6829
Joined: Wed Mar 25, 2009 10:37 pm
Location: Chemainus, Canada
Contact:

Re: Multiple documents while running javascript

Post by Paul - Tracker Supp »

Hi John,

I asked the Editor lead developer about this. To make that JavaScript run in the background such that yo can work on other documents while the script is running would require a huge amount of work, in fact the amount of work required was humourously described as "hugest huge, for sure" so I do not see this happening in the near future I'm afraid.
sorry I don't have better news.

Nice script BTW. :-)
Best regards

Paul O'Rorke
Tracker Support North America
http://www.tracker-software.com
OregonJohn
User
Posts: 14
Joined: Mon Jul 13, 2009 12:27 pm

Re: Multiple documents while running javascript

Post by OregonJohn »

Thanks for the compliment on the script and for checking out the possibilities.

Is there any chance of making a user created plugin? It really is a bother to have to copy my javascript text, activate pdf-xchange, crtl-j, paste, run.

I noticed that Acrobat does allow, in some products, to remember a javascript.

Also, in case anyone wants to use that script there is a small problem -- it would actually print to the default printer as well as put the annotations on other pages.

To fix that, just remove the part that is:
this.print({
bUI: false,
nStart: pageStart - 1,
nEnd: pageEnd - 1,
bSilent: true
});

Great product, we love it.
Sasha - Tracker Dev Team
User
Posts: 5522
Joined: Fri Nov 21, 2014 8:27 am
Contact:

Re: Multiple documents while running javascript

Post by Sasha - Tracker Dev Team »

Hello OregonJohn,

Well, as an idea, you can give the list of the document paths to work with in your script. That should automate this a little, if that would suffice for you that is.

Cheers,
Alex
Subscribe at:
https://www.youtube.com/channel/UC-TwAMNi1haxJ1FX3LvB4CQ
Post Reply