These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

getting documentFileName from folder level javascript

Bianca
Registered: May 26 2009
Posts: 66
Answered

hi at all,

i'm trying to get the documentFileName attribute from a folder level javascript, cause i can't add document javascript to each createt pdf. i know that i should be able to access that attribute by this.documentFileName, but i always get "undefined". i also tried to iterate through active docs, but acrobat just gets an empty array (though there is one opened).

could anyone tell me how to get that attribute?

thanks a lot,

bianca

try67
Expert
Registered: Oct 30 2008
Posts: 2398
Post your code, please.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
tried things like:

var temp = "";
var d = app.activeDocs;
for (var i = 0; i < d.length; i++){
d[i].bringToFront();
app.aler(this.documentFileName);
}
app.alert(temp);

or called this.documentFileName directly. the alert is used to test the result, but i just get nothing or undefined. i also tried dic.documentFileName but there is no alert window. maybe it works althoug there is nothing shown.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You wrote app.aler instead of app.alert inside the loop.
Also, try using this instead (I've noticed in the past that it solves this issue by converting the documentFileName property to a string):
app.alert(this.documentFileName+"");

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
yeah, just tried to restore the code i used.. i'm actually not getting anything, not even a simple alert with a hard coded string...

but as i said - i don't really need these alerts - the code i need to be running correctly is:

var fileName = doc.documentFileName;

var objectID = fileName.split("_")[0];

while(objectID.indexOf("-") != -1) {
objectID = objectID.replace("-", ".");
}

var session = fileName.split("_")[1];

just hard to test if i get no output.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Make sure you have "Show console on errors and warnings" checked in the JavaScript preferences.
As I said, for some reason, documentFileName doesn't return a string. You need to make it into one, for example, by doing so:
var fileName = doc.documentFileName+"";

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

try67
Expert
Registered: Oct 30 2008
Posts: 2398
PS, this piece of code is not correct:

var objectID = fileName.split("_")[0];

while(objectID.indexOf("-") != -1) {
objectID = objectID.replace("-", ".");
}


You can't use indexOf or replace on an Array of Strings. You need to access one of its elements.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
ok, i activated that function and opened the debugger. that message appears in the console:

invalid variable initialization
1:Folder-Level:App:getSession.js

the js is my js to be loaded, so this ok, but what does the first line mean?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
What's the first line in it say?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
var fileName = doc.documentFileName+"";

maybe before i added quotation marks.. doesn't show up on restart. so it seems to work. how could i test without alerts?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Go to the console and then type fileName and run it. You should see the value of it.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
fileName is not defined
1:Console:Exec
undefined

fail...

found something:
typing "this" i get "[object Doc]"
but typing "doc" i get:
doc is not defined
1:Console:Exec
undefined

why is doc not defined?? doc should access the same as this - the current document.
setting fileName = this.documentFileName in the folder level java script doesn't change anything. it's still undefined.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Replace doc with this

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
tried - see above. edited my entry while you were typing yours.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Which version of Acrobat are you using, anyway?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
adobe acrobat professional 8.0, but the script has to work with adobe acrobat standard 6.0 - i could downgrade if it makes a difference.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
What do you get when you run this from the console?

console.println('"The file name of this document is '+ this.documentFileName +'."');

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
"The file name of this document is 221-29871-13871-25346_A21EFFE3A0CBBE84DD3C7B4CB79711DD.pdf."

true

i used to try that, but it doesn't help if i couldn't use that with a folder level js. i have no chance to edit each pdf.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Wait a minute. I understand now what happens.
A folder-level script is launched when you open Acrobat. At that moment there are no open files, so you can't access the documentFileName property. You need to launch the script AFTER you open a file, for example throuhg a menu item or a button.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
oh my god - you saved my day! of corse, there is no document available... thank you so much!!!
try67
Expert
Registered: Oct 30 2008
Posts: 2398
So stupid of me... Should have realized what was going on from the beginning...

Anyway, you should be aware that the documentFileName is only available from version 6, so tell your users not to use it on any earlier version than that.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
acrobat 6 is installed on all clients so this shouldn't be a problem.

by the way: is there kind of an onDocumentOpen-event available from folder level js?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
No. It can only be run from a document-embedded script.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
ok, but adding a menu bar item and running the script is possible from folder level js?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Adding the menu item and attaching the script to it, yes.
Running it needs to be done by the user (or an embedded script).

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
yeah, of corse - should be possible :)

so finally thanks for your help!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
Yes, add in so doing you can control if the menu item or toolbar button is only active while a document is opened.

