Skip to main content
Participating Frequently
March 1, 2013
Question

Export selected artboards, PNG24 javaScript?

  • March 1, 2013
  • 1 reply
  • 5633 views

Hi All,

I'm trying to do some workflow enhancements. For our games we need to export to PNG at different resolutions for different mobile devices. I use a artboard per asset and currently manually export 3 times (using file/export/png with use artboards option), at 72 dpi, 144 dpi and 33.75 dpi.

I've been modifying a simple Javascript I downlaoded and have got it save to different fixed locations at different scales. All good. However exporting every artboard each time will cause issues with our version control system and take too long. It's not really a solution.

So I need a way to either only export the current selected artboards (which I believe can't be done) or somewho show the artboard range dialog that the system uses. Either of those would be a good solution.

Can it be done?

I'm using CS5.

Here is where I'm at so far - please be kind, I'm a dabbler and not very experienced!

var docRef = app.activeDocument;

var num_artboards = docRef.artboards.length;

var getName = app.activeDocument.name;

var fileName = getName.slice(0, -3);

for (var i = 0; i < num_artboards; i++ )

{

    var destFile = new File("/Users/malcolmreed/Desktop/tests/double/" + fileName + "_" + docRef.artboards.name + ".png");   

    var options = new ExportOptionsPNG24();

    options.artBoardClipping = true;

    options.matte = false;

    options.horizontalScale = 200.0;

    options.verticalScale = 200.0;

    options.transparency = true;

       

    docRef.artboards.setActiveArtboardIndex(i);

   

    docRef.exportFile (destFile, ExportType.PNG24, options);   

}

for (var i = 0; i < num_artboards; i++ )

{

    var destFile = new File("/Users/malcolmreed/Desktop/tests/standard/" + fileName + "_" + docRef.artboards.name + ".png");   

    var options = new ExportOptionsPNG24();

    options.artBoardClipping = true;

    options.matte = false;

    options.horizontalScale = 100.0;

    options.verticalScale = 100.0;

    options.transparency = true;

       

    docRef.artboards.setActiveArtboardIndex(i);

   

    docRef.exportFile (destFile, ExportType.PNG24, options);

}

This topic has been closed for replies.

1 reply

Inspiring
March 1, 2013

Yeah you are using a little more code than you actully need here… AI will export the active artboard… You are looping all artboards at the moment but you could just create a simple dialog to get your range start & end… or use csv. Heres how I would do this ( untested ) you will need to change the last path and scale… You will see I use the loop only once and only change the properties I need for the options object…

#target illustrator

exportPNGs();

function exportPNGs() {

          if ( parseFloat( app.version ) < 15 ) { return; }

          if ( app.documents.length == 0 ) { return; }

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

          var i, doc, fileName, opts, expFile;

          doc = app.activeDocument;

          fileName = doc.name.match( /(.*)\.[^\.]+$/ )[1];

          opts = new ExportOptionsPNG24();

          opts.artBoardClipping = true;

          opts.matte = false;

          opts.horizontalScale = opts.verticalScale = 100;

          opts.transparency = true;

          for ( i = 0; i < doc.artboards.length; i++ ) {

                    doc.artboards.setActiveArtboardIndex( i );

                    expFile = File( Folder.desktop + '/tests/standard/' + fileName + '_' + doc.artboards.name + '.png' );   

                    doc.exportFile ( expFile, ExportType.PNG24, opts );

                    opts.horizontalScale = opts.verticalScale = 200;

                    expFile = File( Folder.desktop + '/tests/double/' + fileName + '_' + doc.artboards.name + '.png' );   

                    doc.exportFile ( expFile, ExportType.PNG24, opts );

                    opts.horizontalScale = opts.verticalScale = 300;

                    expFile = File( Folder.desktop + '/tests/someother/' + fileName + '_' + doc.artboards.name + '.png' );   

                    doc.exportFile ( expFile, ExportType.PNG24, opts );

          };

          app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

};

Mal_ReedAuthor
Participating Frequently
March 4, 2013

Hi Mark thanks for the quick response and for tidying up my newbie code!

I can understand most of what you have done by reading it, but must admit the way you get the fileName looks like Voodoo to me!

So from what I have now, it seems to enable exporting a region of the artboards, say artboards 2 and 3 I'd need:

  • a dialog box to type the numbers in.
  • new variables to return the numbers from the dialog box, put into the for loop.  startRange = 1; endRange = 3;  for ( i = startRange; i < endRange; i++ ) {

I'll go off and have a look at creating dialog boxes. Again, I'm new to this. If there are some good tutorials you know of, that would be very handy.

Thanks again, you guys here are amazing.

Jongware
Community Expert
Community Expert
March 4, 2013

Nothing like a smart, fancy dialog but for a quick solution, you could (ab)use the standard Prompt:

listEntry = prompt ("Artboard numbers", "1");

list = listEntry.match (/\d+/g);

alert (list.length);

The 'listEntry' text gets split up in numbers with the GREP match command. This returns the value as an array of all consecutive numbers. You can enter separate artboard numbers with anything you like: "1 2 3" or "1,2,3" or even "1a2b3c" -- anything else than a digit will work.