Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JS: CS4 print active document

Explorer ,
Mar 22, 2009 Mar 22, 2009
Hello

Scripting guide gives this line as an example to print the active document

app.activeDocument.print();

I also want to print using a print preset. The guide indicates this text:

To print a document using a printer preset, include the printer preset in the print command.

If my preset name is: Myoutput

How would I add the preset name? Would it be:

app.activeDocument.print(Myoutput);

or

app.activeDocument.print();
print = Myoutput

Trying to run this single line script is also giving me an error

app.activeDocument.print();

JavaScript error!
Error Number 8
Error String: "Syntax error"
Line: 1
Source "{\rtf1\mac\ansicpg1000\cocartf824
\cocoasubrtf410"
Offending Text: "\"

(Serious Newbie alert)
TOPICS
Scripting
3.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 22, 2009 Mar 22, 2009
John,

First problem is that you are saving your script as rtf so it has no hope of working.

Look in the Adobe Utilities folder and fire up ExtendScript Toolkit CS4. Use File/New to make a new script document. Copy and paste your script from TextEdit (I assume that's what you're using) to your script document.

Now go to the Help menu and fire up the Object Model Viewer. Under Browser, choose the InDesign CS4 object model. Under Classes, choose Document. Under properties and methods, choose the method print(). That shows you the full syntax of the print method.

It shows you this:

Document.print (printDialog: Boolean , using:any)
Adobe InDesign CS4 (6.0) Object Model Object Model
Prints the Document(s).
printDialog: Data Type: Boolean
Whether to invoke the print dialog (Optional)
using: Data Type: any
Printer preset to use. Can accept: PrinterPresetTypes enumerator or PrinterPreset. (Optional)

This might be a bit hard to understand for the uninitiated. The call takes two arguments. The first is a boolean (true or false) indicating whether to show the dialog. The second indicates which PrinterPreset to use.

Since you want to use a specific one for which you know the name, you must first get a reference to it. PrinterPresets also are objects. Look under Classes again for PrinterPreset (singular). And now look under Properties and Methods again, this time look for the parent property. Notice that the parent is the application object.

This means that to get a reference to the preset you want you need to write:

myPreset = app.printerPresets.item("Myoutput");

and now you can write:

app.activeDocument.print(false, myPreset);

Hope this helps not just with the immediate problem but also with how to go about solving these kinds of issues.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 22, 2009 Mar 22, 2009
Yes, it is a big help to the "uninitiated". :)

I am having another problem

My preset is named

ps

myPreset = app.printerPresets.item("ps");
app.activeDocument.print(false, myPreset);

If I turn it to true, it does launch the print window. If false, no print is created. When set to true and the print window opens, the preset is not set to "ps"

However, I have CS2 at home and CS4 out work. I will spend more time tomorrow trying to figure out my failing.

You have been a big help. Thank you!
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2009 Mar 23, 2009
John,

It might be necessary to also set the page range value of the printPreferences. It's a long time since I did any printing by script.

I suspect that turning the first argument to true causes the second argument to be ignored.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 23, 2009 Mar 23, 2009
Hey Dave, (and lurkers)

I got to admit to missing the obvious. The scripting guide also mentioned to check out the sample script "PrintDocumentWithPreset.jsx"

However, when run, it creates a new window, adds a small frame and stops. I named my preset to "ps". (it is printing to postscript)

Is this the line, var myDocument = app.documents.add();, that is creating the new document. Should it not be app.activeDocument? I will be trying to get my mind around this.

(note: just because you responded once does not mean I expect you to keep responding. I value everyone's time :))

//PrintDocumentWithPreset.jsx
//An InDesign CS4 JavaScript
//
//Prints the active document using the specified printer preset.
//Assumes you have a printer preset named "myPrintPreset" and that a document is open.

main();

function main(){
mySetup();
mySnippet();
myTeardown();
}

//
function mySetup(){
var myDocument = app.documents.add();
myDocument.pages.item(0).rectangles.add();
try{
app.printerPresets.add({name:"ps"});
}
catch(myError){}
}
//


//
function mySnippet(){
//
//Prints the active document using the specified printer preset.
var myDocument = app.documents.item(0);
myDocument.print(false, app.printerPresets.item("ps"));
//

}
//


//
function myTeardown(){
}
//
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2009 Mar 23, 2009
You should delete the call to mySetup. myTeardown does nothing so you could delete that too.

That leaves you with the call to mySnippet which does exactly the same as the code you wrote because for the purposes of this script, app.activeDocument is the same as app.documents.item(0) which is the same as app.documents[0].

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2009 Mar 23, 2009
Oh wait! If you're using a printer preset that "prints" to a file, you have to set up the file reference as well. It's the printFile property of app.printPreferences.

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 23, 2009 Mar 23, 2009
Thank you Dave.

Since an open beer does not travel through the mail too well, I'll drink in it your honor. (actually, it will be a martini)

Of course I will be back. But, I'm will try figuring it out at my next stuck point.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 23, 2009 Mar 23, 2009
btw, I'm already stuck but I'm not going to ask yet.

I may have a few holes in the wall while I bang my head against it.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2009 Mar 23, 2009
LATEST
To find out about Files and Folders, check the Core JavaScript object model and also read the JavaScript Tools Guide (under the ESTK Help menu).

Dave
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines