Folder Level Script Required to add Toolbar Item

Jan 08, 2018

Copy link to clipboard

Copied

Hello Guys i tried my level best to add my code to add in tool bar. I cant succeed. I am beginner so i unable to grasp some complicated technical terms. Anyone please add the folder scripting code to run my existing code. Below code is i am using in Acrobat 11 but now i want to do it in acrobat 9 tool bar. Please help.

// pages on which that string is found to a new file.

var pageArray = [];

var stringToSearchFor = "SHEET";

for (var p = 0; p < this.numPages; p++) {

    // iterate over all words

    for (var n = 0; n < this.getPageNumWords(p); n++) {

        if (this.getPageNthWord(p, n) == stringToSearchFor) {

            pageArray.push(p);

            break;

        }

    }

}

if (pageArray.length > 0) {

    // extract all pages that contain the string into a new document

    var d = app.newDoc();   

// this will add a blank page - we need to remove that once we are done

    for (var n = 0; n < pageArray.length; n++) {

        d.insertPages( {

            nPage: d.numPages-1,

            cPath: this.path,

            nStart: pageArray,

            nEnd: pageArray,

        } );

    }

    // remove the first page

    d.deletePages(0);

   var annots=d.getAnnots();

    for (i in annots) annots.destroy();

// Save the new document:

var myDocPath = this.path;

var aPathComps = myDocPath.split("/");

var myFilename = aPathComps[aPathComps.length-2]; // Last element

d.saveAs("/c/QC Tool/Art Pages/"+ myFilename+".xlsx", "com.adobe.acrobat.xlsx");

d.closeDoc(true); 

}

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You put your code in a function and then call that function using the cExec method of addToolButton, like this:

app.addToolButton({ cName: "MyScript1", cLabel: "Extract SHEET Pages", cTooltext: "Extract SHEET Pages",

    cExec: "extractSheetPages()", cEnable: "event.rc = (event.target != null);"});

  

function extractSheetPages() {

    // put the rest of your code here

}

TOPICS
Acrobat SDK and JavaScript, Windows

Views

795

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

Folder Level Script Required to add Toolbar Item

Jan 08, 2018

Copy link to clipboard

Copied

Hello Guys i tried my level best to add my code to add in tool bar. I cant succeed. I am beginner so i unable to grasp some complicated technical terms. Anyone please add the folder scripting code to run my existing code. Below code is i am using in Acrobat 11 but now i want to do it in acrobat 9 tool bar. Please help.

// pages on which that string is found to a new file.

var pageArray = [];

var stringToSearchFor = "SHEET";

for (var p = 0; p < this.numPages; p++) {

    // iterate over all words

    for (var n = 0; n < this.getPageNumWords(p); n++) {

        if (this.getPageNthWord(p, n) == stringToSearchFor) {

            pageArray.push(p);

            break;

        }

    }

}

if (pageArray.length > 0) {

    // extract all pages that contain the string into a new document

    var d = app.newDoc();   

// this will add a blank page - we need to remove that once we are done

    for (var n = 0; n < pageArray.length; n++) {

        d.insertPages( {

            nPage: d.numPages-1,

            cPath: this.path,

            nStart: pageArray,

            nEnd: pageArray,

        } );

    }

    // remove the first page

    d.deletePages(0);

   var annots=d.getAnnots();

    for (i in annots) annots.destroy();

// Save the new document:

var myDocPath = this.path;

var aPathComps = myDocPath.split("/");

var myFilename = aPathComps[aPathComps.length-2]; // Last element

d.saveAs("/c/QC Tool/Art Pages/"+ myFilename+".xlsx", "com.adobe.acrobat.xlsx");

d.closeDoc(true); 

}

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

You put your code in a function and then call that function using the cExec method of addToolButton, like this:

app.addToolButton({ cName: "MyScript1", cLabel: "Extract SHEET Pages", cTooltext: "Extract SHEET Pages",

    cExec: "extractSheetPages()", cEnable: "event.rc = (event.target != null);"});

  

function extractSheetPages() {

    // put the rest of your code here

}

TOPICS
Acrobat SDK and JavaScript, Windows

Views

796

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
Jan 08, 2018 0
Most Valuable Participant ,
Jan 08, 2018

Copy link to clipboard

Copied

Do you have the Acrobat JavaScript API Reference? Did you read the documentation of the addToolButton method?

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...
Jan 08, 2018 0
Jan 08, 2018

Copy link to clipboard

Copied

Hello Try, i read that document. I understand the concept of app.addtoolbutton(). But i dont know how to link my code with that.

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...
Jan 08, 2018 0
Most Valuable Participant ,
Jan 08, 2018

Copy link to clipboard

Copied

You put your code in a function and then call that function using the cExec method of addToolButton, like this:

app.addToolButton({ cName: "MyScript1", cLabel: "Extract SHEET Pages", cTooltext: "Extract SHEET Pages",

    cExec: "extractSheetPages()", cEnable: "event.rc = (event.target != null);"});

  

function extractSheetPages() {

    // put the rest of your code here

}

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...
Jan 08, 2018 0
Jan 08, 2018

Copy link to clipboard

Copied

Exactly this is the place where i stucked. Adding the function. Thanks Try for spending time for me. Finally I have to save this code in js format and should be saved in user folder, am i right?

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...
Jan 08, 2018 0
Most Valuable Participant ,
Jan 08, 2018

Copy link to clipboard

Copied

No, in the app folder. The user folder is not used 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
Reply
Loading...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

You can save the code in the user or app folder.

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...
Jan 08, 2018 0
Most Valuable Participant ,
Jan 08, 2018

Copy link to clipboard

Copied

There is no user folder. Running this code returns an error message:

app.getPath("user", "javascript");

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...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

You will get the error message when the JavaScript folder doesn't exists.

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...
Jan 08, 2018 0
Most Valuable Participant ,
Jan 08, 2018

Copy link to clipboard

Copied

I think you're wrong. I just tried creating a "Javascripts" folder under the Acrobat user folder (C:\Users\USERNAME\AppData\Roaming\Adobe\Acrobat\11.0\Javascripts\), and it's not executing JS files that I put in it, and getPath still throws an 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
Reply
Loading...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

The correct path is:

\Users\USERNAME\AppData\Roaming\Adobe\Acrobat\Privileged\VERSION\JavaScripts

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...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

Bernd is correct, this error shows up when you don't have a user level folder. Are you sure you are creating it in the correct location? Starting with Acrobat 10, you need to add the "Privileged" folder to the 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
Reply
Loading...
Jan 08, 2018 0
Most Valuable Participant ,
Jan 08, 2018

Copy link to clipboard

Copied

I take it back, you're correct. That worked. However, it seems like a real hassle when one can simply use the pre-existing app folder...

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...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

I am with you on that one. You may want to tell that to Adobe

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...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

They stopped creating the User folder back in Acrobat 10. So it's been quite a while.  One of the problems with the App folder (on Windows) is that it often requires access privileges that the user does not always have. So you've got problems going both ways. I've written an extensive set of instructions on this because of all the issues my customers have experienced. 

Instructions for Installing Folder Level Scripts (Automation Tools) and Plug-ins

I think Adobe made a weak stab at a solution for this problem when they created "Commands".  It would be ideal if there was a similar XML grammar for auto installing folder level scripts. You could just give the file to your user and tell them to drop it on Acrobat for an auto install. But given how things have gone for the past 15 years, I expect that when it comes to automation scripting it will get harder for us, not easier. 

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...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

try67  wrote

I take it back, you're correct. That worked. However, it seems like a real hassle when one can simply use the pre-existing app folder...

This is possible when you can create files in this folder.

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...
Jan 08, 2018 0
Most Valuable Participant ,
Jan 08, 2018

Copy link to clipboard

Copied

On the plus side, installing Actions is a breeze now... Just double-click the sequ file and it gets imported into the application.
I don't think the same can be done with js files because that file-extension is used by many other applications. But it would be great if you could drag&drop a js file into Acrobat and it will install itself, as you've described.

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...
Jan 08, 2018 0
Jan 08, 2018

Copy link to clipboard

Copied

Hello Try, I did exactly what you instructed. I got the Toolbar but the code is not running.

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...
Jan 08, 2018 0
Jan 08, 2018

Copy link to clipboard

Copied

Hello Try, I tried some simple test codes for toolbar button those are all working only my code is not working. Am i missing anything related to folder scripting?

app.addToolButton({

cName: "MyScript1",

cLabel: "Extract SHEET Pages",

cTooltext: "Extract SHEET Pages", 

cExec: "extractSheetPages()",

cEnable: "event.rc = (event.target != null);"}); 

    

function extractSheetPages() { 

    // Iterates over all pages and find a given string and extracts all

// pages on which that string is found to a new file.

var pageArray = [];

var stringToSearchFor = "Sheet";

for (var p = 0; p < this.numPages; p++) {

    // iterate over all words

    for (var n = 0; n < this.getPageNumWords(p); n++) {

        if (this.getPageNthWord(p, n) == stringToSearchFor) {

            pageArray.push(p);

            break;

        }

    }

}

if (pageArray.length > 0) {

    // extract all pages that contain the string into a new document

    var d = app.newDoc();   

// this will add a blank page - we need to remove that once we are done

    for (var n = 0; n < pageArray.length; n++) {

        d.insertPages( {

            nPage: d.numPages-1,

            cPath: this.path,

            nStart: pageArray,

            nEnd: pageArray,

        } );

    }

    // remove the first page

    d.deletePages(0);

    var annots=d.getAnnots();

for (i in annots) annots.destroy();

// Save the new document:

// Acquire path from current document

var myDocPath = this.path;

// break into an array of path components

var aPathComps = myDocPath.split("/");

var myFilename = aPathComps[aPathComps.length-2]; // Last element

d.saveAs("/c/QC Tool/Art Pages/"+ myFilename+".txt", "com.adobe.acrobat.accesstext");

// Close the new document without notifying the user:

d.closeDoc(true);

   

}

}

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...
Jan 08, 2018 0
Adobe Community Professional ,
Jan 08, 2018

Copy link to clipboard

Copied

Any error message in the console?

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...
Jan 08, 2018 0
Jan 08, 2018

Copy link to clipboard

Copied

This is the error i am getting in the console.

SyntaxError: syntax error

1:Console:Exec

undefined

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...
Jan 08, 2018 0
Most Valuable Participant ,
Jan 09, 2018

Copy link to clipboard

Copied

What application did you use to create the js 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
Reply
Loading...
Jan 09, 2018 0
Jan 09, 2018

Copy link to clipboard

Copied

Notepad.

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...
Jan 09, 2018 0
Adobe Community Professional ,
Jan 09, 2018

Copy link to clipboard

Copied

Try following:

app.addToolButton({  

cName: "MyScript1",  

cLabel: "Extract SHEET Pages",  

cTooltext: "Extract SHEET Pages",   

cExec: "extractSheetPages()",  

cEnable: "event.rc = (event.target != null);"});   

      

function extractSheetPages() {   

console.show();

console.println("OK");

}

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...
Jan 09, 2018 0
Jan 09, 2018

Copy link to clipboard

Copied

where i have to include my code?

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...
Jan 09, 2018 0
Adobe Community Professional ,
Jan 09, 2018

Copy link to clipboard

Copied

This is only a test.

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...
Jan 09, 2018 0
Jan 09, 2018

Copy link to clipboard

Copied

still throwing same error buddy.

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...
Jan 09, 2018 0
Most Valuable Participant ,
Jan 09, 2018

Copy link to clipboard

Copied

Can you share the js file you created?

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...
Jan 09, 2018 0
Jan 08, 2018

Copy link to clipboard

Copied

Ok Try, thank you very much.

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...
Jan 08, 2018 0