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

Exporting all open docs to jpg - script hanging

Contributor ,
May 11, 2022 May 11, 2022

Copy link to clipboard

Copied

Hi, all,

I appreciate I'm asking for help with someone else's script but I've been trying for hours to get it to loop through all open docs without having the input dialog pop up each time. I feel I'm nearly there with it but it exports the one file then hangs (InDesign is not responsive).

If it's easy, please would you kindly guide me and put me out of my misery. Thank you in advance if you're able to help.

 

// adapted to save to original indd's folder in a sub-folder called JPG


//DESCRIPTION:Export all pages to JPG format with the desired dimensions
// Export to JPG - all pages.jsx
//
// Modified 2016-04-26
// Keith Gilbert, Gilbert Consulting
// www.gilbertconsulting.com
// blog.gilbertconsulting.com

// Assumes any document bleed is the same on all 4 sides of the page
// All pages in a spread are the same height




// Prompt the user for JPG export values
function myInput() {
	var myWindow = new Window("dialog", "Export to JPG - all pages");
		myWindow.preferredSize = [372,418];
		myWindow.alignChildren = "right";
		var mySizePanel = myWindow.add("panel", undefined, "Enter the desired export size in pixels");
			mySizePanel.alignChildren = "left";
			mySizePanel.margins = 20;
			mySizePanel.preferredSize = [340,75];
			var mySizeGroup = mySizePanel.add("group", undefined);			
				mySizeGroup.add("statictext", undefined, "Width:");
				var myWidthField = mySizeGroup.add("edittext", undefined, ""); // width
					myWidthField.characters = 6;
					myWidthField.active = true;
				mySizeGroup.add("statictext", undefined, "\u00A0\u00A0\u00A0OR\u00A0\u00A0\u00A0");
				mySizeGroup.add("statictext", undefined, "Height:");
				var myHeightField = mySizeGroup.add("edittext", undefined, ""); // height
					myHeightField.characters = 6;					
		var myExportPanel = myWindow.add("panel", undefined, "Export");
			myExportPanel.alignChildren = "left";
			myExportPanel.margins = 20;
			myExportPanel.preferredSize = [340,75];
			var mySpreadsGroup = myExportPanel.add("group", undefined);
				mySpreadsGroup.alignChildren = "left";
				mySpreadsGroup.orientation = "column";
				mySpreadsGroup.add("radiobutton",undefined,"Pages"); // pages
				mySpreadsGroup.add("radiobutton",undefined,"Spreads"); // spreads
				mySpreadsGroup.children[0].value = true;
		var myImagePanel = myWindow.add("panel", undefined, "Image");
			myImagePanel.orientation = "column";
			myImagePanel.alignChildren = "right";
			myImagePanel.margins = 20;
			myImagePanel.preferredSize = [340,106];
			var myQualityGroup = myImagePanel.add("group", undefined);
				myQualityGroup.orientation = "row";
				myQualityGroup.add("statictext", undefined, "Quality:");
				var myQualityField = myQualityGroup.add("dropdownlist", undefined, ["Maximum", "High", "Medium", "Low"]); // quality
					myQualityField.selection = 2;
					myQualityField.preferredSize = [200,21];
			var myFormatMethodGroup = myImagePanel.add("group", undefined);
				myFormatMethodGroup.orientation = "row";
				myFormatMethodGroup.add("statictext", undefined, "Format Method:");
				var myFormatMethodField = myFormatMethodGroup.add("dropdownlist", undefined, ["Progressive", "Baseline"]); // format
					myFormatMethodField.selection = 1;
					myFormatMethodField.preferredSize = [200,21];
			var myColorSpaceGroup = myImagePanel.add("group", undefined);
				myColorSpaceGroup.orientation = "row";
				myColorSpaceGroup.add("statictext", undefined, "Color Space:");
				var myColorSpaceField = myColorSpaceGroup.add("dropdownlist", undefined, ["RGB", "CMYK", "Gray"]); // color space
					myColorSpaceField.selection = 0;
					myColorSpaceField.preferredSize = [200,21];
		var myOptionsPanel = myWindow.add("panel", undefined, "Options");
			myOptionsPanel.orientation = "column";
			myOptionsPanel.alignChildren = "left";
			myOptionsPanel.margins = 20;
			myOptionsPanel.preferredSize = [340,127];
			var myColorProfileField = myOptionsPanel.add("checkbox", undefined, "\u00A0Embed Color Profile"); // color profile
				myColorProfileField.value = false;
			var myAliasField = myOptionsPanel.add("checkbox", undefined, "\u00A0Anti-alias"); // anti-alias
				myAliasField.value = true;
			var myBleedField = myOptionsPanel.add("checkbox", undefined, "\u00A0Use Document Bleed Settings"); // bleed
			var myOverprintField = myOptionsPanel.add("checkbox", undefined, "\u00A0Simulate Overprint"); // overprint
		var myButtonGroup = myWindow.add ("group");
			var myCancelBtn = myButtonGroup.add ("button", undefined, "Cancel");
			var myExportBtn = myButtonGroup.add ("button", undefined, "Export", {name:"ok"});
		myExportBtn.onClick = function() { // User clicked the OK button
			if (((myWidthField.text == "") && (myHeightField.text == "")) ||
				((myWidthField.text != "") && (myHeightField.text != ""))) {
				alert("Please enter an export width OR height in pixels"); // User left both width and height blank, or entered both a width and height
			}
			else {
				if (isNaN(myWidthField.text) || isNaN(myHeightField.text)) {
					alert ("Only numbers are allowed in the Width and Height fields"); // User entered a non-number in the width or height fields
				}
				else {
					exit(); // This onClick function
				}
			}
		}
	if (myWindow.show() == 1) { // User didn't click the cancel button
		return [myWidthField.text, myHeightField.text,
			mySpreadsGroup.children[0].value, mySpreadsGroup.children[1].value,
			myQualityField.selection.text, myFormatMethodField.selection.text, myColorSpaceField.selection.text, 
			myColorProfileField.value, myAliasField.value, myBleedField.value, myOverprintField.value];
	}
	else {
		return; // This dialog function
	}
}



			var myResultsArray = new Array;
			var myResultsArray = myInput();
			if (myResultsArray) { // User did not click Cancel
				// Gather the settings that are common to all exports
				var myWidth = myResultsArray[0];
				var myHeight = myResultsArray[1];
				switch(myResultsArray[4]) { // Quality
					case "Maximum":
						app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MAXIMUM;
					break;
					case "High":
						app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.HIGH;
					break;
					case "Medium":
						app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.MEDIUM;
					break;
					case "Low":
						app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.LOW;
					break;
					default:
					break;
				}
				switch(myResultsArray[5]) { // Format method
					case "Progressive":
						app.jpegExportPreferences.jpegRenderingStyle = JPEGOptionsFormat.PROGRESSIVE_ENCODING;
					break;
					case "Baseline":
						app.jpegExportPreferences.jpegRenderingStyle = JPEGOptionsFormat.BASELINE_ENCODING;
					break;
					default:
					break;
				}
				switch(myResultsArray[6]) { // Colorspace
					case "RGB":
						app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.RGB;
					break;
					case "CMYK":
						app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.CMYK;
					break;
					case "Gray":
						app.jpegExportPreferences.jpegColorSpace = JpegColorSpaceEnum.GRAY;
					break;
					default:
					break;
				}







	while (app.documents.length) {

Main();
    }

function Main() {
	// Check to see whether any InDesign documents are open.
	// If no documents are open, display an error message.
		var myDoc = app.activeDocument;
		// Set the measurement system to points and the origin to page
		userHoriz = myDoc.viewPreferences.horizontalMeasurementUnits;
		userVert = myDoc.viewPreferences.verticalMeasurementUnits;
		userOrigin = myDoc.viewPreferences.rulerOrigin;
		myDoc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;
		myDoc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;
		myDoc.viewPreferences.rulerOrigin = RulerOrigin.PAGE_ORIGIN;
		myDoc.zeroPoint = [0,0];

// build the JPEG name, removing indd
    var myFileName = myDoc.name.replace('.indd', '');
        
// path to the folder of the indd-file
    var myPath = myDoc.filePath;

				app.jpegExportPreferences.embedColorProfile = myResultsArray[7]; // Color profile
				app.jpegExportPreferences.antiAlias = myResultsArray[8]; // Anti alias
				app.jpegExportPreferences.simulateOverprint = myResultsArray[10]; // Simulate overprint
				var myStartPosition = myDoc.pages.firstItem().documentOffset;
				var myEndPosition = myDoc.pages.lastItem().documentOffset;
				if (myResultsArray[2]) { // Export Pages
					app.jpegExportPreferences.exportingSpread = false;
					for (var myCounter = myStartPosition; myCounter <= myEndPosition; myCounter++) {
						var myPage = myDoc.pages.item(myCounter);
						var myPageName = myDoc.pages.item(myCounter).name;
						if (myResultsArray[9]) { // Export bleeds
							app.jpegExportPreferences.useDocumentBleeds = true;
							var myBleedAmount = myDoc.documentPreferences.documentBleedTopOffset;
						}
						else { // Don't export bleeds
							app.jpegExportPreferences.useDocumentBleeds = false;
							var myBleedAmount = 0;
						}
						// Calculate the dimensions of the current page (each page of the document might be a different size)
						var myCurrentWidth = myPage.bounds[3]-myPage.bounds[1]+(2*myBleedAmount);
						var myCurrentHeight = myPage.bounds[2]-myPage.bounds[0]+(2*myBleedAmount);
						if (myWidth > 0) {
							// Calculate the scale percentage
							var myResizePercentage = myWidth/myCurrentWidth;
							var myExportRes = myResizePercentage * 72;	
						}
						else {
							// Calculate the scale percentage
							var myResizePercentage = myHeight/myCurrentHeight;
							var myExportRes = myResizePercentage * 72;
						}
						app.jpegExportPreferences.exportResolution = myExportRes;
						app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
						app.jpegExportPreferences.pageString = myPageName;
						//The name of the exported files will be the base name + the 
						//page name + ".jpg". If the page name contains a colon (as it will 
						//if the document contains sections), then remove the colon.
						var myRegExp = /:/gi;
						myPageName = myPageName.replace(myRegExp, "_");
						// Pad the page number with leading zeros
						if (myPageName < 100) {
							myPageName = "0"+myPageName;
						}
						if (myPageName < 10) {
							myPageName = "0"+myPageName;
						}
						myDoc.exportFile(ExportFormat.JPG, File(myPath + "/JPG/" + myFileName+"_"+myPageName+".jpg"), false);
					}
				}
				else { // Export Spreads
					app.jpegExportPreferences.exportingSpread = true;
					for (var myCounter = myStartPosition; myCounter <= myEndPosition; myCounter++) {
						var myPage = myDoc.pages.item(myCounter);
						var myPageName = myDoc.pages.item(myCounter).name;
						var mySpread = myPage.parent;
						if (mySpread != myOldSpread) {
							if (myResultsArray[11]) { // Export bleeds
								app.jpegExportPreferences.useDocumentBleeds = true;
								var myBleedAmount = myDoc.documentPreferences.documentBleedTopOffset;
							}
							else { // Don't export bleeds
								app.jpegExportPreferences.useDocumentBleeds = false;
								var myBleedAmount = 0;
							}
							// Calculate the dimensions of the current spread (each page of the document might be a different width, and spreads may be more than 2 pages)
							var myCurrentWidth = 0;
							var myCurrentHeight = mySpread.pages.lastItem().bounds[2]-mySpread.pages.firstItem().bounds[0]+(2*myBleedAmount);
							for (j = 0; j < mySpread.pages.length; j++) {
								myCurrentWidth = myCurrentWidth + (mySpread.pages[j].bounds[3]-mySpread.pages[j].bounds[1]);	
							}
							myCurrentWidth = myCurrentWidth + (2*myBleedAmount);
							if (myWidth > 0) {
								// Calculate the scale percentage
								var myResizePercentage = myWidth/myCurrentWidth;
								var myExportRes = myResizePercentage * 72;	
							}
							else {
								// Calculate the scale percentage
								var myResizePercentage = myHeight/myCurrentHeight;
								var myExportRes = myResizePercentage * 72;
							}
							app.jpegExportPreferences.exportResolution = myExportRes;
							app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
							app.jpegExportPreferences.pageString = myPageName;
							//The name of the exported files will be the base name + the 
							//page name + ".jpg". If the page name contains a colon (as it will 
							//if the document contains sections), then remove the colon.
							var myRegExp = /:/gi;
							myPageName = myPageName.replace(myRegExp, "_");
							// Pad the page number with leading zeros
							if (myPageName < 100) {
								myPageName = "0"+myPageName;
							}
							if (myPageName < 10) {
								myPageName = "0"+myPageName;
							}
							myDoc.exportFile(ExportFormat.JPG, File(myPath + "/JPG/" + myFileName+"_"+myPageName+".jpg"), false);
							var myOldSpread = mySpread;
						}
					}
				}
			}
		}
		// Return the measurement system and origin to the way it was
		var myDoc = app.activeDocument;
		myDoc.viewPreferences.horizontalMeasurementUnits = userHoriz;
		myDoc.viewPreferences.verticalMeasurementUnits = userVert;
		myDoc.viewPreferences.rulerOrigin = userOrigin;
	

    // … and close the document
    myDoc.close(SaveOptions.NO);


 

