• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

Trying to use Javascript to Overwrite an Audit File via document.saveAs

New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Hello,

I'm working on digitalizing a document that is subject to some strict standards about editing, and therefore auditing. As a way to accomodate this, I wanted to create and/or update an audit file that will keep track of the contents of the form, who changed them, and when.

 

I created a folder-level function that gets called upon a user triggering the document "willSave" action. This function checks for an audit file, and if it doesn't exist, creates one and saves contents appropriately. However, if the audit file already exists, it will try to open it up, edit it appropriately, and then overwrite it. However, it fails when it tries to overwrite it.

 

These are the error messages I get:
This one happens some of the time (doesn't seem to be a result of my code - when I debug, I can't get this to pop-up):

Will25789442962a_0-1661350887943.png

This one happens every time via saveAs (confirmed via debug):

Will25789442962a_1-1661352099127.png

 

 

I have debugged my code and it for sure is failing on the saveAs call. Referencing this article by Thom Parker, https://acrobatusers.com/tutorials/how-save-pdf-acrobat-javascript/, I thought overwriting should be possible. I tried this with a .txt file at first, but thought maybe it was a bug or limitation of not using a .pdf. Then I switched to a .pdf audit file, and it's the same issue.

 

Would really appreciate some insight!! See my folder level script below.

function saveItself(doc) {
	app.beginPriv();	
	app.alert("I ran!");
	doc.saveAs(doc.path);
	app.endPriv();	
}

function closePls(doc){
	app.beginPriv();
	doc.closeDoc(true);
	app.endPriv();
}

function openPls(auditPath) {
	app.beginPriv();
	app.alert("open ran!");
	return app.openDoc({cPath: auditPath});
	app.endPriv();
}



var vertBar="|";
var underscore="_";
var dateString=util.printd("dd-mmm-yyyy HH:MM:ss",new Date());





