Skip to main content
Inspiring
September 28, 2023
Question

REPORT TEMPLATE - Spawn, Extract, Rename, & Delete

  • September 28, 2023
  • 1 reply
  • 1641 views

So, I have stumbled upon a trifecta by managing to get a script working that spawns, extracts, and deletes the spawned page from the original document -- effectively creating a separate uneditable report document using my read-only "DISPLAY..." fieldnames method in a template contained within the parent document.  As the report document is always spawned to the last page, I have set the extract & delete to the last page as well.

 

//Working Script
this.getTemplate("TRANSCOM").spawn();
var sourceDocument = this;
var totalPageCount = this.numPages;
this.pageNum = totalPageCount - 1;
this.extractPages({nStart: this.pageNum =
totalPageCount - 1})
sourceDocument.deletePages(totalPageCount - 1);

//END


Unfortunately, I require a superfecta and have failed miserably in accomplishing a "save as" or script prompted "rename" of the extracted page in addition to the previously successful spawn/extract/delete.  I have attempted numerous methods with zero success to include attempts of scripting a predetermined filename -- all to no avail.

 

//Nonworking Script

this.getTemplate("TRANSCOM").spawn();
var sourceDocument = this;
var totalPageCount = this.numPages;
this.pageNum = totalPageCount -1;
this.extractPages({nStart: this.pageNum = totalPageCount - 1});
extractedPageFile.rename(newFilePath);
var newFilename = app.response("TRANSCOM");
if (newFilename) {
var currentPath = this.path;
var fileExtension = currentPath.substring(currentPath.lastIndexOf("."));
var newPath = currentPath.substring(0, currentPath.lastIndexOf("/") + 1) + newFilename + fileExtension;
this.saveAs(newPath);
this.closeDoc(true);
app.openDoc(newPath);
} else {
app.alert("Please rename file using the following format 'TRANSCOM-ddMMMyyyy'.");
}
sourceDocument.deletePages(totalPageCount - 1);
//END

I've managed to get the "newFilename & app.response" working independently as a scripted button but once combined with the template spawn and extraction, there is an all stop.  Does anybody have any ideas?  Any and all assistance is greatly appreciated. 🙂

This topic has been closed for replies.

1 reply

Thom Parker
Community Expert
Community Expert
September 28, 2023

So it's important to assign values to variables before using them, and to use the Acrobat JavaScript reference so that the Acrobat JS objects, functions, and properties are used correctly. And also so that you are using real functions. 

 

Did you look at the JavaScript console to see what error is reported?   

Here's a tutorial on the console:

https://www.pdfscripting.com/public/images/video/AcroJSIntro/AcroJSIntro_ConsoleWindow.cfm 

 

I think I mentioned this before, but extracting a page is a really bad idea for a script inside a document. And it just isn't going to work here. There are also many errors and some non-sense code.  

Read this:

https://www.pdfscripting.com/public/How-to-Save-a-PDF-2.cfm

and this:

https://www.pdfscripting.com/public/Trust-and-Privilege-in-Acrobat-Scripts.cfm

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
September 28, 2023

The JS console merely responds with a terribly unhelpful "undefined".  As for any errors and nonsense code... I will happily hit the "I believe" button as I have zero depth of understanding of what I'm doing.  I think I'v mentioned this before, but I occassionally manage to cobble things together that work and more often than not, come to the Adobe forums as a means to fix what I've either broken or cannot figure out, :S 

 

All of this fun gets dumped on my lap becuase I'm a stubborn knuckldragger who refuses to quit -- there is nearly always a way. 😉  

try67
Community Expert
Community Expert
September 28, 2023

"undefined" is not an error. The opposite, actually. It means the code has finished executing, without returning any values or causing any errors.