Search code examples
javascriptvectorphotoshop-script

Photoshop Scripting: How to export PathItem by its name


I am desperately trying to figure out how to write a js for photoshop, that exports only a specific path and not all of the contained paths under the "paths" tab in my PS Document to an Illustrator File. For example in the picture here:

image of my paths tab

I would like to export only the path with the Name : "2 Acryl" by using a script. I already have a working script that exports all of the paths into one AI File. I just can't figure out how to reference a single path by its name and export it.

function unSaved() {
try {
    activeDocument.path;
    /* Finish Unsaved Document Error Check - Part A: Try */

    /* Main Code Start */

    /* Based on the following topic thread:
    https://community.adobe.com/t5/photoshop/exporting-all-paths-to-illustrator-error/m-p/8796143 */

    var doc = app.activeDocument;
    var docPath = doc.path;
    var docName = doc.name.replace(/\.[^\.]+$/, '');
    var newFile = File(docPath + '/' + docName + '_Paths' + '.ai');
    var expOptions = new ExportOptionsIllustrator;
    expOptions.path = IllustratorPathType.ALLPATHS;
    doc.exportDocument(newFile, ExportType.ILLUSTRATORPATHS, expOptions);
    // alert('All Photoshop paths have been exported as a single Illustrator file in:' + '\r' + docPath);

    /* Main Code Finish */

    /* Start Unsaved Document Error Check - Part B: Catch */
} catch (err) {
    alert('An image must be both open and/or saved before running this script!')
}

}


Solution

  • This can be done by altering the ExportOptionsIllustrator object according your needs.

     var expOptions = new ExportOptionsIllustrator;
    

    To export a single path, the ExportOptionsIllustrator.path property must be set to

     expOptions.path = IllustratorPathType.NAMEDPATH;
    

    After that you can pick your desired path by it's name using:

    expOptions.pathName = '2 Acryl';
    

    Edit:

    Apparently due to a bug in Photoshop itself the export option IllustratorPathType.NAMEDPATH is simply ignored. Photoshop will always export all paths no matter what.

    Here's a hacky workaround though. Photoshop script offers the PathItems object which contains a list of all the paths in the document. So the idea is this:

    1. create a clone of the current document
    2. iterate over all the paths inside the clone
    3. remove all EXCEPT for the single path you want to keep
    4. export the clone's path as an Illustrator file
    5. close the clone without saving

    Here's the updated script:

    try {
        var doc = app.activeDocument;
        var docPath = doc.path;
        var docName = doc.name.replace(/\.[^\.]+$/, '');
        var newFile = File(docPath + '/' + docName + '_Paths' + '.ai');
        var expOptions = new ExportOptionsIllustrator;
        
        var duplicate = app.activeDocument.duplicate('duplicate');
        
        for(a = duplicate.pathItems.length-1; a>=0; a--)
        {
            if(duplicate.pathItems[a].name != '2 Acryl')
            {
                duplicate.pathItems[a].remove();
            }
        }
        
        duplicate.exportDocument(newFile, ExportType.ILLUSTRATORPATHS, expOptions);
        duplicate.close(SaveOptions.DONOTSAVECHANGES);
        
    } catch (err) {
        alert('An image must be both open and/or saved before running this script!')
    }
    

    Edit2:

    If this still fails, there's a last resort. Photoshop has a built-in function to export paths to Illustrator. Simply go to:

    File * Export * Paths -> Illustrator...

    and choose your desired path in the popup dialog.