Skip to main content
edwardc52369671
Participating Frequently
July 15, 2025
Answered

How can I adapt this script to export EPS instead?

  • July 15, 2025
  • 2 replies
  • 217 views

Hi, I'm currently using the below indesign script to export named JPGS of each page. To work the script requires a text box with the filename on each page all linked by a common paragraph style. The script works perfectly to export JPGS, however I've been unable scucessfully adapt it to export EPS. Can anyone help?

 

-

 

if (app.documents.length != 0){
var myDoc = app.activeDocument;
MakeJPEGfile();
} else {
alert("Please open a document and try again.");
}
function myPS() {
try {
return myDoc.selection[0].appliedParagraphStyle;
} catch (e) {
alert("Place cursor to text with paragraph style for filenames");
exit();
}
}
function MakeJPEGfile() {
app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
app.jpegExportPreferences.exportResolution = 300;
app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;
app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myPS();
var f = myDoc.findGrep();
for (var myCounter = 0; myCounter < f.length; myCounter++) {
try {
var curPage = f[myCounter].parentTextFrames[0].parentPage;
if (curPage.appliedSection.name != "") {
curPage.appliedSection.name = "";
}
var objName = f[myCounter].contents.replace(/ /g,"_").toLowerCase();
app.jpegExportPreferences.pageString = curPage.name;
//var myFilePath = "~/C:\output/" + myPageName + ".jpg";
var myFilePath = myDoc.filePath + "/" + objName + ".jpg"; //export to a folder of the current document
var myFile = new File(myFilePath);
myDoc.exportFile(ExportFormat.jpg, myFile, false);
} catch(e) {
//pasteboard?
}
}
}

Correct answer m1b

Hi @edwardc52369671, I fixed it up a bit and added a little bonus function to clean the filename. See how that goes.

- Mark

 

/**
 * @file Export Each Heading Page As EPS.js
 * 
 * Finds text in the selected paragraph style
 * and exports each of those pages as EPS.
 * 
 * @author edwardc52369671 and m1b
 * @version 2025-07-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-can-i-adapt-this-script-to-export-eps-instead/m-p/15415219
 */
function main() {

    if (app.documents.length === 0)
        return alert("Please open a document and try again.");

    var doc = app.activeDocument;
    var text = doc.selection[0];

    if (!text || !text.hasOwnProperty('appliedParagraphStyle'))
        return alert("Place cursor to text with paragraph style for filenames");

    app.findGrepPreferences = null;
    app.findGrepPreferences.appliedParagraphStyle = text.appliedParagraphStyle;

    // find any text in the selected paragraph style
    var found = doc.findGrep();

    // we'll export to a folder "EPS"
    var exportFolder = Folder(doc.filePath + '/EPS');

    if (!exportFolder.exists)
        exportFolder.create();

    var alreadyDone = {};

    // loop over each found text
    for (var i = 0, page; i < found.length; i++) {

        if (!found[i].parentTextFrames[0])
            continue;

        page = found[i].parentTextFrames[0].parentPage;

        if (alreadyDone[page.name])
            continue;

        if (page.appliedSection.name != "") {
            page.appliedSection.name = "";
        }

        // make sure found text is okay for file naming
        var objName = cleanFileName(found[i].contents.replace(/ /g, "_").toLowerCase());

        // set the current export page
        app.epsExportPreferences.pageRange = page.name;

        // do the export
        var myFilePath = exportFolder + "/" + objName + '_' + page.name + ".eps"; //export to a folder of the current document
        doc.exportFile(ExportFormat.epsType, File(myFilePath), false);

        // mark this page as done
        alreadyDone[page.name] = true;

    }

    // reveal the export folder
    exportFolder.execute();

}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Export Pages As EPS');

/**
 * Returns a cleaned file name by removing invalid characters.
 *
 * Illegal Characters:
 *   /   Path separator (Unix, macOS)
 *   \   Path separator (Windows)
 *   :   Reserved on Windows (drive letter separator) and older macOS versions
 *   *   Wildcard
 *   ?   Wildcard
 *   "   Quotation mark, not allowed in Windows filenames
 *   <   Redirect operator, disallowed in Windows
 *   >   Redirect operator, disallowed in Windows
 *   |   Pipe, used for command chaining in shells, not allowed in Windows filenames
 *   `   Backtick, often used in shell commands and can cause confusion, best avoided
 *
 * Also excludes:
 *   \x00-\x1F  Control characters, usually invisible
 *   ^\s*       Leading whitespace
 *   \s*$       Trailing whitespace
 *
 * @author m1b
 * @version 2025-06-30
 * @param dirtyName - the file name to clean.
 * @returns {String?} - the cleaned file name.
 */
