Highlighted

Spaces in filename replaced by dash

Participant ,
Apr 08, 2020

Copy link to clipboard

Copied

Hello,

Strange phenomenon in following script: when I run the script and the filename has spaces in the filename (e.g. this is a test.ai), then the png file is named as follows: this-is-a-test.png.

 

Can anybody help my to avoid this transformation in the jsx?

 

#target illustrator
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts;

// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with all files that have to be converted to a PNG', '~' );

// If the folder is selected, then ....
if ( sourceFolder != null )
{
    files = new Array();
    fileType = prompt( 'Which type of files would you like to convert to PNG? Example how to input: *.ai', ' ' );
   
    // check if all files have the right extension like described above.
    files = sourceFolder.getFiles( fileType );
   
    if ( files.length > 0 )
    {
        // select the destination folder where the png files have to be saved
        destFolder = Folder.selectDialog( 'Select the folder where the converted PNG files have to be saved.', '~' );
        for ( i = 0; i < files.length; i++ )
        {
            sourceDoc = app.open(files[i]); // returns the document object
                                   
            // Call function getNewName to get the name and file to save the pdf
            targetFile = getNewName();
           
            // Call function getPNGOptions get the PNGExportOptions for the files
            pngExportOpts = getPNGOptions();
           
            // Export as PNG
            sourceDoc.exportFile( targetFile, ExportType.PNG24, pngExportOpts );
           
            sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
        }
        alert( 'The converted PNG files are saved in following folder: ' + destFolder );
    }
    else
    {
        alert( 'No files founded!' );
    }
}

/*********************************************************
getNewName: Function to get the new file name. The primary
name is the same as the source file.
Functie om de nieuwe filenaam te genereren. De oorspronkelijke naam wordt gebruikt (zonder extensie)
**********************************************************/
function getNewName()
{
    var ext, docName, newName, saveInFile, docName;
    docName = sourceDoc.name;
    ext = '.png'; // new extension for PNG
    newName = "";
       
    for ( var i = 0 ; docName[i] != "." ; i++ )
    {
        newName += docName[i];
    }
    newName += ext; // full name of the new file including the png extension
   
    // create the new file with the corrected extension
    saveInFile = new File( destFolder + '/' + newName );

    return saveInFile;
}

/*********************************************************
getPNGOptions: Function to set the PNG saving options of the
files using the PDFSaveOptions object.
Hier worden de PNG opties beschreven (= als je een opslaan als selecteert, welke opties je aanvinkt en uitvinkt.
**********************************************************/
function getPNGOptions()
{
   
    // Hier worden de PNG opties beschreven (= als je een opslaan als selecteert, welke opties je aanvinkt en uitvinkt.
    var pngExportOpts = new ExportOptionsPNG24();
   
    // Setting PNGExportOptions properties. Please see the JavaScript Reference
    // for a description of these properties.
    // Add more properties here if you like
    pngExportOpts.antiAliasing = true;
    pngExportOpts.artBoardClipping = true;
    pngExportOpts.horizontalScale = 200;
    //pngExportOpts.matte = true;
    //pngExportOpts.matteColor = 0, 0, 0;
    pngExportOpts.saveAsHTML = false;
    pngExportOpts.transparency = true;
    pngExportOpts.verticalScale = 200;

    return pngExportOpts;
}

 

Topics

Scripting

Views

57

Likes

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

Spaces in filename replaced by dash

Participant ,
Apr 08, 2020

Copy link to clipboard

Copied

Hello,

Strange phenomenon in following script: when I run the script and the filename has spaces in the filename (e.g. this is a test.ai), then the png file is named as follows: this-is-a-test.png.

 

Can anybody help my to avoid this transformation in the jsx?

 

#target illustrator
var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts;

// Select the source folder.
sourceFolder = Folder.selectDialog( 'Select the folder with all files that have to be converted to a PNG', '~' );

// If the folder is selected, then ....
if ( sourceFolder != null )
{
    files = new Array();
    fileType = prompt( 'Which type of files would you like to convert to PNG? Example how to input: *.ai', ' ' );
   
    // check if all files have the right extension like described above.
    files = sourceFolder.getFiles( fileType );
   
    if ( files.length > 0 )
    {
        // select the destination folder where the png files have to be saved
        destFolder = Folder.selectDialog( 'Select the folder where the converted PNG files have to be saved.', '~' );
        for ( i = 0; i < files.length; i++ )
        {
            sourceDoc = app.open(files[i]); // returns the document object
                                   
            // Call function getNewName to get the name and file to save the pdf
            targetFile = getNewName();
           
            // Call function getPNGOptions get the PNGExportOptions for the files
            pngExportOpts = getPNGOptions();
           
            // Export as PNG
            sourceDoc.exportFile( targetFile, ExportType.PNG24, pngExportOpts );
           
            sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
        }
        alert( 'The converted PNG files are saved in following folder: ' + destFolder );
    }
    else
    {
        alert( 'No files founded!' );
    }
}

/*********************************************************
getNewName: Function to get the new file name. The primary
name is the same as the source file.
Functie om de nieuwe filenaam te genereren. De oorspronkelijke naam wordt gebruikt (zonder extensie)
**********************************************************/
function getNewName()
{
    var ext, docName, newName, saveInFile, docName;
    docName = sourceDoc.name;
    ext = '.png'; // new extension for PNG
    newName = "";
       
    for ( var i = 0 ; docName[i] != "." ; i++ )
    {
        newName += docName[i];
    }
    newName += ext; // full name of the new file including the png extension
   
    // create the new file with the corrected extension
    saveInFile = new File( destFolder + '/' + newName );

    return saveInFile;
}

/*********************************************************
getPNGOptions: Function to set the PNG saving options of the
files using the PDFSaveOptions object.
Hier worden de PNG opties beschreven (= als je een opslaan als selecteert, welke opties je aanvinkt en uitvinkt.
**********************************************************/
function getPNGOptions()
{
   
    // Hier worden de PNG opties beschreven (= als je een opslaan als selecteert, welke opties je aanvinkt en uitvinkt.
    var pngExportOpts = new ExportOptionsPNG24();
   
    // Setting PNGExportOptions properties. Please see the JavaScript Reference
    // for a description of these properties.
    // Add more properties here if you like
    pngExportOpts.antiAliasing = true;
    pngExportOpts.artBoardClipping = true;
    pngExportOpts.horizontalScale = 200;
    //pngExportOpts.matte = true;
    //pngExportOpts.matteColor = 0, 0, 0;
    pngExportOpts.saveAsHTML = false;
    pngExportOpts.transparency = true;
    pngExportOpts.verticalScale = 200;

    return pngExportOpts;
}

 

Topics

Scripting

Views

58

Likes

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
Apr 08, 2020 0
Most Valuable Participant ,
Apr 08, 2020

Copy link to clipboard

Copied

There is nothing in the code that would cause the name to be refactored, so whatever is happening is specific to the PNG saver or a combination of circumstances such as operating system and storage location. Since you haven't offered any info on the actual naming conventions and system we can't really tell you more than that. If the save location is a server or NAS, you may need to fiddle with compatibility options in their configuration panels. Same for external drives that may use specific formatting options. in any case, without more specific info this isn't going anywhere.

 

Mylenium

Likes

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
Reply
Loading...
Apr 08, 2020 0
Participant ,
Apr 08, 2020

Copy link to clipboard

Copied

I'm working with Windows 10 and the folder is located on the harddisk of the laptop.

Likes

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
Reply
Loading...
Apr 08, 2020 0