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

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

New Here ,
Apr 08, 2008 Apr 08, 2008
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?
TOPICS
Scripting
1.0K
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
Valorous Hero ,
Apr 09, 2008 Apr 09, 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
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
Valorous Hero ,
Apr 09, 2008 Apr 09, 2008
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
New Here ,
Apr 09, 2008 Apr 09, 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?
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
Valorous Hero ,
Apr 10, 2008 Apr 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.
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
Valorous Hero ,
Apr 10, 2008 Apr 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
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
New Here ,
Apr 10, 2008 Apr 10, 2008
Kasyan, I am not printing to a file. I need to print directly to the printer.
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
Valorous Hero ,
Apr 10, 2008 Apr 10, 2008
LATEST
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.
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