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

Disabling Javascript in new file when Save As is used

New Here ,
May 22, 2023 May 22, 2023

I have a PDF form that I put together that has a document number at the top.  I have a javascript in the document to increment that number up by 1 every time the form is opened:

 

var v = +this.getField("Permit Number").valueAsString+3000;

this.getField("Permit Number").value = util.printf("%04d", (v+1-3000));

 

I know very little javascript and I found this example somewhere online and modified it for my own needs.  

Is there a way so that when this form is filled out and Save As is used to save the filled out form that the javascript is disabled in the new file?  I don't want the document number to change again if someone opens the filled out form later on.

TOPICS
JavaScript
1.4K
Translate
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
1 ACCEPTED SOLUTION
Community Expert ,
May 22, 2023 May 22, 2023

You can use something like this, then (adjust the actual file name in the first line, of course):

 

if (this.documentFileName=="OriginalName.pdf") {
	var v = +this.getField("Permit Number").valueAsString+3000;
	this.getField("Permit Number").value = util.printf("%04d", (v+1-3000));
}

View solution in original post

Translate
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 ,
May 22, 2023 May 22, 2023

You can modify the code so that it only updates if the file has a specific file name (and not others). Would that work for you?

Translate
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 ,
May 22, 2023 May 22, 2023

That would I do believe work perfectly!

Translate
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 ,
May 22, 2023 May 22, 2023

You can use something like this, then (adjust the actual file name in the first line, of course):

 

if (this.documentFileName=="OriginalName.pdf") {
	var v = +this.getField("Permit Number").valueAsString+3000;
	this.getField("Permit Number").value = util.printf("%04d", (v+1-3000));
}
Translate
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 ,
May 23, 2023 May 23, 2023

That works great!  Thank you!  

 

Now a follow up question which may be a little more tricky:

I'm now trying to add to the script to have it save the file immediately after opening and having the new number plugged in so it's ready for the next time being opened (especially after the form is filled out and it's "saved as" a different file name).  Trying to use app.execMenuItem("Save"); doesn't work which seems to be due to security concerns; doing a lot of searching on the web to make this function has not gotten me very far.  Thoughts?

Translate
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 ,
May 23, 2023 May 23, 2023

In order to do that you must install a script file on the local machine of each user who will use this file. Can you do that? If not, the closest you can get is to open the Save As dialog. Just change "Save" to "SaveAs" in your code.

Translate
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 ,
May 23, 2023 May 23, 2023
LATEST

Unfortunately no; they are managed machines here and we have no installation or admin access of any kind.  The SaveAs should work well enough though as a reminder for them to save it before editing. 

 

Thank you very much for all your help!

Translate
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