Skip to main content
Participant
April 8, 2008
Question

JS: How to make a script to print one page at a time and set print job name?

  • April 8, 2008
  • 7 replies
  • 1077 views
What I want is to print one page at a time to the printer.

Document.print(false, app.printerPresets.item(0)) almost get me there. But how do I get it to print page by page? There seems to be no way to set which page to print to printerPreset.

Also, is it possible to change print job name from the default of using document name?
This topic has been closed for replies.

7 replies

Kasyan Servetsky
Legend
April 10, 2008
I think its impossible to do, neither directly in InDesign, nor by scripting. The only parameter we can use in this case is printFile which is valid only when the current printer is defined as postscript file.
Participant
April 10, 2008
Kasyan, I am not printing to a file. I need to print directly to the printer.
Kasyan Servetsky
Legend
April 10, 2008
>Is there any solution to this other than saving the document to a different name before each page print?

Here is an altered and abriged version of the script that I use. I use print preset called "PS to PDF" in it, so you should change it to your own.
//-------------------------------------------
if(app.documents.length == 0){

alert("Please open a document and try again.");
exit();
}
var myFolder = Folder ("~/Desktop");

var myDoc = app.activeDocument;
var myNoExtention = getFileNameOnly (myDoc.name);
var myPrintPrefs = myDoc.printPreferences;

if (app.printerPresets.item("PS to PDF") == null) {
alert("Preset \"PS to PDF\" has not been installed");
exit();
}
else {
myPrintPrefs.activePrinterPreset = app.printerPresets.item("PS to PDF");
myDoc.sections.everyItem().sectionPrefix = "";
myDoc.sections.everyItem().includeSectionPrefix = false;
}
var stop = myDoc.pages.length;

var w = new Window ( 'window', 'Printing PS-files' );
var pb = w.add ('progressbar', [12, 12, 300, 24], 0, stop);
var txt = w.add('statictext');
txt.bounds = [0, 0, 200, 20];
txt.alignment = "left";
w.show()
for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++){
curPage = myCounter + 1;
pb.value = curPage;
var myText = String("Page " + curPage + " of " + myDoc.pages.length);
txt.text = myText;
var myPageName = myDoc.pages.item(myCounter).name;
var cornumber = pagenum(myPageName);
var myFilePath = myFolder + "/" + myNoExtention + "_" + cornumber + ".ps";
var myFile = new File(myFilePath);
myPrintPrefs.pageRange = myPageName;
myPrintPrefs.printFile = myFile;
myDoc.print(false);
}
w.hide();

alert("Done!");

function pagenum(myNumber){
if (myNumber >= 1 && myNumber <= 9)
{
x = "0" + "0" + myNumber;
}
else if (myNumber >= 10 && myNumber <= 99)
{
x = "0" + myNumber;
}
else if (myNumber >= 100 && myNumber <= 999)
{
x = myNumber;
}
return x
}
function getFileNameOnly (myFileName) {
var myString = "";
var myResult = myFileName.lastIndexOf(".");
if (myResult == -1) {
myString = myFileName;
}
else {
myString = myFileName.substr(0, myResult);
}
return myString;
}

//----------------------------------
Kasyan
Kasyan Servetsky
Legend
April 10, 2008
>I didn't know I can just do myDoc.print(false) and that uses document's PreintPreferences.

False here means don't show the dialog box.
Participant
April 9, 2008
Thank you both. I didn't know I can just do myDoc.print(false) and that uses document's PreintPreferences.

The one remain thing is how to set the print job name? InDesign uses the document name as print job name (and Documnet.name is readonly so I can't change it while printing). I want the job name to be "DocumentName" + pageName so that every page print don't end up being the same over at the printer's queue. I don't see any option to set print job name in PrintPreferences. Is there any solution to this other than saving the document to a different name before each page print?
Kasyan Servetsky
Legend
April 9, 2008
Kasyan Servetsky
Legend
April 9, 2008
Hi Richard,

You can do it by setting the pageRange property of the printPreferences object to the name of each page in turn:

var myPrintPrefs = myDoc.printPreferences;


for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++){
var myPageName = myDoc.pages.item(myCounter).name;
var cornumber = pagenum(myPageName);
var myFilePath = myFolder + "/" + "Q_"+ myIssueNumber + "_" + cornumber + ".ps";
var myFile = new File(myFilePath);
myPrintPrefs.pageRange = myPageName;
myPrintPrefs.printFile = myFile;
myDoc.print(false);
}


Kasyan