• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Export selected artboards, PNG24 javaScript?

Community Beginner ,
Mar 01, 2013 Mar 01, 2013

Copy link to clipboard

Copied

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);

}

TOPICS
Scripting

Views

3.9K

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
Adobe
Guru ,
Mar 01, 2013 Mar 01, 2013

Copy link to clipboard

Copied

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;

};

Votes

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
Community Beginner ,
Mar 04, 2013 Mar 04, 2013

Copy link to clipboard

Copied

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.

Votes

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
Community Expert ,
Mar 04, 2013 Mar 04, 2013

Copy link to clipboard

Copied

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.

Votes

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
Community Beginner ,
Mar 04, 2013 Mar 04, 2013

Copy link to clipboard

Copied

Thanks [Jongware], I haven't tried this yet (will have to wait till later) but I'm wondering if you'd need to list all boards - What if the required range was 10 to 75. Would you need to type in all the numbers between 10 and 75?

Votes

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
Community Expert ,
Mar 04, 2013 Mar 04, 2013

Copy link to clipboard

Copied

Mal_Reed wrote:

..  I'm wondering if you'd need to list all boards - What if the required range was 10 to 75. Would you need to type in all the numbers between 10 and 75?

Um. Yeah, with my simple solution you would!

You can use the Prompt to enter ranges, but then you're on your own in "parsing" the proper ranges from the input. With my simple number-grabbing, an entry such as this

1, 2-5, 7

would return precisely those numbers -- remember, the separators between the numbers don't "mean" anything! To attach 'meaning', you need to verify the entire input string (and so you also must be prepared to return an alert "Bad input"). With this syntax, for example, you could use some code like this:

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

// remove (optional) spaces

listEntry = listEntry.replace(/, /g,',');

// split on comma's

list = listEntry.split(',');

// check each entry

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

{

  // only numbers?

  if (list.match(/^\d+$/))

   continue;

  // numbers separated by hyphen?

  if (list.match(/^\d+-\d+$/))

  {

    // Peter Kahrel's Expand Number Range!

   list = list.replace(/(\d+)-(\d+)/g, function(full,arg1,arg2)

     {

       var j, result='';

       for (j=Number(arg1); j<Number(arg2); j++)

         result += String(j)+',';

        return result+Number(arg2);

     }

   );

   continue;

  }

  // no, issue an alert

  alert ('Bad entry: "'+list+'"');

}

// rebuild entire list

list = list.join(',');

// .. and split again in separate numbers

list = list.split(',');

alert (list.join('\r'));

.. as you can see, it's a bit longer than one (= me!) initially expected it to be ... hopefully, you can follow the logic here.

Using a fancy ScriptUI dialog, by the way, would not make this any easier, as there is no standard "range" edit control. So you would still need this same logic.

Votes

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
Community Expert ,
Mar 05, 2013 Mar 05, 2013

Copy link to clipboard

Copied

Same as above, but re-ordered for a more efficient operation. It also kicks bad entries out of the list, so you can use what's left right away.

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

// remove (optional) spaces

listEntry = listEntry.replace(/, /g,',');

// expand number ranges, per Peter Kahrel's suggestion:

listEntry = listEntry.replace(/(\d+)-(\d+)/g, function(full,arg1,arg2)

{

   var j, result=arg1;

   for (j=Number(arg1)+1; j<=Number(arg2); j++)

           result += ','+String(j);

          return result;

}

);

// split on comma's

list = listEntry.split(',');

// check each entry

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

{

  // only numbers?

  if (!list.match(/^\d+$/))

  {

            // no, issue an alert

            alert ('Bad entry: "'+list+'"');

            list.splice(i,1);

            i--;

  }

}

alert (list.join('\r'));

Votes

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
Community Beginner ,
Mar 06, 2013 Mar 06, 2013

Copy link to clipboard

Copied

Thanks again for the help.

As I'm only interested in the start range number and the end range number, I'm extracting the first two numbers from the list to pass into the export function. I've also done a little work around to enable listing a single number to export just one artboard. It's all working well and will save time, remove some user error and most importantly keep me sane!

I've clearly got a lot to learn as I don't understand everything here, but I'm keen to keep learning scripting and improve the workflow of the apps I use daily.

