Adding "activate some layers" to ExportDocsAsPNG24.jsx

New Here ,
Jun 16, 2015 Jun 16, 2015

Copy link to clipboard

Copied

I'de like to add this code

function test(){ 

 

    function tryLockLayer(name){ 

        try{ 

            lrs.getByName(name).locked = false; 

        } catch(e) { 

            return; 

        } 

    }; 

    

    var doc=app.activeDocument; 

    var lrs = doc.layers; 

    tryLockLayer("Watermark"); 

    tryLockLayer("All"); 

    tryLockLayer("Fill"); 

    tryLockLayer("No fill"); 

    tryLockLayer("Background"); 

    

 

 

test();

function test1(){ 

 

    function tryLockLayer(name){ 

        try{ 

            lrs.getByName(name).visible = false; 

        } catch(e) { 

            return; 

        } 

    }; 

    

    var doc=app.activeDocument; 

    var lrs = doc.layers; 

    tryLockLayer("Watermark"); 

    tryLockLayer("All"); 

    tryLockLayer("Fill"); 

    tryLockLayer("No fill"); 

    tryLockLayer("Background"); 

    

 

 

test1();

function test2(){ 

 

    function tryLockLayer(name){ 

        try{ 

            lrs.getByName(name).visible = true; 

        } catch(e) { 

            return; 

        } 

    }; 

    

    var doc=app.activeDocument; 

    var lrs = doc.layers; 

    tryLockLayer("Watermark"); 

    tryLockLayer("Fill");

    tryLockLayer("Background"); 

    

 

 

test2();

            

for (i=0; i<app.documents.length; i++) 

    { 

        var idoc = app.documents

        //alert("+"+idoc.path+"+"); 

        if (idoc.path == "") 

            { 

                file = File.saveDialog ("Save file..."); 

                idoc.saveAs (file); 

            } 

        else 

            { 

                //alert(idoc.name); 

                if (!idoc.saved) 

                    {  

                        idoc.save(); 

                        idoc.saved = true; 

                    } 

            } 

    } 

in this script

/**********************************************************

ADOBE SYSTEMS INCORPORATED

Copyright 2005 Adobe Systems Incorporated

All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and

distribute this file in accordance with the terms

of the Adobe license agreement accompanying it. 

If you have received this file from a source

other than Adobe, then your use, modification,

or distribution of it requires the prior

written permission of Adobe.

*********************************************************/

/**********************************************************

ExportDocsAsPNG24.jsx

DESCRIPTION

This sample gets files specified by the user from the

selected folder and batch processes them and saves them

as PDFs in the user desired destination with the same

file name.

**********************************************************/

// Main Code [Execution of script begins here]

// uncomment to suppress Illustrator warning dialogs

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts;

// Select the source folder.

sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PNG', '~' );

// If a valid folder is selected

if ( sourceFolder != null )

{

    files = new Array();

    fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' );

   

    // Get all files matching the pattern

    files = sourceFolder.getFiles( fileType );

   

    if ( files.length > 0 )

    {

        // Get the destination to save the files

        destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PNG files.', '~' );

        for ( i = 0; i < files.length; i++ )

        {

            sourceDoc = app.open(files); // 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( 'Files are saved as PNG in ' + destFolder );

    }

    else

    {

        alert( 'No matching files found' );

    }

}

/*********************************************************

getNewName: Function to get the new file name. The primary

name is the same as the source file.

**********************************************************/

function getNewName()

{

    var ext, docName, newName, saveInFile, docName;

    docName = sourceDoc.name;

    ext = '.png'; // new extension for png file

    newName = "";

       

    for ( var i = 0 ; docName != "." ; i++ )

    {

        newName += docName;

    }

    newName += ext; // full png name of the file

   

    // Create a file object to save the png

    saveInFile = new File( destFolder + '/' + newName );

    return saveInFile;

}

/*********************************************************

getPNGOptions: Function to set the PNG saving options of the

files using the PDFSaveOptions object.

**********************************************************/

function getPNGOptions()

{

   

    // Create the PDFSaveOptions object to set the PDF options

    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 = 100.0;

    //pngExportOpts.matte = true;

    //pngExportOpts.matteColor = 0, 0, 0;

    pngExportOpts.saveAsHTML = false;

    pngExportOpts.transparency = true;

    //pngExportOpts.verticalScale = 100.0;

    return pngExportOpts;

}

I need to activate some layers before export the PNG file.

Thanks

Ivan

TOPICS
Scripting

Views

433

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
community guidelines

correct answers 1 Correct Answer

Guide , Jun 24, 2015 Jun 24, 2015
It can be condensed more, and is more efficient as there are less processing steps.here is improved codehave a look over it and compare it to the other one I posted, and maybe yours as well.it should help you understand a bit more about scripting/programing. /********************************************************** ADOBE SYSTEMS INCORPORATED  Copyright 2005 Adobe Systems Incorporated  All Rights Reserved NOTICE:  Adobe permits you to use, modify, and  distribute this file in accordance ...

Likes

Translate

Translate
Guide ,
Jun 19, 2015 Jun 19, 2015

Copy link to clipboard

Copied

to make this work now, are you just running your script first then running the "ExportDocsAsPNG24.jsx"?

if so, you may find that putting your code at the start of the other script will work fine.

would be worth checking for any conflicting variables and changing any you do find.

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
community guidelines
New Here ,
Jun 19, 2015 Jun 19, 2015

Copy link to clipboard

Copied

Yes, I'm running my script first and after the other.

The problem is that the first script works only if I first open all the files, and than I save manually every one file. After that I can run the second script that ask me a folder where the files are located.

I'd like the approach of the second script so I'd like to insert the first script in the other.

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
community guidelines
Guide ,
Jun 23, 2015 Jun 23, 2015

Copy link to clipboard

Copied

the top script is a bit of a mess.

do you need it to save the files? I dropped that last section of code.

also rearranged it to reduce the amount of code and renamed functions to make it clear what they do.

a quick test makes me think it's working correctly

not sure why you hide all layers only to unhide some again, but left it doing that

/**********************************************************

ADOBE SYSTEMS INCORPORATED 

Copyright 2005 Adobe Systems Incorporated 

All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and 

distribute this file in accordance with the terms

of the Adobe license agreement accompanying it.  

If you have received this file from a source 

other than Adobe, then your use, modification,

or distribution of it requires the prior 

written permission of Adobe.

*********************************************************/ 

 

/**********************************************************

ExportDocsAsPNG24.jsx

DESCRIPTION

This sample gets files specified by the user from the 

selected folder and batch processes them and saves them 

as PDFs in the user desired destination with the same 

file name.

**********************************************************/ 

 

// Main Code [Execution of script begins here] 

 

// uncomment to suppress Illustrator warning dialogs 

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 

 

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts, lrs; 

 

// Select the source folder. 

sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PNG', '~' ); 

 

// If a valid folder is selected 

if ( sourceFolder != null ) 

    files = new Array(); 

    fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' ); 

     

    // Get all files matching the pattern 

    files = sourceFolder.getFiles( fileType ); 

     

    if ( files.length > 0 ) 

    { 

        // Get the destination to save the files 

        destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PNG files.', '~' ); 

        for ( i = 0; i < files.length; i++ ) 

        { 

            sourceDoc = app.open(files); // returns the document object 

            

//---------------------------------------------------------------------------------------------------------------          

//

//              ADDED Script to do to each file before exporting

//

//---------------------------------------------------------------------------------------------------------------       

             lrs = sourceDoc.layers;

             UnLockLayer("Watermark");

             UnLockLayer("All");

             UnLockLayer("Fill");

             UnLockLayer("No fill");

             UnLockLayer("Background");

             HideLayer("Watermark");

             HideLayer("All");

             HideLayer("Fill");

             HideLayer("No fill");

             HideLayer("Background");

             UnHideLayer("Watermark");

             UnHideLayer("Fill");

             UnHideLayer("Background");

            

//---------------------------------------------------------------------------------------------------------------               

            

            // 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( 'Files are saved as PNG in ' + destFolder ); 

    } 

    else 

    { 

        alert( 'No matching files found' ); 

    } 

 

 

/*********************************************************

getNewName: Function to get the new file name. The primary

name is the same as the source file.

**********************************************************/ 

 

function getNewName() 

    var ext, docName, newName, saveInFile, docName; 

    docName = sourceDoc.name; 

    ext = '.png'; // new extension for png file 

    newName = ""; 

         

    for ( var i = 0 ; docName != "." ; i++ ) 

    { 

        newName += docName

    } 

    newName += ext; // full png name of the file 

     

    // Create a file object to save the png 

    saveInFile = new File( destFolder + '/' + newName ); 

 

    return saveInFile; 

 

 

 

 

 

/*********************************************************

getPNGOptions: Function to set the PNG saving options of the 

files using the PDFSaveOptions object.

**********************************************************/ 

 

function getPNGOptions() 

     

    // Create the PDFSaveOptions object to set the PDF options 

    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 = 100.0; 

    //pngExportOpts.matte = true; 

    //pngExportOpts.matteColor = 0, 0, 0; 

    pngExportOpts.saveAsHTML = false; 

    pngExportOpts.transparency = true; 

    //pngExportOpts.verticalScale = 100.0; 

 

    return pngExportOpts; 

//----------------------------------------------------------------------------------

//          Your Functions

//----------------------------------------------------------------------------------

function UnLockLayer(name){   

        try{   

            lrs.getByName(name).locked = false;   

        }catch(e){   

            return;   

        }   

function HideLayer(name){   

        try{   

            lrs.getByName(name).visible = false;   

        }catch(e){   

            return;   

        }   

}

function UnHideLayer(name){   

        try{   

            lrs.getByName(name).visible = true;    

        }catch(e){   

            return;   

        }   

}

   

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
community guidelines
New Here ,
Jun 23, 2015 Jun 23, 2015

Copy link to clipboard

Copied

I've copied the first script from the forum and modified a bit. It's a bit of a mess because I don't know scripting

Your script works great.

I want to hide all layers because I don't know which layers are active in the documents. If there is another way to say I want only these layers visible it's the same for me.

And there is no need to save the files now that the first script is into the other!

Thank you very much!

Ivan

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
community guidelines
Guide ,
Jun 24, 2015 Jun 24, 2015

Copy link to clipboard

Copied

It can be condensed more, and is more efficient as there are less processing steps.

here is improved code

have a look over it and compare it to the other one I posted, and maybe yours as well.

it should help you understand a bit more about scripting/programing.

/**********************************************************

ADOBE SYSTEMS INCORPORATED 

Copyright 2005 Adobe Systems Incorporated 

All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and 

distribute this file in accordance with the terms

of the Adobe license agreement accompanying it.  

If you have received this file from a source 

other than Adobe, then your use, modification,

or distribution of it requires the prior 

written permission of Adobe.

*********************************************************/ 

 

/**********************************************************

ExportDocsAsPNG24.jsx

DESCRIPTION

This sample gets files specified by the user from the 

selected folder and batch processes them and saves them 

as PDFs in the user desired destination with the same 

file name.

**********************************************************/ 

 

// Main Code [Execution of script begins here] 

 

// uncomment to suppress Illustrator warning dialogs 

// app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS; 

 

var destFolder, sourceFolder, files, fileType, sourceDoc, targetFile, pngExportOpts, lrs; 

 

// Select the source folder. 

sourceFolder = Folder.selectDialog( 'Select the folder with Illustrator files you want to convert to PNG', '~' ); 

 

// If a valid folder is selected 

if ( sourceFolder != null ) 

    files = new Array(); 

    fileType = prompt( 'Select type of Illustrator files to you want to process. Eg: *.ai', ' ' ); 

     

    // Get all files matching the pattern 

    files = sourceFolder.getFiles( fileType ); 

     

    if ( files.length > 0 ) 

    { 

        // Get the destination to save the files 

        destFolder = Folder.selectDialog( 'Select the folder where you want to save the converted PNG files.', '~' ); 

        for ( i = 0; i < files.length; i++ ) 

        { 

            sourceDoc = app.open(files); // returns the document object 

            

//---------------------------------------------------------------------------------------------------------------          

//

//              ADDED Script to do to each file before exporting

//

//---------------------------------------------------------------------------------------------------------------       

             lrs = sourceDoc.layers;

             // the ShowLayer function unlocks layer by name

             // and sets to visible or hidden via a true or false statment in argument 2

             ShowLayer("Watermark",true);

             ShowLayer("All",false);

             ShowLayer("Fill",true);

             ShowLayer("No fill",false);

             ShowLayer("Background",true);

            

//---------------------------------------------------------------------------------------------------------------               

            

            // 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( 'Files are saved as PNG in ' + destFolder ); 

    } 

    else 

    { 

        alert( 'No matching files found' ); 

    } 

 

 

/*********************************************************

getNewName: Function to get the new file name. The primary

name is the same as the source file.

**********************************************************/ 

 

function getNewName() 

    var ext, docName, newName, saveInFile, docName; 

    docName = sourceDoc.name; 

    ext = '.png'; // new extension for png file 

    newName = ""; 

         

    for ( var i = 0 ; docName != "." ; i++ ) 

    { 

        newName += docName

    } 

    newName += ext; // full png name of the file 

     

    // Create a file object to save the png 

    saveInFile = new File( destFolder + '/' + newName ); 

 

    return saveInFile; 

 

 

 

 

 

/*********************************************************

getPNGOptions: Function to set the PNG saving options of the 

files using the PDFSaveOptions object.

**********************************************************/ 

 

function getPNGOptions() 

     

    // Create the PDFSaveOptions object to set the PDF options 

    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 = 100.0; 

    //pngExportOpts.matte = true; 

    //pngExportOpts.matteColor = 0, 0, 0; 

    pngExportOpts.saveAsHTML = false; 

    pngExportOpts.transparency = true; 

    //pngExportOpts.verticalScale = 100.0; 

 

    return pngExportOpts; 

//----------------------------------------------------------------------------------

//          Your Function

//----------------------------------------------------------------------------------

function ShowLayer(name,vis){   

        try{   

            lrs.getByName(name).visible = vis;

            lrs.getByName(name).locked = false;

        }catch(e){   

            return;   

        }   

}

   

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
community guidelines
New Here ,
Jun 24, 2015 Jun 24, 2015

Copy link to clipboard

Copied

Ok, I see the difference.

Thank you!

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
community guidelines
Guide ,
Jun 24, 2015 Jun 24, 2015

Copy link to clipboard

Copied

Glad I could help‌.

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
community guidelines
New Here ,
May 26, 2021 May 26, 2021

Copy link to clipboard

Copied

LATEST

While exporting the ai file how to change the height. Please refer the screenshot

Screen Shot 2021-05-27 at 6.49.18 AM.png

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
community guidelines