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;
}
}