function trustedAuditFunction (doc) {
app.beginPriv();
var activeDocArr=[];
var cnt = 0;
var thisFile="";


var pathPDF=doc.path.replace(/.pdf/,"")+" - output.pdf";
var fileName=doc.documentFileName.replace(/.pdf/,"")+" - output.pdf";
var mostRecentSaveSnapshot="\n\nForm State on: "+dateString+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nCompletion\n"+doc.getField("Completion").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nDate"+vertBar+"WO"+vertBar+"Customer"+vertBar+"FF"+"Assembly Number"+vertBar+"Assembly Rev\n"+doc.getField("Date").value+vertBar+doc.getField("WO").value+vertBar+doc.getField("Customer").value+vertBar+doc.getField("FF").value+vertBar+doc.getField("Assembly Number").value+vertBar+doc.getField("Assembly Rev").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPrior"+vertBar+"ECN"+vertBar+"Additional\n"+doc.getField("Prior").value+" "+doc.getField("ECN").value+" "+doc.getField("Additional").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow1"+vertBar+"Date_33"+vertBar+"Line   StationRow1"+vertBar+"Check Box1"+vertBar+"Check Box11"+vertBar+"Verified byYes No NA"+vertBar+"Date_3"+vertBar+"Verified byYes No NA10"+vertBar+"Date_13"+vertBar+"Check Box21"+vertBar+"Check Box31"+vertBar+"Check Box41"+vertBar+"Verified byYes No NA20"+vertBar+"Date_23\n"+doc.getField("Performed byRow1").value+vertBar+doc.getField("Date_33").value+vertBar+doc.getField("Line   StationRow1").value+vertBar+doc.getField("Check Box1").value+vertBar+doc.getField("Check Box11").value+vertBar+doc.getField("Verified byYes No NA").value+vertBar+doc.getField("Date_3").value+vertBar+doc.getField("Verified byYes No NA10").value+vertBar+doc.getField("Date_13").value+vertBar+doc.getField("Check Box21").value+vertBar+doc.getField("Check Box31").value+vertBar+doc.getField("Check Box41").value+vertBar+doc.getField("Verified byYes No NA20").value+vertBar+doc.getField("Date_23").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow2"+vertBar+"Date_34"+vertBar+"Line   StationRow2"+vertBar+"Check Box2"+vertBar+"Check Box12"+vertBar+"Verified byYes No NA1"+vertBar+"Date_4"+vertBar+"Verified byYes No NA11"+vertBar+"Date_14"+vertBar+"Check Box22"+vertBar+"Check Box32"+vertBar+"Check Box42"+vertBar+"Verified byYes No NA21"+vertBar+"Date_24\n"+doc.getField("Performed byRow2").value+vertBar+doc.getField("Date_34").value+vertBar+doc.getField("Line   StationRow2").value+vertBar+doc.getField("Check Box2").value+vertBar+doc.getField("Check Box12").value+vertBar+doc.getField("Verified byYes No NA1").value+vertBar+doc.getField("Date_4").value+vertBar+doc.getField("Verified byYes No NA11").value+vertBar+doc.getField("Date_14").value+vertBar+doc.getField("Check Box22").value+vertBar+doc.getField("Check Box32").value+vertBar+doc.getField("Check Box42").value+vertBar+doc.getField("Verified byYes No NA21").value+vertBar+doc.getField("Date_24").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow3"+vertBar+"Date_35"+vertBar+"Line   StationRow3"+vertBar+"Check Box3"+vertBar+"Check Box13"+vertBar+"Verified byYes No NA2"+vertBar+"Date_5"+vertBar+"Verified byYes No NA12"+vertBar+"Date_15"+vertBar+"Check Box23"+vertBar+"Check Box33"+vertBar+"Check Box43"+vertBar+"Verified byYes No NA22"+vertBar+"Date_25\n"+doc.getField("Performed byRow3").value+vertBar+doc.getField("Date_35").value+vertBar+doc.getField("Line   StationRow3").value+vertBar+doc.getField("Check Box3").value+vertBar+doc.getField("Check Box13").value+vertBar+doc.getField("Verified byYes No NA2").value+vertBar+doc.getField("Date_5").value+vertBar+doc.getField("Verified byYes No NA12").value+vertBar+doc.getField("Date_15").value+vertBar+doc.getField("Check Box23").value+vertBar+doc.getField("Check Box33").value+vertBar+doc.getField("Check Box43").value+vertBar+doc.getField("Verified byYes No NA22").value+vertBar+doc.getField("Date_25").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow4"+vertBar+"Date_36"+vertBar+"Line   StationRow4"+vertBar+"Check Box4"+vertBar+"Check Box14"+vertBar+"Verified byYes No NA3"+vertBar+"Date_6"+vertBar+"Verified byYes No NA13"+vertBar+"Date_16"+vertBar+"Check Box24"+vertBar+"Check Box34"+vertBar+"Check Box44"+vertBar+"Verified byYes No NA23"+vertBar+"Date_26\n"+doc.getField("Performed byRow4").value+vertBar+doc.getField("Date_36").value+vertBar+doc.getField("Line   StationRow4").value+vertBar+doc.getField("Check Box4").value+vertBar+doc.getField("Check Box14").value+vertBar+doc.getField("Verified byYes No NA3").value+vertBar+doc.getField("Date_6").value+vertBar+doc.getField("Verified byYes No NA13").value+vertBar+doc.getField("Date_16").value+vertBar+doc.getField("Check Box24").value+vertBar+doc.getField("Check Box34").value+vertBar+doc.getField("Check Box44").value+vertBar+doc.getField("Verified byYes No NA23").value+vertBar+doc.getField("Date_26").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow5"+vertBar+"Date_37"+vertBar+"Line   StationRow5"+vertBar+"Check Box5"+vertBar+"Check Box15"+vertBar+"Verified byYes No NA4"+vertBar+"Date_7"+vertBar+"Verified byYes No NA14"+vertBar+"Date_17"+vertBar+"Check Box25"+vertBar+"Check Box35"+vertBar+"Check Box45"+vertBar+"Verified byYes No NA24"+vertBar+"Date_27\n"+doc.getField("Performed byRow5").value+vertBar+doc.getField("Date_37").value+vertBar+doc.getField("Line   StationRow5").value+vertBar+doc.getField("Check Box5").value+vertBar+doc.getField("Check Box15").value+vertBar+doc.getField("Verified byYes No NA4").value+vertBar+doc.getField("Date_7").value+vertBar+doc.getField("Verified byYes No NA14").value+vertBar+doc.getField("Date_17").value+vertBar+doc.getField("Check Box25").value+vertBar+doc.getField("Check Box35").value+vertBar+doc.getField("Check Box45").value+vertBar+doc.getField("Verified byYes No NA24").value+vertBar+doc.getField("Date_27").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow6"+vertBar+"Date_38"+vertBar+"Line   StationRow6"+vertBar+"Check Box6"+vertBar+"Check Box16"+vertBar+"Verified byYes No NA5"+vertBar+"Date_8"+vertBar+"Verified byYes No NA15"+vertBar+"Date_18"+vertBar+"Check Box26"+vertBar+"Check Box36"+vertBar+"Check Box46"+vertBar+"Verified byYes No NA25"+vertBar+"Date_28\n"+doc.getField("Performed byRow6").value+vertBar+doc.getField("Date_38").value+vertBar+doc.getField("Line   StationRow6").value+vertBar+doc.getField("Check Box6").value+vertBar+doc.getField("Check Box16").value+vertBar+doc.getField("Verified byYes No NA5").value+vertBar+doc.getField("Date_8").value+vertBar+doc.getField("Verified byYes No NA15").value+vertBar+doc.getField("Date_18").value+vertBar+doc.getField("Check Box26").value+vertBar+doc.getField("Check Box36").value+vertBar+doc.getField("Check Box46").value+vertBar+doc.getField("Verified byYes No NA25").value+vertBar+doc.getField("Date_28").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow7"+vertBar+"Date_39"+vertBar+"Line   StationRow7"+vertBar+"Check Box7"+vertBar+"Check Box17"+vertBar+"Verified byYes No NA6"+vertBar+"Date_9"+vertBar+"Verified byYes No NA16"+vertBar+"Date_19"+vertBar+"Check Box27"+vertBar+"Check Box37"+vertBar+"Check Box47"+vertBar+"Verified byYes No NA26"+vertBar+"Date_29\n"+doc.getField("Performed byRow7").value+vertBar+doc.getField("Date_39").value+vertBar+doc.getField("Line   StationRow7").value+vertBar+doc.getField("Check Box7").value+vertBar+doc.getField("Check Box17").value+vertBar+doc.getField("Verified byYes No NA6").value+vertBar+doc.getField("Date_9").value+vertBar+doc.getField("Verified byYes No NA16").value+vertBar+doc.getField("Date_19").value+vertBar+doc.getField("Check Box27").value+vertBar+doc.getField("Check Box37").value+vertBar+doc.getField("Check Box47").value+vertBar+doc.getField("Verified byYes No NA26").value+vertBar+doc.getField("Date_29").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow8"+vertBar+"Date_40"+vertBar+"Line   StationRow8"+vertBar+"Check Box8"+vertBar+"Check Box18"+vertBar+"Verified byYes No NA7"+vertBar+"Date_10"+vertBar+"Verified byYes No NA17"+vertBar+"Date_20"+vertBar+"Check Box28"+vertBar+"Check Box38"+vertBar+"Check Box48"+vertBar+"Verified byYes No NA27"+vertBar+"Date_30\n"+doc.getField("Performed byRow8").value+vertBar+doc.getField("Date_40").value+vertBar+doc.getField("Line   StationRow8").value+vertBar+doc.getField("Check Box8").value+vertBar+doc.getField("Check Box18").value+vertBar+doc.getField("Verified byYes No NA7").value+vertBar+doc.getField("Date_10").value+vertBar+doc.getField("Verified byYes No NA17").value+vertBar+doc.getField("Date_20").value+vertBar+doc.getField("Check Box28").value+vertBar+doc.getField("Check Box38").value+vertBar+doc.getField("Check Box48").value+vertBar+doc.getField("Verified byYes No NA27").value+vertBar+doc.getField("Date_30").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow9"+vertBar+"Date_41"+vertBar+"Line   StationRow9"+vertBar+"Check Box9"+vertBar+"Check Box19"+vertBar+"Verified byYes No NA8"+vertBar+"Date_11"+vertBar+"Verified byYes No NA18"+vertBar+"Date_21"+vertBar+"Check Box29"+vertBar+"Check Box39"+vertBar+"Check Box49"+vertBar+"Verified byYes No NA28"+vertBar+"Date_31\n"+doc.getField("Performed byRow9").value+vertBar+doc.getField("Date_41").value+vertBar+doc.getField("Line   StationRow9").value+vertBar+doc.getField("Check Box9").value+vertBar+doc.getField("Check Box19").value+vertBar+doc.getField("Verified byYes No NA8").value+vertBar+doc.getField("Date_11").value+vertBar+doc.getField("Verified byYes No NA18").value+vertBar+doc.getField("Date_21").value+vertBar+doc.getField("Check Box29").value+vertBar+doc.getField("Check Box39").value+vertBar+doc.getField("Check Box49").value+vertBar+doc.getField("Verified byYes No NA28").value+vertBar+doc.getField("Date_31").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow10"+vertBar+"Date_42"+vertBar+"Line   StationRow10"+vertBar+"Check Box10"+vertBar+"Check Box20"+vertBar+"Verified byYes No NA9"+vertBar+"Date_12"+vertBar+"Verified byYes No NA19"+vertBar+"Date_22"+vertBar+"Check Box30"+vertBar+"Check Box40"+vertBar+"Check Box50"+vertBar+"Verified byYes No NA29"+vertBar+"Date_32\n"+doc.getField("Performed byRow10").value+vertBar+doc.getField("Date_42").value+vertBar+doc.getField("Line   StationRow10").value+vertBar+doc.getField("Check Box10").value+vertBar+doc.getField("Check Box20").value+vertBar+doc.getField("Verified byYes No NA9").value+vertBar+doc.getField("Date_12").value+vertBar+doc.getField("Verified byYes No NA19").value+vertBar+doc.getField("Date_22").value+vertBar+doc.getField("Check Box30").value+vertBar+doc.getField("Check Box40").value+vertBar+doc.getField("Check Box50").value+vertBar+doc.getField("Verified byYes No NA29").value+vertBar+doc.getField("Date_32").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\n";

try {
	var oFile = util.readFileIntoStream(pathPDF);
	if (oFile!==null){
	//case when output file already exists. Append snapshot to it.
	var doc2=openTheAuditDoc(pathPDF);
	
	var exportFieldPDF=doc2.getField(doc2.getNthFieldName(0));
	exportFieldPDF.value=mostRecentSaveSnapshot+exportFieldPDF.value;
	exportFieldPDF.readonly=false;
auditSaveItself(doc2);	
auditCloseDoc(doc2);

} else {
	//case when no output file exists. Just save snapshot of document. We really shouldn't ever hit this code, because readFileIntoStream should throw an exception when the file doesn't exist. 
	var aCropRect = this.getPageBox("Crop",0);

	var nMiddle = aCropRect[2]/2; 
	
	var rectFld = [];
	rectFld[0] = nMiddle-900; 
	rectFld[1] = aCropRect[3]+1000;  
	rectFld[2] = nMiddle+900; 
	rectFld[3] = aCropRect[3];  
	//pdf file
	var oReport = new Report();
	oReport.size = 1.2; 
	oReport.style = "DefaultNoteText"; 
	var doc2=oReport.open(fileName);
	
	
	
	var exportFieldPDF=doc2.addField({cName: "Audit", cFieldType: "text", nPageNum: 0, oCoords:rectFld});//"Form State on "+dateString, cFieldType: "text", nPageNum: 0, oCoords:rectFld});
	exportFieldPDF.multiline=true;
	exportFieldPDF.readonly=true;
	exportFieldPDF.value=mostRecentSaveSnapshot;
	doc2.disclosed=true;
	
	
	
	
	app.setTimeOut(doc2.saveAs({cPath: pathPDF}), 3000);
	auditCloseDoc(doc2);
	}
}
catch (e) {
	app.alert("exception e: "+e);
if (oFile==null){
	//case when no output file exists. Just save snapshot of document.
	var aCropRect = this.getPageBox("Crop",0);
	var nMiddle = aCropRect[2]/2; 
	var rectFld = [];
	rectFld[0] = nMiddle-900; // Left side is 75pts to the left of the middle
	rectFld[1] = aCropRect[3]+1000;  // Top side is 20pts above the bottom
	rectFld[2] = nMiddle+900; // Right side is 75pts to the right of the middle
	rectFld[3] = aCropRect[3];  // Bottom is the same as the Crop Box
	//pdf file
	var oReport = new Report();
	oReport.size = 1.2; 
	oReport.style = "DefaultNoteText"; 
	var doc2=oReport.open(fileName);
	
	
	
	var exportFieldPDF=doc2.addField({cName: "Audit", cFieldType: "text", nPageNum: 0, oCoords:rectFld});
	exportFieldPDF.multiline=true;
	exportFieldPDF.readonly=true;
	doc2.disclosed=true;
	exportFieldPDF.value=mostRecentSaveSnapshot;
	
	app.setTimeOut(doc2.saveAs({cPath: pathPDF}), 3000);
	auditCloseDoc(doc2);
} else {
	app.alert("Unknown error.");
}
}

app.endPriv();


}