TOPICS
Import and export , Scripting

Views

259

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , May 12, 2022 May 12, 2022

I know you say you don't need it anymore, but this is what you're after with your while loop around Main() I think. 

var nds = app.documents.length - 1;
while (nds--) {
app.activeDocument = app.documents[nds];
Main();
}

Votes

Translate

Translate
Community Expert ,
May 11, 2022 May 11, 2022

Copy link to clipboard

Copied

I don't know anything about scripting but I wonder why you don't contact directly Keith Gilbert which is the author of this script. He is a great InDesign expert.

Votes

Translate

Translate

Report

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
Community Expert ,
May 11, 2022 May 11, 2022

Copy link to clipboard

Copied

Hi JustR,

also see into Peter Kahrel's script Batch Process:

https://creativepro.com/files/kahrel/indesign/batch_convert.html

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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
Contributor ,
May 12, 2022 May 12, 2022

Copy link to clipboard

Copied

As luck would have it, I don't need to make the script now. However, if I ever need to in the future I think I'd strip out all the preferences needed and completely circumvent the need for the user input window.

 

I'd discounted Peter Kahrel's script as it always asks for a path and I couldn't open it in BBEdit to amend it. I wonder if it's possible to use a custom script in the "Run a Script:" bit so that it automatically saves to the doc's parent folder. I'll have to look into that at a later date.

Votes

Translate

Translate

Report

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
Community Expert ,
May 12, 2022 May 12, 2022

Copy link to clipboard

Copied

"I'd discounted Peter Kahrel's script as it always asks for a path and I couldn't open it in BBEdit to amend it."

 

Hi JustyR,

if you downloaded a recent version of Peter's script you can see that the code is editable.

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

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
Community Expert ,
May 12, 2022 May 12, 2022

Copy link to clipboard

Copied

I know you say you don't need it anymore, but this is what you're after with your while loop around Main() I think. 

var nds = app.documents.length - 1;
while (nds--) {
app.activeDocument = app.documents[nds];
Main();
}

Votes

Translate

Translate

Report

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
Contributor ,
May 13, 2022 May 13, 2022

Copy link to clipboard

Copied

LATEST

Thanks for trying to help, brianp311. I gave it a quick test and it doesn't hang anymore, but it still only exports the one document.

Votes

Translate

Translate

Report

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