Creating a Save As Javascript Toolbar Menu Item

New Here ,
Aug 20, 2019

Copy link to clipboard

Copied

Hi, first off... I really don't have much familiarity with javascript. That being said, I've read some of the articles online about how to create the save as javascripts and trusted folders. I don't understand all of it but I've been able to piece some things together.

I'm trying to implement a way to perform a Save As to a shared network folder with a filename based on the values of various fields within the open PDF.

I've been able to do this with a button in the PDF by creating the following javascript and placing it in the trusted folder:

mySaveAs = app.trustPropagatorFunction(function(doc,path) {

app.beginPriv();

doc.saveAs(path);

app.endPriv();

})

myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path) {

// Privileged and/or non-privileged code above

app.beginPriv();

mySaveAs(doc,path);

app.endPriv();

// Privileged and/or non-privileged code below

});

Then adding a button on the PDF and adding the following javascript to the button:

// build file name

var myFileName = getField("BRANCH_NUMBER").value + "_" + getField("PRIMARY_FIRST_NAME").value + "_" + getField("PRIMARY_LAST_NAME").value + "_" + getField("PRIMARY_MEMBER_NUMBER").value + "_" + getField("Form_Name").value + "_" + getField("CurrentDate").value + getField("CurrentTime").value + ".pdf";

// add folder name

myFileName = "/gffg-fs/test/" + myFileName

myTrustedSpecialTaskFunc(this, myFileName);

app.alert("This Form (" + getField("BRANCH_NUMBER").value + "_" + getField("PRIMARY_FIRST_NAME").value + "_" + getField("PRIMARY_LAST_NAME").value + "_" + getField("PRIMARY_MEMBER_NUMBER").value + "_" + getField("Form_Name").value + "_" +

getField("CurrentDate").value + getField("CurrentTime").value + ") has saved successfully to the following location: /gffg-fs/test/" , 7);

And this works fine as-is. When the user clicks the button, it saves the PDF with a filename made up of the various fields values in the PDF and saves it to the appropriate folder.

Now, here's my problem... Instead of having a button on the PDF, I’d like to put this in a toolbar menu but I’ve been having difficulty. I’ve tried various things but keep coming up with errors. I’m assuming that I can’t just move the javascript that I have for the existing button and put it in the trusted folder javascript that I use to create the toolbar menu?

I’ve been able to create a javascript in the trusted folder to create the toolbar menu item but I’m not sure what should be in the cExec: section. I’m thinking this is the name of the code that will execute when they click on the toolbar menu item. But where do I put this?

app.addMenuItem({cName:"Save to Branch Audit Folder", cParent:"Edit", cExec:"myFileName()"});

Thanks in advance!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You need to use a function, and then call that function from the cExec parameter, like this:

app.addMenuItem({cName:"Save to Branch Audit Folder", cParent:"Edit", cExec:"saveMyFile()"});

function saveMyFile() {

    // your code goes here

}

// put the trusted functions here

All of this needs to go inside a .js file installed in the Javascripts folder, of course.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

73

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

Creating a Save As Javascript Toolbar Menu Item

New Here ,
Aug 20, 2019

Copy link to clipboard

Copied

Hi, first off... I really don't have much familiarity with javascript. That being said, I've read some of the articles online about how to create the save as javascripts and trusted folders. I don't understand all of it but I've been able to piece some things together.

I'm trying to implement a way to perform a Save As to a shared network folder with a filename based on the values of various fields within the open PDF.

I've been able to do this with a button in the PDF by creating the following javascript and placing it in the trusted folder:

mySaveAs = app.trustPropagatorFunction(function(doc,path) {

app.beginPriv();

doc.saveAs(path);

app.endPriv();

})

myTrustedSpecialTaskFunc = app.trustedFunction(function(doc,path) {

// Privileged and/or non-privileged code above

app.beginPriv();

mySaveAs(doc,path);

app.endPriv();

// Privileged and/or non-privileged code below

});

Then adding a button on the PDF and adding the following javascript to the button:

// build file name

var myFileName = getField("BRANCH_NUMBER").value + "_" + getField("PRIMARY_FIRST_NAME").value + "_" + getField("PRIMARY_LAST_NAME").value + "_" + getField("PRIMARY_MEMBER_NUMBER").value + "_" + getField("Form_Name").value + "_" + getField("CurrentDate").value + getField("CurrentTime").value + ".pdf";

// add folder name

myFileName = "/gffg-fs/test/" + myFileName

myTrustedSpecialTaskFunc(this, myFileName);

app.alert("This Form (" + getField("BRANCH_NUMBER").value + "_" + getField("PRIMARY_FIRST_NAME").value + "_" + getField("PRIMARY_LAST_NAME").value + "_" + getField("PRIMARY_MEMBER_NUMBER").value + "_" + getField("Form_Name").value + "_" +

getField("CurrentDate").value + getField("CurrentTime").value + ") has saved successfully to the following location: /gffg-fs/test/" , 7);

And this works fine as-is. When the user clicks the button, it saves the PDF with a filename made up of the various fields values in the PDF and saves it to the appropriate folder.

Now, here's my problem... Instead of having a button on the PDF, I’d like to put this in a toolbar menu but I’ve been having difficulty. I’ve tried various things but keep coming up with errors. I’m assuming that I can’t just move the javascript that I have for the existing button and put it in the trusted folder javascript that I use to create the toolbar menu?

I’ve been able to create a javascript in the trusted folder to create the toolbar menu item but I’m not sure what should be in the cExec: section. I’m thinking this is the name of the code that will execute when they click on the toolbar menu item. But where do I put this?

app.addMenuItem({cName:"Save to Branch Audit Folder", cParent:"Edit", cExec:"myFileName()"});

Thanks in advance!

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You need to use a function, and then call that function from the cExec parameter, like this:

app.addMenuItem({cName:"Save to Branch Audit Folder", cParent:"Edit", cExec:"saveMyFile()"});

function saveMyFile() {

    // your code goes here

}

// put the trusted functions here

All of this needs to go inside a .js file installed in the Javascripts folder, of course.

TOPICS
Acrobat SDK and JavaScript, Windows

Views

74

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
Aug 20, 2019 0
Most Valuable Participant ,
Aug 20, 2019

Copy link to clipboard

Copied

You need to use a function, and then call that function from the cExec parameter, like this:

app.addMenuItem({cName:"Save to Branch Audit Folder", cParent:"Edit", cExec:"saveMyFile()"});

function saveMyFile() {

    // your code goes here

}

// put the trusted functions here

All of this needs to go inside a .js file installed in the Javascripts folder, of course.

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
Reply
Loading...
Aug 20, 2019 0
New Here ,
Aug 20, 2019

Copy link to clipboard

Copied

Hi,

Thanks for the quick reply. I tried the below (putting the same javascript code that I had previously used on the button on the PDF) inside the .js file in the javascript folder. Note that I removed the previous pdf button just to make sure there was no potential conflict.

app.addMenuItem({cName:"Save to Branch Audit Folder", cParent:"Edit", cExec:"saveMyFile()"});

function saveMyFile(){

// build file name

var myFileName = getField("BRANCH_NUMBER").value + "_" + getField("PRIMARY_FIRST_NAME").value + "_" + getField("PRIMARY_LAST_NAME").value + "_" + getField("PRIMARY_MEMBER_NUMBER").value + "_" +

getField("Form_Name").value + "_" + getField("CurrentDate").value + getField("CurrentTime").value + ".pdf";

// add folder name

myFileName = "/gffg-fs/test/" + myFileName

myTrustedSpecialTaskFunc(this, myFileName);

app.alert("This Form (" + getField("BRANCH_NUMBER").value + "_" + getField("PRIMARY_FIRST_NAME").value + "_" + getField("PRIMARY_LAST_NAME").value + "_" + getField("PRIMARY_MEMBER_NUMBER").value + "_" + getField("Form_Name").value + "_" + getField("CurrentDate").value + getField("CurrentTime").value + ") has saved successfully to the following location: /gffg-fs/test/" , 7);

}

When I clicked on the toolbar menu item, I get an "internal error" message and the console shows the following: "ReferenceError: getField is not defined 6: Folder-Level:App:test.js

Can I use a getField in the .js file in the javascript folder? I know it works when I had it assigned to a button on the PDF.

Thanks

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
Reply
Loading...
Aug 20, 2019 0
Most Valuable Participant ,
Aug 20, 2019

Copy link to clipboard

Copied

I would use "this.getField" instead of just "getField".

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
Reply
Loading...
Aug 20, 2019 0
New Here ,
Aug 20, 2019

Copy link to clipboard

Copied

Hi,

Perfect! Thanks. I thought I tried using "this.getField" with the same error message but I missed changing one of the expressions.

You guys rock!

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
Reply
Loading...
Aug 20, 2019 0
try67 LATEST
Most Valuable Participant ,
Aug 20, 2019

Copy link to clipboard

Copied

That's great!

One small suggestion, though: I would rename the "path" variable in your trusted function to something else, as there's already a "path" property of the Document object and you might have a conflict between the two (although because it's in a different context that's not likely, but better not to risk it).

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
Reply
Loading...
Aug 20, 2019 0