You can see samples at PDFScripting.com.

For a tutorial about adding a menu item, see Add a Custom Menu Item to Acrobat for a tutorial.

To have the menu item or toolbar button active only when a document is open, useTo have the menu item or toolbar button active only when a document is open, use
cEnable: "event.rc = (app.doc != null)",
for the enable option. This code uses a logical test to see if there is an application document object or there is none (null).

George Kaiser

try67
Expert
Registered: Oct 30 2008
Posts: 2398
I've recently noticed that in version 9 the disabled menu items are not visible at all when no file is open.
In all previous versions they were always visible, but greyed out until a file was opened.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
See [url=http://www.acrobatusers.com/tutorials/2007/01/custom_menu_commands]Adding Custom Menu Commands[/url] for a tutorial for this. You can control the 'enable/disable' of the added menu item by setting the 'cEnable' with the flollowing code:
cEnable: 'event.rc = (event.target != null);'
or
cEnable: "event.rc = (app.doc != null)",
both snippets of code test to see if there is an open object.

George Kaiser

Bianca
Registered: May 26 2009
Posts: 66
thanks gkaiseril, tutorial is working! now i can start customizing it.
Bianca
Registered: May 26 2009
Posts: 66
adding a button is not possible, but creating a menu item works, so this is ok. i also get the file name with help of my new item. i will test buttons when i'm back with acrobat 6.0, maybe it will work with that version.

thanks a lot - you really helped me!
Bianca
Registered: May 26 2009
Posts: 66
i had to notice that adding a menu item is not enough - the function has to be used from browser plugin of adobe acrobat, so this is just not working. try, you said, that adding buttons without an opened document is not available in acrobat 9.0, but i also can't do this with 8.0 - am i doing something wrong or may it be the same issue with 8.0?
try67
Expert
Registered: Oct 30 2008
Posts: 2398
What is exactly the problem? You're not seeing the menu item at all?
I seem to remember that in a browser you need enable a special menu, somthing like "User Menu", in order to see the items you've added.

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Bianca
Registered: May 26 2009
Posts: 66
i don't see an added button at all - not even in regular adobe acrobat window. adding menuitem works, but of course not in browser.

i will take a look for that menu..

my fault - it is working in browser plugin, but not in regular window, that's ok. thx! :)
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
It should work in both. You may want to look at [url=http://www.pdfscripting.com]PDFScripting.com's[/url] free [url=http://www.pdfscripting.com/public/65.cfm]Free Acrobat Automation Tools[/url] for working examples.

After you add the JS file to the folder, you need to restart Acrobat/Reader for the code to be activated by Acorbat/Reader.

George Kaiser

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Just as a heads up for the topics discussed on this thread:

First, here are two articles that contain info relavant to the information discussed.

http://www.acrobatusers.com/tutorials/2006/folder_level_scripts
http://www.acrobatusers.com/tutorials/2007/10/apply_security_with_js

Next, the app.activeDocs property is privileged. When it is accessed from a non-privileged context it only contains the current PDF. To get all the open docs requires a trusted function. In fact, since this is for a folder level script it a good idea to use a trusted function anyway, even if you won't need app.activeDocs, to avoid warning messages on the alert boxes.

And finally, the JavaScript replace funtion takes a regular expression as input so it can replace all occurances of a pattern in one go. No loops are necessary.

Try this in a folder level script
var getNReplace = app.trustedFunction(function(){app.beginPriv(); // All Open Documentsvar cSessionDocs = [];for(var i=0;i<app.activeDocs.length;i++)cSessionDocs.push(app.activeDocs[i].documentFileName.split("_").shift().replace(/-/g,".")); app.alert("OpenDocs\n" + cSessionDocs.join("\n")); // For current DoccSessionDocs = this.documentFileName.split("_").shift().replace(/-/g,"."); app.alert("Current Doc\n" + cSessionDocs); app.endPriv();});app.addMenuItem({cName:"Show Files", cParent:"File",cExec:"getNReplace();"});

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.pdfscripting.com/public/34.cfm#JSIntro][b]The Console Window (Video tutorial)[/b][/url]
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window(article)[/b][/url]

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

Bianca
Registered: May 26 2009
Posts: 66
ok, it works! i have to reset the toolbar after starting acrobat, because the button is not displayed (no open document as you mentioned before..).. this is not that great for users, so i will decide, if i use buttons or a menu item, which is displayed without resetting. the problem is: how do i display menu items in browser plugin?