function cleanFileName(dirtyName) {

    var cleanName = dirtyName.replace(/[\/\\:\*\?"<>\|\`\x00-\x1F]|^\s*|\s*$/g, '');
    if (cleanName.length > 0)
        return cleanName

};

Edit 2025-07-15: added check so we don't export the same page multiple times.

2 replies

Willi Adelberger
Community Expert
Community Expert
July 15, 2025

Are you aware that EPS is an outdated ancient file type from the past millenium?

Use PDF/X-4 instead.

m1b
Community Expert
m1bCommunity ExpertCorrect answer
Community Expert
July 15, 2025

Hi @edwardc52369671, I fixed it up a bit and added a little bonus function to clean the filename. See how that goes.

- Mark

 

/**
 * @file Export Each Heading Page As EPS.js
 * 
 * Finds text in the selected paragraph style
 * and exports each of those pages as EPS.
 * 
 * @author edwardc52369671 and m1b
 * @version 2025-07-15
 * @discussion https://community.adobe.com/t5/indesign-discussions/how-can-i-adapt-this-script-to-export-eps-instead/m-p/15415219
 */
function main() {

    if (app.documents.length === 0)
        return alert("Please open a document and try again.");

    var doc = app.activeDocument;
    var text = doc.selection[0];

    if (!text || !text.hasOwnProperty('appliedParagraphStyle'))
        return alert("Place cursor to text with paragraph style for filenames");

    app.findGrepPreferences = null;
    app.findGrepPreferences.appliedParagraphStyle = text.appliedParagraphStyle;

    // find any text in the selected paragraph style
    var found = doc.findGrep();

    // we'll export to a folder "EPS"
    var exportFolder = Folder(doc.filePath + '/EPS');

    if (!exportFolder.exists)
        exportFolder.create();

    var alreadyDone = {};

    // loop over each found text
    for (var i = 0, page; i < found.length; i++) {

        if (!found[i].parentTextFrames[0])
            continue;

        page = found[i].parentTextFrames[0].parentPage;

        if (alreadyDone[page.name])
            continue;

        if (page.appliedSection.name != "") {
            page.appliedSection.name = "";
        }

        // make sure found text is okay for file naming
        var objName = cleanFileName(found[i].contents.replace(/ /g, "_").toLowerCase());

        // set the current export page
        app.epsExportPreferences.pageRange = page.name;

        // do the export
        var myFilePath = exportFolder + "/" + objName + '_' + page.name + ".eps"; //export to a folder of the current document
        doc.exportFile(ExportFormat.epsType, File(myFilePath), false);

        // mark this page as done
        alreadyDone[page.name] = true;

    }

    // reveal the export folder
    exportFolder.execute();

}
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Export Pages As EPS');

/**
 * Returns a cleaned file name by removing invalid characters.
 *
 * Illegal Characters:
 *   /   Path separator (Unix, macOS)
 *   \   Path separator (Windows)
 *   :   Reserved on Windows (drive letter separator) and older macOS versions
 *   *   Wildcard
 *   ?   Wildcard
 *   "   Quotation mark, not allowed in Windows filenames
 *   <   Redirect operator, disallowed in Windows
 *   >   Redirect operator, disallowed in Windows
 *   |   Pipe, used for command chaining in shells, not allowed in Windows filenames
 *   `   Backtick, often used in shell commands and can cause confusion, best avoided
 *
 * Also excludes:
 *   \x00-\x1F  Control characters, usually invisible
 *   ^\s*       Leading whitespace
 *   \s*$       Trailing whitespace
 *
 * @author m1b
 * @version 2025-06-30
 * @param dirtyName - the file name to clean.
 * @returns {String?} - the cleaned file name.
 */
function cleanFileName(dirtyName) {

    var cleanName = dirtyName.replace(/[\/\\:\*\?"<>\|\`\x00-\x1F]|^\s*|\s*$/g, '');
    if (cleanName.length > 0)
        return cleanName

};

Edit 2025-07-15: added check so we don't export the same page multiple times.

edwardc52369671
Participating Frequently
July 16, 2025

Thanks so much for your help with this, it works perfectly!!