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

Open a doc from a document level script

jdawson422
Registered: Feb 18 2008
Posts: 12
Answered

I am trying to create a document level script that when it is opened, I want it to open another document.

In the first document (ScriptTest.pdf) the following is in the document script area:

//<Document-Level>
//<ACRO_source>OpenEvent</ACRO_source>
//<ACRO_script>
/*********** belongs to: Document-Level:OpenEvent ***********/
app.alert({
	nIcon: 2,
	nType: 0,
	cMsg: "You just opened this document.",
	cTitle: "OPEN EVENT"
});
 
var myDocument = myTrustedOpen();
 
function OpenEvent()
{
}
//</ACRO_script>
//</Document-Level>

In a trusted folder
(C:\Documents and Settings\jdawson\Application Data\Adobe\Acrobat\8.0
\JavaScripts)

The file config.js contains the following code:

app.alert({
	nIcon: 2,
	nType: 0,
	cMsg: "Application Started.",
	cTitle: "APP START"
});
 
myTrustedOpen = app.trustedFunction(
	function()
	{
		app.beginPriv();
			app.alert({
				nIcon: 2,
				nType: 0,
				cMsg: "This is a trusted function.",
				cTitle: "TRUSTED FUNCTION"
			});
			var myDoc = app.openDoc({
				cPath: "/C/Documents and Settings/jdawson/My Documents/Test/smalldocument.pdf",
				bHidden: false
			});
		app.endPriv();
		return myDoc;
	}
);

When ScriptTest.pdf is opened, three alerts are displayed titled:

1. "APP START"
2. "TRUSTED FUNCTION"
3. "OPEN EVENT"

Then an error message as follows:

Quote:

NotAllowedError: Security settings prevent access to this property or method.
App.openDoc:20:Document-Level:OpenEvent

When I close the script editor with ScriptTest.pdf already open, it seems to work fine.

I thought this should work. If someone can help me get this correct I would be very grateful.

Sincerely,

Jim

My Product Information:
Acrobat Pro 8.1, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You're going a bit overboard here. The "app.openDoc()" function is not privileged. It can be run directly from a document event script. It just can't be run while Acrobat is loading the document, which is what is happening with your code. The "NotAllowedError:" message isn't because of privilege, it's because it simply isn't allowed.

The solution is to place the "openDoc" code in the page "Open" event for the first page in the PDF. The page Open event happens after the document has been initialized. Make sure you add a state variable to the open action so the open doesn't happen a second time. For example:

if(this.bAlreadyOpened == null)
{
app.openDoc("/C/Documents and Settings/jdawson/My Documents/Test/smalldocument.pdf");
this.bAlreadyOpened = true;
}

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

jdawson422
Registered: Feb 18 2008
Posts: 12
Well, I got the opening of the document working. I removed everything from the config.js file and added exactly what you provided to the page level script area.

Now, saving it to a new file is giving me problems. And this is where I am using the trustedFunction method.

In the config.js file I have:

myTrustedSaveAs = app.trustedFunction(function(adoc){app.beginPriv();var myTrustedRetn = adoc.saveAs({cpath: "/C/Documents and Settings/jdawson/My Documents/Test/doc_ver8.pdf",bPromptToOverwrite: False});app.endPriv();return myTrustedRetn;});

and in the ScriptTest.pdf I have:

//-------------------------------------------------------------//-----------------Do not edit the XML tags--------------------//------------------------------------------------------------- //<Document-Level>//<ACRO_source>OpenEvent</ACRO_source>//<ACRO_script>/*********** belongs to: Document-Level:OpenEvent ***********/app.alert({nIcon: 2,nType: 0,cMsg: "You just opened this document.",cTitle: "OPEN EVENT"}); function OpenEvent(){}//</ACRO_script>//</Document-Level> //<Page-Actions>//<ACRO_source>Page1:Page Open:Action1</ACRO_source>//<ACRO_script>/*********** belongs to: Page-Actions:Page1:Page Open:Action1 ***********/if(this.bAlreadyOpened == null){var myDoc = app.openDoc("/C/Documents and Settings/jdawson/My Documents/Test/smalldocument.pdf");var newDoc = myTrustedSaveAs(myDoc);this.bAlreadyOpened = true;} //</ACRO_script>//</Page-Actions>

When I open ScriptTest.pdf I get the alert (sort of a debugging aid for now) and then this error message comes up:

Quote:
Acrobat JavaScript Debugger Functions Version 7.0
Acrobat Database Connectivity Built-in Functions Version 8.0
Acrobat EScript Built-in Functions Version 8.0
Acrobat Annotations / Collaboration Built-in Functions Version 8.0
Acrobat Annotations / Collaboration Built-in Wizard Functions Version 8.0
Acrobat Multimedia Version 8.0
Acrobat SOAP 8.0

adoc has no properties
5:Folder-Level:User:config.js
adoc has no properties
5:Folder-Level:User:config.js
The smalldocument.pdf does open, by the way.

Thanks again for the generous help.
jdawson422
Registered: Feb 18 2008
Posts: 12
I copied my response to a new thread under a new topic so that others can see my new problem. Sorry for making this thread longer than necessary.

Thanks to thomp for his answer.

Respectfully,
Jim
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
I can't find your new posts so I'll give you the answer here.

You can only see a document from another document if the document is disclosed. This stated in the entry for the "openDoc" fucntion in the Acrobat Reference.

Open doc returns null unless this code is in a document script in the PDF that's opened.

this.disclosed = true;

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