Skip to main content
Participant
October 20, 2024
Question

Error When Exporting Artboards as JPG Using Script

  • October 20, 2024
  • 2 replies
  • 196 views
        if (exportArtboards) {
            var baseFileName = doc.name.replace(/\.[^\.]+$/, '');
            var fileFolder = doc.path;

            for (var n = 0; n < doc.artboards.length; n++) {
                doc.artboards.setActiveArtboardIndex(n);
                var artboardBounds = doc.artboards[n].artboardRect;

                var artboardWidth = artboardBounds[2] - artboardBounds[0];
                var artboardHeight = artboardBounds[1] - artboardBounds[3];

                var targetWidth = 1280;
                var targetHeight = Math.round((artboardHeight / artboardWidth) * targetWidth);

                var fileName = (doc.artboards.length === 1 ? baseFileName : baseFileName + (n + 1)) + ".jpg";
                var exportFile = new File(fileFolder + "/" + fileName);
                var exportOptions = new ExportOptionsJPEG();
                exportOptions.qualitySetting = 60;
                exportOptions.artBoardClipping = true;

                exportOptions.horizontalScale = (targetWidth / artboardWidth) * 100;
                exportOptions.verticalScale = (targetHeight / artboardHeight) * 100;

                $.writeln("Artboard bounds: " + artboardBounds);
                $.writeln("Original width: " + artboardWidth + ", height: " + artboardHeight);
                $.writeln("Export target width: " + targetWidth + ", height: " + targetHeight);
                $.writeln("Horizontal scale: " + exportOptions.horizontalScale + ", Vertical scale: " + exportOptions.verticalScale);

                doc.exportFile(exportFile, ExportType.JPEG, exportOptions);
            }
        }

I used the script above to export artboards as JPG images. The problem now is that if the artboard size is too small, an error message appears: 'Specified value greater than maximum allowed value.' After checking the documentation, I found that this error occurs if the scale factor is greater than 7.76. Is there any way to solve this?

This topic has been closed for replies.

2 replies

m1b
Community Expert
Community Expert
October 21, 2024

Hi @Liu254468142lhe if you need to be able to (a) specify an arbitrary width and height, and (b) handle an arbitrary sized artboard, then there will always be cases where the "scaleFactor" is too high for the jpeg export API. The next approach—if you are willing to put more work into your script—is to scale the actual artwork, including artboard(s) and then export at 100%.

- Mark

Legend
October 20, 2024

exportFile is limited to that scale factor; exportForScreens allows you to specify a larger number.


A good sample is written by @m1b on this page. Please refer to it.

Javascript for using Asset Export