#target illustrator

exportPNGs();

// CREATE the export function and for loop.

function exportPNGs() {

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

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

          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

          var i, doc, fileName, opts, expFile, startRange, endRange;

          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;

         

          //=== CREATE THE PROMPT WINDOW====

          listEntry = prompt ("Artboard range @1:1 scale of 7.2ppi", "0")

          // remove (optional) spaces

          listEntry = listEntry.replace(/, /g,',');

          // expand number ranges, per Peter Kahrel's suggestion:

          listEntry = listEntry.replace(/(\d+)-(\d+)/g, function(full,arg1,arg2)

          {

              var j, result=arg1;

              for (j=Number(arg1)+1; j<=Number(arg2); j++)

              result += ','+String(j);

              return result;

          }

          );

          // split on comma's

          list = listEntry.split(',');

          // check each entry

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

          {

              // only numbers?

              if (!list.match(/^\d+$/))

              {

                  // no, issue an alert

                  alert ('Bad entry: "'+list+'"');

                  list.splice(i,1);

                  i--;

               }

          }

          //===END OF PROMPT WINDOW====

         

          if (list[1] >= 2)

          {

              // alert ("this end range is good: " + list[1]);

          }

          else

          {

              //alert ("changing end range to: " + list[0]);

              list[1] = list[0];

           }

         

          startRange = (list[0]);

          endRange = (list[1]);

         

          startRange = (startRange -1); //need as it won't export a single artboard, using 5,5 it needs 4,5 start and end range to export artboard 5.

         

        //alert (startRange);

        //alert (endRange);

          for ( i = startRange; i < endRange; i++ ) {

                    doc.artboards.setActiveArtboardIndex( i );

                   

                    //EXPORT iPhone retina and iPad.

                    opts.horizontalScale = opts.verticalScale = 100;

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

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

                   

                    // EXPORT non retina iPhone.

                    opts.horizontalScale = opts.verticalScale = 46.875;

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

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

          };

             app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;

};

Votes

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
Contributor ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

Hi, is anyone still around that could help me get this script working?

 

Used as is it returns this error:

 Screenshot 2022-04-16 at 11.52.16.png

 

If I remove line 37

              if (!list.match(/^\d+$/))

 

It brings up the artboard range dialog and appears to work, but if I put any single number (158), two numbers with a comma or a hyphen (158-159 or 158,159), it brings up a "bad entry" alert box.

 

If I remove lines 35 to 44

          {
              // only numbers?
              if (!list.match(/^\d+$/))
              {
                  // no, issue an alert
                  alert ('Bad entry: "'+list+'"');
                  list.splice(i,1);
                  i--;
               }
          }

 

It works but will only ever export one artboard, which is the next number after the start of the range, e.g. 150-159 will export 151.

 

The png file it exports has the original filename followed by _undefined.png

 

Can anyone help me solve this puzzle?

Votes

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 ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

I don't know which of the scripts you're referring to, but it's almost certainly a missing index. So if (!list.match(/^\d+$/)) should be if (!list[i].match(/^\d+$/)) . The same goes for any "list" which is meant to be an element of an array in that particular "for" loop.

Votes

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
Contributor ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

Thank you, femkeblanco. That's got the script working. I've been using the last one by Mal Reed (the one above my last post).

 

I still can't get it to export a range of artboards though and I think it has something to do with the fact that the exported png always has _undefined.png at the end. So it could simply be overwriting each png with the next.

 

I can't seem to find a way to amend the script to have the artboard name in the filename. It looks to be there, but doesn't work.

Votes

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 ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

In the File() constructor, (1) the path specifies pre-existing folders and (2) the artboards are also missing the indices. 

 

To save to the desktop, replace these two lines at the bottom of the script

 

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

 

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

 

with this line

 

expFile = File('~/Desktop/' + doc.name + '_' + doc.artboards[i].name + '.png'); 

 

Votes

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
Contributor ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

That's a lot better as it's putting the artboard name in the filename.

 

Sorry to be a massive pain though. It's still not exporting the artboard range it should be.

 

This is the current state of the script. I've removed the second scaled version as I'm going to amend the script to do eps and jpeg at the same time.

 

// https://community.adobe.com/t5/illustrator-discussions/export-selected-artboards-png24-javascript/m-p/4868365/highlight/true
#target illustrator
exportPNGs();
// CREATE the export function and for loop.
function exportPNGs() {
          if ( parseFloat( app.version ) < 15 ) { return; }
          if ( app.documents.length == 0 ) { return; }
          app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
          var i, doc, fileName, opts, expFile, startRange, endRange;
          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;
          
          //=== CREATE THE PROMPT WINDOW====
          listEntry = prompt ("Artboard range @1:1 scale of 7.2ppi", "0")
          // remove (optional) spaces
          listEntry = listEntry.replace(/, /g,',');
          // expand number ranges, per Peter Kahrel's suggestion:
          listEntry = listEntry.replace(/(\d+)-(\d+)/g, function(full,arg1,arg2)
          {
              var j, result=arg1;
              for (j=Number(arg1)+1; j<=Number(arg2); j++)
              result += ','+String(j);
              return result;
          }
          );
          // split on comma's
          list = listEntry.split(',');
          // check each entry
          for (i=0; i<list.length; i++)
          {
              // only numbers?
              if (!list[i].match(/^\d+$/))
              {
                  // no, issue an alert
                  alert ('Bad entry: "'+list+'"');
                  list.splice(i,1);
                  i--;
               }
          }
          //===END OF PROMPT WINDOW====
          
          if (list[1] >= 2)
          {
              // alert ("this end range is good: " + list[1]);
          }
          else
          {
              //alert ("changing end range to: " + list[0]);
              list[1] = list[0];
           }
          
          startRange = (list[0]);
          endRange = (list[1]);
          
          startRange = (startRange -1); //need as it won't export a single artboard, using 5,5 it needs 4,5 start and end range to export artboard 5.
          
        //alert (startRange);
        //alert (endRange);
          for ( i = startRange; i < endRange; i++ ) {
                    doc.artboards.setActiveArtboardIndex( i );
                    
                    //EXPORT iPhone retina and iPad.
                    opts.horizontalScale = opts.verticalScale = 100;
                    expFile = File('~/Desktop/' + doc.name + '_' + doc.artboards[i].name + '.png');  
                    doc.exportFile ( expFile, ExportType.PNG24, opts );
                    
          };
             app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
};

 

Thanks so much for your continued help with this. Much appreciated.

 

 

Votes

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
Contributor ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

Aha, I think I've worked out what's going on.

 

Firstly, I didn't fully understand the format needed when typing in the artboard range. In Illustrator you'd put a hyphen, 140-145 to export those six artboards. But for this script, you'd put a comma, 140,145 to export those six artboards.

 

Secondly, there are some artboards with "/" in the name, such as "HMP/YOI". Those artboards aren't getting exported, presumably because the script thinks it's hit a bit of code?

 

Votes

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 ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

I don't follow the logic of the original script and the poor formating makes reading it painful.  I've tried rewriting the last part so that 140-145 should export the six artboards and 140, 145 should export the two artboards.  You are correct about the forward slash in the artboard name.  Those will be replaced with underscores. 

 

// https://community.adobe.com/t5/illustrator-discussions/export-selected-artboards-png24-javascript/m-p/4868365/highlight/true
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, startRange, endRange;
    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;
    listEntry = prompt ("Artboard range @1:1 scale of 7.2ppi", "0");
    listEntry = listEntry.replace(/, /g,',');
    listEntry = listEntry.replace(/(\d+)-(\d+)/g, function(full,arg1,arg2) {
        var j, result = arg1;
        for (j = Number(arg1) + 1; j <= Number(arg2); j++)
        result += ',' + String(j);
        return result;});
    list = listEntry.split(',');
    for (var i = 0; i < list.length; i++) {
        if (!list[i].match(/^\d+$/)) {
            alert ('Bad entry: "'+list+'"');
            list.splice(i, 1);
            i--;
        }
    }
    for (var i = 0; i < list.length; i++) {
        doc.artboards.setActiveArtboardIndex(list[i] - 1);
        opts.horizontalScale = opts.verticalScale = 100;
        var ABname = doc.artboards[list[i] - 1].name.replace(/\//g, "_")
        expFile = File('~/Desktop/' + doc.name + '_' + ABname + '.png');
        doc.exportFile (expFile, ExportType.PNG24, opts);
    }
    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
}

 