var trustedFunction=app.trustedFunction(trustedAuditFunction);

var openTheAuditDoc=app.trustedFunction(openPls);

var auditCloseDoc=app.trustedFunction(closePls);

var auditSaveItself=app.trustedFunction(saveItself);

 

Views

136

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

I've moved this from the Using the Community forum (which is the forum for issues using the forums) to the Acrobat forum.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

What's the full path of the file? Does the "I ran!" alert window show before the error message?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Hey try67,

Yep, the "I ran!" alert shows up one time, right before this error:

Pork37_0-1661356575471.png

 

The full path to the audit file is this (I whited out some personal information in the path):

Pork37_2-1661356856641.png

 

 

The pathPDF variable is what I use to save the file in the first place, so it should be the correct path for opening/saving it as well, I would think.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Does the path happen to contain a comma?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

No comma. It contains the following characters:

1. A standard space (" ")

2. A dash (-)

3. An underscore (_)

4. A period (.)

 

All of these should be acceptable characters in a Windows path.

 

I should mention that the code successfully opens and edits the audit file using this pathPDF variable, it just fails to save using this same path.

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Did you disclose both files?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Just to be triple sure, I added this to the top of my "trustedAuditFunction:"

function trustedAuditFunction (doc) {
app.beginPriv();
doc.disclosed=true;

 And also added this line right after opening the audit document:

var doc2=openTheAuditDoc(pathPDF);
	doc2.disclosed=true;

But, still no dice. Same errors as before. 

 

Just to be extra clear about the workflow here...

1. I'm opening the main document that people would edit, sign, save, etc.

2. I make a small change and then save/CTRL+S

3. This calls to the trustedAuditFunction, passing in "this" for the "doc" argument

4. Within the trustedAuditFunction, "doc" now represents the main document, and "doc2" upon assignment represents the audit document that I either want to create/append/edit.

 

So, I'm working with 2 documents at the same time within this folder level script. Opening works and closing works, but saving only works when creating a new document. Saving fails when attempting to save to an existing file that I open within Adobe Acrobat. All of these opening, closing, saving operations rely on the same file path (pathPDF). And now, given my tests above, both of these documents should be disclosed at the time of their use in this function.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

I would like to add another finding here which may be at the root of the problem.

 

I removed the call to the folder level script entirely, and set "this.disclosed=true;" in the console. I verified that this took, however, when I try to save normally, that's when this pops up!:

Pork37_0-1661361427780.png

 

I checked online about how to change this, and the main suggestion was to go to:
Edit > Preferences > Documents > In 'PDF/A View Mode' Change 'View documents in PDF/A mode' to 'Never'

 

I have made this change, yet, I still cannot even do a basic save without this popping up. What settings am I missing to make this an editable, NOT read-only copy?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

You have to embed this code in the file itself, not in your folder-level script. It won't work from there.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

By "this code" do you mean setting the disclosed property to true? That would make sense, and can do that. I'm still having problems with making the PDF not Read Only though... any thoughts on that?

 

I've set  'PDF/A View Mode' to Never:

Pork37_1-1661362752787.png

 

and I've confirmed that I don't have any security settings enabled for this document:

Pork37_0-1661362700109.png

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Yes, the latter code you posted, for disclosing the file.

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Thanks for clarifying about that.

 

Sorry to ask this a third time, but just want to make sure we are on the same page here. I'm now at the point where I am removing my code from the equation (no code runs no matter what actions I take in the document), and just want to make this document not read-only before continuing to work through any code. This seems to be the hangup and probably why saveAs won't overwrite properly, which is my core issue here. Do you have any suggestions outside of the steps I've already taken in my previous posts, to make a document not read-only?

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

I figured out why this was happening...

 

I had the "View > Preview Pane" selected. This preview was causing the file to be opened more than once, which is why the saveAs was telling me it was readOnly or opened elsewhere.

 

Preview Pane... more like Preview Pain 😉 lol

 

Thanks for all of your help try67. I appreciate the prompt assistance you've given so far

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Pork37_0-1661366840014.png

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Yes, that makes sense, but well spotted, and thanks for sharing the info with us!

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

No problem...

 

I wish the issues ended here, but, it seems I am back at square one. So, with resolving the Preview Pane problem, I am able to save documents manually without issue. So that's a great step forward.

 

What this means now, is that this is now the sole error coming up when my folder script runs (and this is when trying to save the audit file after I open it and edit it):

1. 

Pork37_0-1661369992589.png

 

I have gone into both the main document, and the already created audit file, and applied a "this.disclosed=true;" line of code to run onOpen for the document. So, the document should be disclosing itself prior to calling any folder level function. This didn't make a change though 😕

 

I'm open to any and all ideas... I have also changed the security settings of the folder I'm doing this in to allow EVERYONE full control of the files. Which should mean Windows shouldn't be getting in the way anymore...

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Aug 24, 2022 Aug 24, 2022

Copy link to clipboard

Copied

Start eliminating variables. Move the file to a simple folder location (like C:\Temp\) and give it a simple name (like 1.pdf), and then run just the save command using that path. Does that work?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Aug 26, 2022 Aug 26, 2022

Copy link to clipboard

Copied

LATEST

Hey there try67,

Just wanted to provide you with an update here. Thank you very much for your support, and for suggesting I simplify my code. This helped me resolve my problems here and effectively get me where I wanted to be with this script 🙂

 

Here is the new code I am now using, in case it's helpful for anyone in the future or you're curious where I went with it. It effectively is creating an audit pdf file (meaning, creates a pdf with | delimited textboxes, which includes the date and time the document was saved, and all the fields and their corresponding values at that point in time), is able to open it in the background, edit it, and save it. I can also export to a .txt file from that audit file using a menu item which is especially useful for actually looking through the audit content (the pdf audit file is more of a means to store the audit info and easily retrieve it with code - not view it).

function addMenuToExportText(){
	app.beginPriv();
	app.addMenuItem({cName:"Export Audit File as TXT", cParent:"File", cExec:"auditExportEverythingToATXTFile(this);"});
	app.endPriv();
}

var vertBar="|";
var exportFieldPDF="";

function saveItself(doc,auditPath) {
	app.beginPriv();	
	doc.saveAs(auditPath);
	app.endPriv();	
}

function closePls(doc){
	app.beginPriv();
	doc.closeDoc(true);
	app.endPriv();
}

function openPls(doc,auditPath) {
	app.beginPriv();
	var fileName=doc.documentFileName.replace(/.pdf/,"")+" - output.pdf";
	var doc2="";
	var oReport="";
	try{
		doc2=app.openDoc({cPath: auditPath, bHidden: true});
	}
	catch (e) {
		//if cannot find file in try block, need to create one in catch block.
	//create pdf file
	oReport = new Report();
	oReport.size = 1.2; 
	oReport.style = "DefaultNoteText";  
	doc2=oReport.open(fileName);
	}
	return doc2;
	app.endPriv();
}

function trustedAuditFunction (doc) {
app.beginPriv();
var dateString=util.printd("dd-mmm-yyyy HH:MM:ss",new Date());
var mostRecentSaveSnapshot="\n\nForm State on: "+dateString+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nCompletion\n"+doc.getField("Completion").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nDate"+vertBar+"WO"+vertBar+"Customer"+vertBar+"FF"+"Assembly Number"+vertBar+"Assembly Rev\n"+doc.getField("Date").value+vertBar+doc.getField("WO").value+vertBar+doc.getField("Customer").value+vertBar+doc.getField("FF").value+vertBar+doc.getField("Assembly Number").value+vertBar+doc.getField("Assembly Rev").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPrior"+vertBar+"ECN"+vertBar+"Additional\n"+doc.getField("Prior").value+" "+doc.getField("ECN").value+" "+doc.getField("Additional").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow1"+vertBar+"Date_33"+vertBar+"Line   StationRow1"+vertBar+"Check Box1"+vertBar+"Check Box11"+vertBar+"Verified byYes No NA"+vertBar+"Date_3"+vertBar+"Verified byYes No NA10"+vertBar+"Date_13"+vertBar+"Check Box21"+vertBar+"Check Box31"+vertBar+"Check Box41"+vertBar+"Verified byYes No NA20"+vertBar+"Date_23\n"+doc.getField("Performed byRow1").value+vertBar+doc.getField("Date_33").value+vertBar+doc.getField("Line   StationRow1").value+vertBar+doc.getField("Check Box1").value+vertBar+doc.getField("Check Box11").value+vertBar+doc.getField("Verified byYes No NA").value+vertBar+doc.getField("Date_3").value+vertBar+doc.getField("Verified byYes No NA10").value+vertBar+doc.getField("Date_13").value+vertBar+doc.getField("Check Box21").value+vertBar+doc.getField("Check Box31").value+vertBar+doc.getField("Check Box41").value+vertBar+doc.getField("Verified byYes No NA20").value+vertBar+doc.getField("Date_23").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow2"+vertBar+"Date_34"+vertBar+"Line   StationRow2"+vertBar+"Check Box2"+vertBar+"Check Box12"+vertBar+"Verified byYes No NA1"+vertBar+"Date_4"+vertBar+"Verified byYes No NA11"+vertBar+"Date_14"+vertBar+"Check Box22"+vertBar+"Check Box32"+vertBar+"Check Box42"+vertBar+"Verified byYes No NA21"+vertBar+"Date_24\n"+doc.getField("Performed byRow2").value+vertBar+doc.getField("Date_34").value+vertBar+doc.getField("Line   StationRow2").value+vertBar+doc.getField("Check Box2").value+vertBar+doc.getField("Check Box12").value+vertBar+doc.getField("Verified byYes No NA1").value+vertBar+doc.getField("Date_4").value+vertBar+doc.getField("Verified byYes No NA11").value+vertBar+doc.getField("Date_14").value+vertBar+doc.getField("Check Box22").value+vertBar+doc.getField("Check Box32").value+vertBar+doc.getField("Check Box42").value+vertBar+doc.getField("Verified byYes No NA21").value+vertBar+doc.getField("Date_24").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow3"+vertBar+"Date_35"+vertBar+"Line   StationRow3"+vertBar+"Check Box3"+vertBar+"Check Box13"+vertBar+"Verified byYes No NA2"+vertBar+"Date_5"+vertBar+"Verified byYes No NA12"+vertBar+"Date_15"+vertBar+"Check Box23"+vertBar+"Check Box33"+vertBar+"Check Box43"+vertBar+"Verified byYes No NA22"+vertBar+"Date_25\n"+doc.getField("Performed byRow3").value+vertBar+doc.getField("Date_35").value+vertBar+doc.getField("Line   StationRow3").value+vertBar+doc.getField("Check Box3").value+vertBar+doc.getField("Check Box13").value+vertBar+doc.getField("Verified byYes No NA2").value+vertBar+doc.getField("Date_5").value+vertBar+doc.getField("Verified byYes No NA12").value+vertBar+doc.getField("Date_15").value+vertBar+doc.getField("Check Box23").value+vertBar+doc.getField("Check Box33").value+vertBar+doc.getField("Check Box43").value+vertBar+doc.getField("Verified byYes No NA22").value+vertBar+doc.getField("Date_25").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow4"+vertBar+"Date_36"+vertBar+"Line   StationRow4"+vertBar+"Check Box4"+vertBar+"Check Box14"+vertBar+"Verified byYes No NA3"+vertBar+"Date_6"+vertBar+"Verified byYes No NA13"+vertBar+"Date_16"+vertBar+"Check Box24"+vertBar+"Check Box34"+vertBar+"Check Box44"+vertBar+"Verified byYes No NA23"+vertBar+"Date_26\n"+doc.getField("Performed byRow4").value+vertBar+doc.getField("Date_36").value+vertBar+doc.getField("Line   StationRow4").value+vertBar+doc.getField("Check Box4").value+vertBar+doc.getField("Check Box14").value+vertBar+doc.getField("Verified byYes No NA3").value+vertBar+doc.getField("Date_6").value+vertBar+doc.getField("Verified byYes No NA13").value+vertBar+doc.getField("Date_16").value+vertBar+doc.getField("Check Box24").value+vertBar+doc.getField("Check Box34").value+vertBar+doc.getField("Check Box44").value+vertBar+doc.getField("Verified byYes No NA23").value+vertBar+doc.getField("Date_26").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow5"+vertBar+"Date_37"+vertBar+"Line   StationRow5"+vertBar+"Check Box5"+vertBar+"Check Box15"+vertBar+"Verified byYes No NA4"+vertBar+"Date_7"+vertBar+"Verified byYes No NA14"+vertBar+"Date_17"+vertBar+"Check Box25"+vertBar+"Check Box35"+vertBar+"Check Box45"+vertBar+"Verified byYes No NA24"+vertBar+"Date_27\n"+doc.getField("Performed byRow5").value+vertBar+doc.getField("Date_37").value+vertBar+doc.getField("Line   StationRow5").value+vertBar+doc.getField("Check Box5").value+vertBar+doc.getField("Check Box15").value+vertBar+doc.getField("Verified byYes No NA4").value+vertBar+doc.getField("Date_7").value+vertBar+doc.getField("Verified byYes No NA14").value+vertBar+doc.getField("Date_17").value+vertBar+doc.getField("Check Box25").value+vertBar+doc.getField("Check Box35").value+vertBar+doc.getField("Check Box45").value+vertBar+doc.getField("Verified byYes No NA24").value+vertBar+doc.getField("Date_27").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow6"+vertBar+"Date_38"+vertBar+"Line   StationRow6"+vertBar+"Check Box6"+vertBar+"Check Box16"+vertBar+"Verified byYes No NA5"+vertBar+"Date_8"+vertBar+"Verified byYes No NA15"+vertBar+"Date_18"+vertBar+"Check Box26"+vertBar+"Check Box36"+vertBar+"Check Box46"+vertBar+"Verified byYes No NA25"+vertBar+"Date_28\n"+doc.getField("Performed byRow6").value+vertBar+doc.getField("Date_38").value+vertBar+doc.getField("Line   StationRow6").value+vertBar+doc.getField("Check Box6").value+vertBar+doc.getField("Check Box16").value+vertBar+doc.getField("Verified byYes No NA5").value+vertBar+doc.getField("Date_8").value+vertBar+doc.getField("Verified byYes No NA15").value+vertBar+doc.getField("Date_18").value+vertBar+doc.getField("Check Box26").value+vertBar+doc.getField("Check Box36").value+vertBar+doc.getField("Check Box46").value+vertBar+doc.getField("Verified byYes No NA25").value+vertBar+doc.getField("Date_28").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow7"+vertBar+"Date_39"+vertBar+"Line   StationRow7"+vertBar+"Check Box7"+vertBar+"Check Box17"+vertBar+"Verified byYes No NA6"+vertBar+"Date_9"+vertBar+"Verified byYes No NA16"+vertBar+"Date_19"+vertBar+"Check Box27"+vertBar+"Check Box37"+vertBar+"Check Box47"+vertBar+"Verified byYes No NA26"+vertBar+"Date_29\n"+doc.getField("Performed byRow7").value+vertBar+doc.getField("Date_39").value+vertBar+doc.getField("Line   StationRow7").value+vertBar+doc.getField("Check Box7").value+vertBar+doc.getField("Check Box17").value+vertBar+doc.getField("Verified byYes No NA6").value+vertBar+doc.getField("Date_9").value+vertBar+doc.getField("Verified byYes No NA16").value+vertBar+doc.getField("Date_19").value+vertBar+doc.getField("Check Box27").value+vertBar+doc.getField("Check Box37").value+vertBar+doc.getField("Check Box47").value+vertBar+doc.getField("Verified byYes No NA26").value+vertBar+doc.getField("Date_29").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow8"+vertBar+"Date_40"+vertBar+"Line   StationRow8"+vertBar+"Check Box8"+vertBar+"Check Box18"+vertBar+"Verified byYes No NA7"+vertBar+"Date_10"+vertBar+"Verified byYes No NA17"+vertBar+"Date_20"+vertBar+"Check Box28"+vertBar+"Check Box38"+vertBar+"Check Box48"+vertBar+"Verified byYes No NA27"+vertBar+"Date_30\n"+doc.getField("Performed byRow8").value+vertBar+doc.getField("Date_40").value+vertBar+doc.getField("Line   StationRow8").value+vertBar+doc.getField("Check Box8").value+vertBar+doc.getField("Check Box18").value+vertBar+doc.getField("Verified byYes No NA7").value+vertBar+doc.getField("Date_10").value+vertBar+doc.getField("Verified byYes No NA17").value+vertBar+doc.getField("Date_20").value+vertBar+doc.getField("Check Box28").value+vertBar+doc.getField("Check Box38").value+vertBar+doc.getField("Check Box48").value+vertBar+doc.getField("Verified byYes No NA27").value+vertBar+doc.getField("Date_30").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow9"+vertBar+"Date_41"+vertBar+"Line   StationRow9"+vertBar+"Check Box9"+vertBar+"Check Box19"+vertBar+"Verified byYes No NA8"+vertBar+"Date_11"+vertBar+"Verified byYes No NA18"+vertBar+"Date_21"+vertBar+"Check Box29"+vertBar+"Check Box39"+vertBar+"Check Box49"+vertBar+"Verified byYes No NA28"+vertBar+"Date_31\n"+doc.getField("Performed byRow9").value+vertBar+doc.getField("Date_41").value+vertBar+doc.getField("Line   StationRow9").value+vertBar+doc.getField("Check Box9").value+vertBar+doc.getField("Check Box19").value+vertBar+doc.getField("Verified byYes No NA8").value+vertBar+doc.getField("Date_11").value+vertBar+doc.getField("Verified byYes No NA18").value+vertBar+doc.getField("Date_21").value+vertBar+doc.getField("Check Box29").value+vertBar+doc.getField("Check Box39").value+vertBar+doc.getField("Check Box49").value+vertBar+doc.getField("Verified byYes No NA28").value+vertBar+doc.getField("Date_31").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\nPerformed byRow10"+vertBar+"Date_42"+vertBar+"Line   StationRow10"+vertBar+"Check Box10"+vertBar+"Check Box20"+vertBar+"Verified byYes No NA9"+vertBar+"Date_12"+vertBar+"Verified byYes No NA19"+vertBar+"Date_22"+vertBar+"Check Box30"+vertBar+"Check Box40"+vertBar+"Check Box50"+vertBar+"Verified byYes No NA29"+vertBar+"Date_32\n"+doc.getField("Performed byRow10").value+vertBar+doc.getField("Date_42").value+vertBar+doc.getField("Line   StationRow10").value+vertBar+doc.getField("Check Box10").value+vertBar+doc.getField("Check Box20").value+vertBar+doc.getField("Verified byYes No NA9").value+vertBar+doc.getField("Date_12").value+vertBar+doc.getField("Verified byYes No NA19").value+vertBar+doc.getField("Date_22").value+vertBar+doc.getField("Check Box30").value+vertBar+doc.getField("Check Box40").value+vertBar+doc.getField("Check Box50").value+vertBar+doc.getField("Verified byYes No NA29").value+vertBar+doc.getField("Date_32").value+"\n\n----- ----- ----- ----- ----- ----- ----- ----- ----- -----\n\n";
var doc2="";
var auditPath=doc.path.replace(/.pdf/,"")+" - output.pdf";
try {
	doc2=openTheAuditDoc(doc,auditPath); //this creates and opens a file, or it opens an already existing one.
	exportFieldPDF=auditFormFieldDeterminer(doc2);
	exportFieldPDF.value=mostRecentSaveSnapshot+exportFieldPDF.value; //regardless of if document is new or existing, this code should work based on the openTheAuditDoc handling.
	auditSaveItself(doc2,auditPath);
	auditCloseDoc(doc2);
} 
catch (e) {
	app.alert(e + "found an audit document, but failed with an action taken on it.");
	auditCloseDoc(doc2);
}
app.endPriv();
}

function exportEverythingToATXTFile (auditDoc){
		auditDoc.exportAsText({cPath: auditDoc.path.replace(/.pdf/,"")+".txt"});
}

function formFieldDeterminer(doc2){
		var exportFieldPDFValue="";
		if(doc2.numFields==0){
			//create the first field in the audit file
				var aCropRect = this.getPageBox("Crop",0);
		// Calculate the placement rectangle for the text box.
	// Field is centered so find the mid point of the page
	var nMiddle = aCropRect[2]/2; 
	// Field is along bottom edge of crop, 150 points wide, 20 points tall
	var rectFld = [];
	rectFld[0] = nMiddle-900; // Left side is 75pts to the left of the middle
	rectFld[1] = aCropRect[3]+1000;  // Top side is 20pts above the bottom
	rectFld[2] = nMiddle+900; // Right side is 75pts to the right of the middle
	rectFld[3] = aCropRect[3];  // Bottom is the same as the Crop Box
	exportFieldPDF=doc2.addField({cName: "1", cFieldType: "text", nPageNum: 0, oCoords:rectFld});
	doc2.setPageAction(0, "Open", "auditAddMenuToExportText();"); //add menu item to allow for exporting to TXT file from the mess that is this audit PDF.
		}
		else if(doc2.numFields>0){
			exportFieldPDFValue=doc2.getField(doc2.getNthFieldName(doc2.numFields-1)).value;
			if (exportFieldPDFValue.split("Form State").length==3){
				exportFieldPDF=doc2.addField({cName: (doc2.numFields+1), cFieldType: "text", nPageNum: 0, oCoords:[doc2.numFields,doc2.numFields,doc2.numFields,doc2.numFields]});
			} else {
				exportFieldPDF=doc2.getField(doc2.getNthFieldName(doc2.numFields-1));
			}
	}
	exportFieldPDF.multiline=true;
	return exportFieldPDF;	
}

var trustedFunction=app.trustedFunction(trustedAuditFunction);

var openTheAuditDoc=app.trustedFunction(openPls);

var auditCloseDoc=app.trustedFunction(closePls);

var auditSaveItself=app.trustedFunction(saveItself);

var auditExportEverythingToATXTFile=app.trustedFunction(exportEverythingToATXTFile);

var auditFormFieldDeterminer=app.trustedFunction(formFieldDeterminer);

var auditAddMenuToExportText=app.trustedFunction(addMenuToExportText);

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines