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);
}
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
}
Copy link to clipboard
Copied
Do you have the Acrobat JavaScript API Reference? Did you read the documentation of the addToolButton method?
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.
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
}
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?
Copy link to clipboard
Copied
No, in the app folder. The user folder is not used anymore.
Copy link to clipboard
Copied
You can save the code in the user or app folder.
Copy link to clipboard
Copied
There is no user folder. Running this code returns an error message:
app.getPath("user", "javascript");
Copy link to clipboard
Copied
You will get the error message when the JavaScript folder doesn't exists.
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.
Copy link to clipboard
Copied
The correct path is:
\Users\USERNAME\AppData\Roaming\Adobe\Acrobat\Privileged\VERSION\JavaScripts
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...
Copy link to clipboard
Copied
I am with you on that one. You may want to tell that to Adobe
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.
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.
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);
}
}
Copy link to clipboard
Copied
Any error message in the console?
Copy link to clipboard
Copied
This is the error i am getting in the console.
SyntaxError: syntax error
1:Console:Exec
undefined
Copy link to clipboard
Copied
What application did you use to create the js file?
Copy link to clipboard
Copied
Notepad.
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");
}
Copy link to clipboard
Copied
where i have to include my code?
Copy link to clipboard
Copied
This is only a test.
Copy link to clipboard
Copied
still throwing same error buddy.
Copy link to clipboard
Copied
Can you share the js file you created?