Votes

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
Contributor ,
Apr 16, 2022 Apr 16, 2022

Copy link to clipboard

Copied

Wow, that's awesome. That takes me a lot further than I was at this time yesterday. I've got a Keyboard Maestro macro using an Applescript and Shell script taking the exported files, renaming them, putting them into specific folders based on their names, etc.

 

So this script you've helped get working is a massive, massive help and actually the first piece of the puzzle I've decided to make.

 

Thanks again.

Votes

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
Contributor ,
Apr 18, 2022 Apr 18, 2022

Copy link to clipboard

Copied

Hi again,

 

So, erm, I've hit upon two issues.

The first issue, that I can't seem to find a fix for, is that in Export for Screens it asks what type of anti-aliasing to use. I need to use Type Optimized (Hinted). Is that possible?

 

The second,  I've fixed and included the code here for future reference: is that the PNG needs to be 300dpi instead of 72dpi. Using imagecapture() and a resolution option fixes that.

 

// https://community.adobe.com/t5/illustrator-discussions/export-selected-artboards-png24-javascript/m-p/4868365/highlight/true
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, startRange, endRange;
    doc = app.activeDocument;
    // fileName = doc.name.match(/(.*)\.[^\.]+$/)[1];
    opts = new ImageCaptureOptions();
    opts.resolution = 300;
    opts.artBoardClipping = true;
    opts.matte = false;
    opts.horizontalScale = opts.verticalScale = 100;
    opts.transparency = true;
    listEntry = prompt ("Artboard range @1:1 scale of 7.2ppi", "0");
    listEntry = listEntry.replace(/, /g,',');
    listEntry = listEntry.replace(/(\d+)-(\d+)/g, function(full,arg1,arg2) {
        var j, result = arg1;
        for (j = Number(arg1) + 1; j <= Number(arg2); j++)
        result += ',' + String(j);
        return result;});
    list = listEntry.split(',');
    for (var i = 0; i < list.length; i++) {
        if (!list[i].match(/^\d+$/)) {
            alert ('Bad entry: "'+list+'"');
            list.splice(i, 1);
            i--;
        }
    }
    for (var i = 0; i < list.length; i++) {
        doc.artboards.setActiveArtboardIndex(list[i] - 1);
        opts.horizontalScale = opts.verticalScale = 100;
        var ABname = doc.artboards[list[i] - 1].name.replace(/\//g, "_")
        expFile = File('~/Desktop/' + doc.name + '_' + ABname + '.png');
        doc.imageCapture(expFile, ABname.artboardRect, opts);
    }
    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
}

 

Votes

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
Contributor ,
Apr 21, 2022 Apr 21, 2022

Copy link to clipboard

Copied

Hi, femkeblanco,

 

I've been refining the script to export with typeoptimized and 300dpi, using Export for Screens. I found and adapted a small script that does that, but am now trying to incorporate it into the one you kindly got working for me, so that it uses the artboard range.

 

However, I can't seem to get it working. It throws up an error on line 36 (Error 1243: Illegal argument, argument 2, required value is missing)

 

Are you able to help?

