// DESCRIPTION: batch-convert files // Peter Kahrel // 31 March 2007 #target indesign-4.0 app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll; showFilebox = app.documents.length == 0; startDir = 'Type folder name or * to show folder dialog'; // Get all (five) necessary parameters as an object. // Property names are dir, subdirInclude, sourceType, // destinationType, pdfPreset, ignoreErrors params = getData( startDir, showFilebox ); app.dialogs.everyItem().destroy(); if( params.destinationType == params.sourceType && params.destinationType != '.indd' ) errorM( 'Source and destination types\rmust be different.' ); // we'll collect the names of the files that couldn't be processed problems = []; var alldocs = []; if( app.documents.length > 0 ) { for( i = 0; i < app.documents.length; i++ ) try { alldocs.push( app.documents[i].fullName ) } catch(_) { problems.push( app.documents[i].name + ' has never been saved' ) } } else if( params.subdirInclude == true ) alldocs = getAllfiles( params.dir, [], params.sourceType ); else alldocs = Folder( params.dir ).getFiles( '*' + params.sourceType ); if( params.ignoreErrors ) app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract; for( i = 0; i < alldocs.length; i++ ) { try { app.open( alldocs[i] ); f = new File( alldocs[i].fullName.replace( RegExp( params.sourceType+'$' ), params.destinationType )); switch( params.destinationType ) { case '.pdf' : app.documents.item( decodeURI(alldocs[i].name) ).exportFile( ExportFormat.pdfType, f , false , params.pdfPreset ); break; case '.eps' : app.documents.item( decodeURI(alldocs[i].name) ).exportFile( ExportFormat.epsType, f , false ); break; case '.inx' : app.documents.item( decodeURI(alldocs[i].name) ).exportFile( ExportFormat.indesignInterchange, f ); break; case '.rtf' : longestStory( app.documents.item( decodeURI(alldocs[i].name) )).exportFile( ExportFormat.rtf, f ); break; case '.indd' : app.activeDocument.save( f ); } } catch(_) { problems.push( app.documents.item( decodeURI(alldocs[i].name)).fullName ) } finally { if( showFilebox == true ) app.activeDocument.close(); } } app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll; if( problems.length > 0 ) alert( 'Problems with:\r\r' + problems.join('\r') ) //end--------------------------------------------------------------------- function getData( dirr, showFilebox ) { var pdfPresets = app.pdfExportPresets.everyItem().name; var subfolders, sourceButtons, ignoreErrors, destButtons, dropdown; var dlg = app.dialogs.add( { name : 'File converter' } ); with(dlg) { with( dialogColumns.add() ) { if( showFilebox == true ) { with( borderPanels.add( ) ) { with( dialogColumns.add() ) staticTexts.add( { staticLabel : 'Folder:' } ); with( dialogColumns.add() ) dirr = textEditboxes.add( { editContents : dirr, minWidth : 235 } ); } with( borderPanels.add()) { with( dialogColumns.add() ) subfolders = checkboxControls.add( { staticLabel : 'Include subfolders ', checkedState : false } ); with( dialogColumns.add() ) skipErrors = checkboxControls.add( { staticLabel : 'Ignore errors', checkedState : true, minWidth : 142 } ); } } else { dirr.editContents = ''; subfolders = false; subfolders.checkedState = false; skipErrors = false; skipErrors.checkedState = false; } with( borderPanels.add() ) { staticTexts.add( { staticLabel : 'Source file format: ', minWidth : 126 } ); with( sourceButtons = radiobuttonGroups.add( ) ) { radiobuttonControls.add( { staticLabel : 'InDesign', checkedState : true } ); if( showFilebox == true ) radiobuttonControls.add( { staticLabel : 'Interchange (INX) ' } ); } } with( borderPanels.add() ) { staticTexts.add( {staticLabel : 'Destination file format: ' } ); with( destButtons = radiobuttonGroups.add( ) ) { if( showFilebox == true ) radiobuttonControls.add( { staticLabel : 'InDesign' } ); radiobuttonControls.add( { staticLabel : 'PDF' , checkedState : true } ); radiobuttonControls.add( { staticLabel : 'EPS' } ); radiobuttonControls.add( { staticLabel : 'Interchange (INX)' } ); radiobuttonControls.add( { staticLabel : 'Rich Text Format (RTF)' } ); } } with( borderPanels.add() ) { staticTexts.add( { staticLabel : 'PDF presets:', minWidth : 93 } ); dropDown = dropdowns.add( { stringList : pdfPresets, selectedIndex : 0 }); } } } if( dlg.show() == false ) destroyExit(); //if * pressed get folder from folder dialog //and display script dialog again if( dirr.editContents == '*' ) { dirr = Folder.selectDialog( 'Select a folder', dirr ); if( dirr == null ) destroyExit(); return getData( dirr, showFilebox ); } //return dialog data as an object return { dir : dirr.editContents, subdirInclude : subfolders.checkedState, sourceType : showFilebox == true ? [ '.indd', '.inx' ][sourceButtons.selectedButton] : [ '.indd' ][sourceButtons.selectedButton], destinationType : showFilebox == true ? [ '.indd', '.pdf', '.eps', '.inx', '.rtf' ][destButtons.selectedButton] : [ '.pdf', '.eps', '.inx', '.rtf' ][destButtons.selectedButton], pdfPreset : pdfPresets[dropDown.selectedIndex], ignoreErrors : skipErrors.checkedState } } function getAllfiles( dir, array, mask ) { var f = Folder( dir ).getFiles( '*.*' ); for( var i = 0; i < f.length; i++ ) if( f[i] instanceof Folder ) getAllfiles( f[i], array, mask ); else if( f[i].name.substr( -mask.length ) == mask ) array.push( f[i] ); return array; } //get longest story in document, //assuming it is the central one function longestStory( doc ) { var temp; var longest = 0; for( var i = 0; i < doc.stories.length; i++ ) if( doc.stories[i].contents.length > longest ) { longest = doc.stories[i].contents.length; temp = i; } return doc.stories[temp]; } function destroyExit() { app.dialogs.everyItem().destroy(); exit(); } function errorM( m ) { alert( m ); exit(); }