// https://community.adobe.com/t5/illustrator-discussions/export-selected-artboards-png24-javascript/m-p/4868365/highlight/true
exportPNGs();
function exportPNGs() {
    if (parseFloat(app.version) < 15) {return;}
    if (app.documents.length == 0) {return;}
    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
    var i, doc, fileName, exportForScreensOptions, expFile, startRange, endRange;
    doc = app.activeDocument;
    // fileName = doc.name.match(/(.*)\.[^\.]+$/)[1];
    var resolution = 300;
    var exportForScreensOptions = new ExportForScreensOptionsPNG24() ; 
    exportForScreensOptions.antiAliasing = AntiAliasingMethod.TYPEOPTIMIZED ;
    exportForScreensOptions.scaleType = ExportForScreensScaleType.SCALEBYRESOLUTION;
    exportForScreensOptions.scaleTypeValue = resolution;
    
    listEntry = prompt ("Artboard range @Deleted User Type Optimised", "0");
    listEntry = listEntry.replace(/, /g,',');
    listEntry = listEntry.replace(/(\d+)-(\d+)/g, function(full,arg1,arg2) {
        var j, result = arg1;
        for (j = Number(arg1) + 1; j <= Number(arg2); j++)
        result += ',' + String(j);
        return result;});
    list = listEntry.split(',');
    for (var i = 0; i < list.length; i++) {
        if (!list[i].match(/^\d+$/)) {
            alert ('Bad entry: "'+list+'"');
            list.splice(i, 1);
            i--;
        }
    }
    for (var i = 0; i < list.length; i++) {
        doc.artboards.setActiveArtboardIndex(list[i] - 1);
        var itemToExport = new ExportForScreensItemToExport() ; 
        var ABname = doc.artboards[list[i] - 1].name.replace(/\//g, "_") 
        expFile = File('~/Desktop/' + doc.name + '_' + ABname + '.png');
        doc.exportForScreens(expFile, ABname.artboardRect, exportForScreensOptions, itemToExport);
    }
    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS;
}

Votes

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 ,
Apr 21, 2022 Apr 21, 2022

Copy link to clipboard

Copied

Unfortunately, I use an older version of Illustrator and I don't have exportForScreens(). However, looking at this example, exportForScreens() takes at least four arguments, (1) a file, (2) ExportForScreensType, (3) exportForScreensOptions and (4) itemToExport.  So your second argument is incorrect, as the error is telling you. Line 36 should be something like this:

doc.exportForScreens(expFile, ExportForScreensType.se_png24, exportForScreensOptions, itemToExport);

Also, you'll probably need to add a line like this before the exportForScreens() line:

itemToExport.artboards = list[i];

Although, as I said, I don't have exportForScreens(), so this is all guesswork. If you still can't get it to work, may be @Charu Rajput will be kind enough to help?

Votes

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
Contributor ,
Apr 23, 2022 Apr 23, 2022

Copy link to clipboard

Copied

LATEST

Ok, after a lot of hunting and multiple failures, Charu Rajput has come to the rescue to finalise the script for me. I don't think I'd ever have worked it out myself.

 

The following script asks the user for the number(s) of the artboard(s) and exports PNGs at 300dpi with Type Optimized anti-aliasing. The files are exported to the desktop with the document name, followed by the artboard name.

 

Thanks to everyone involved (named or not as I looked at dozens of scripts)

 

// https://community.adobe.com/t5/illustrator-discussions/export-selected-artboards-png24-javascript/m-p/4868365/highlight/true
// Special thanks to femkeblanco and Charu Rajput for their expert guidance 
exportPNGs();
function exportPNGs() {
    if (parseFloat(app.version) < 15) { return; }
    if (app.documents.length == 0) { return; }
    app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
    var i, doc, fileName, exportForScreensOptions, expFile, startRange, endRange;
    doc = app.activeDocument;
    var resolution = 300;
    var exportForScreensOptions = new ExportForScreensOptionsPNG24();
    exportForScreensOptions.antiAliasing = AntiAliasingMethod.TYPEOPTIMIZED;
    exportForScreensOptions.scaleType = ExportForScreensScaleType.SCALEBYRESOLUTION;
    exportForScreensOptions.scaleTypeValue = resolution;

    listEntry = prompt("Artboard range @Deleted User Type Optimised", "0");
    listEntry = listEntry.replace(/, /g, ',');
    listEntry = listEntry.replace(/(\d+)-(\d+)/g, function (full, arg1, arg2) {
        var j, result = arg1;
        for (j = Number(arg1) + 1; j <= Number(arg2); j++)
            result += ',' + String(j);
        return result;
    });
    list = listEntry.split(',');
    for (var i = 0; i < list.length; i++) {
        if (!list[i].match(/^\d+$/)) {
            alert('Bad entry: "' + list + '"');
            list.splice(i, 1);
            i--;
        }
    }
    var itemToExport = new ExportForScreensItemToExport();
    itemToExport.artboards = list.toString();
    expFolder = File('~/Desktop/');
    docName = doc.name.replace('.ai', '_');
    doc.exportForScreens(expFolder, ExportForScreensType.SE_PNG24, exportForScreensOptions, itemToExport, docName);
    app.userInteractionLevel = UserInteractionLevel.DISPLAYALERTS
    }

Votes

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