Skip to main content
Participant
December 14, 2021
Answered

Using JavaScript, cannot export png24 with 300 ppi

  • December 14, 2021
  • 5 replies
  • 1888 views

I have a document that I can export manually with 300ppi resolution, just like it's set in Effect -> Document Raster Effects Settings...

But when I use script, it always seems to default to 72ppi. I tried every other file format and it always sets to 72. Tried using JPEG quality parameter = 100, still resolves to 72ppi.

my document size is 600x600 pt, one artboard, the artboard corners are set at integer values, either at 0 pt or 600 pt. 

Any help would be appreciated!
Here's my code snippet below:

function exportFileToPNG(){

    if (app. documents. length > 0) {

    var exportOptions = new ExportOptionsPNG24() ;

    exportOptions.artBoardClipping = true;

    var type = ExportType. PNG24;

    var file = new File(filePath);

    app.activeDocument.exportFile(file, type, exportOptions);

}
}

This topic has been closed for replies.
Correct answer Doug A Roberts

There is no function to set PPI in the scripting reference for either JPEG or PNG exports. See here for a possible workaround:

https://community.adobe.com/t5/illustrator-discussions/setting-the-resolution-output-in-an-export-png-script/td-p/10568663

 

Incidentally, Effect > Document Raster Effect Settings doesn't set PPI for export either, it only sets it for raster effects such as drop shadow within Illustrator.

5 replies

CarlosCanto
Community Expert
Community Expert
December 14, 2021

another alternative for future reference, Export for Screens supports custom Resolution exports and it's available in the Scripting API

Inventsable
Legend
December 14, 2021

You can have your script create, load, execute then unload an Action that exports beyond 72:

 

/**
 * Thanks Ten-A! 
 * https://community.adobe.com/t5/illustrator/illustrator-jpegexportoptions-object-properties-are-missing-in-object-model/td-p/8699364?page=1
 *
 *  {String} pth - Filepath for document destination
 */
function exportJPG(pth) {
  //Exports 300ppi CMYK jpg
  var str = "";
  for (var i = 0; i < pth.length; i++) str += u16to8(pth.charCodeAt(i));
  var act =
    "/version 3" +
    "/name [ 4" +
    "	73657431" +
    "]" +
    "/isOpen 1" +
    "/actionCount 1" +
    "/action-1 {" +
    "	/name [ 4" +
    "		61637432" +
    "	]" +
    "	/keyIndex 0" +
    "	/colorIndex 0" +
    "	/isOpen 1" +
    "	/eventCount 1" +
    "	/event-1 {" +
    "		/useRulersIn1stQuadrant 0" +
    "		/internalName (adobe_exportDocument)" +
    "		/localizedName [ 9" +
    "			4578706f7274204173" +
    "		]" +
    "		/isOpen 1" +
    "		/isOn 1" +
    "		/hasDialog 1" +
    "		/showDialog 0" +
    "		/parameterCount 7" +
    "		/parameter-1 {" +
    "			/key 1885434477" +
    "			/showInPalette 0" +
    "			/type (raw)" +
    "			/value < 104" +
    "				0a00000001000000030000000100000000002c01020000000000000001000000" +
    "				69006d006100670065006d006100700000006800720069007300200054007500" +
    "				73006b006f00770073006b00690020002d00200041006e006e00610020004300" +
    "				0000000001000000" +
    "			>" +
    "			/size 104" +
    "		}" +
    "/parameter-2 {/key 1851878757/showInPalette 4294967295" +
    "/type (ustring)/value [ " +
    str.length / 2 +
    " " +
    str +
    "]}" +
    "		/parameter-3 {" +
    "			/key 1718775156" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ 16" +
    "				4a5045472066696c6520666f726d6174" +
    "			]" +
    "		}" +
    "		/parameter-4 {" +
    "			/key 1702392942" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ 12" +
    "				6a70672c6a70652c6a706567" +
    "			]" +
    "		}" +
    "		/parameter-5 {" +
    "			/key 1936548194" +
    "			/showInPalette -1" +
    "			/type (boolean)" +
    "			/value 1" +
    "		}" +
    "		/parameter-6 {" +
    "			/key 1935764588" +
    "			/showInPalette -1" +
    "			/type (boolean)" +
    "			/value 1" +
    "		}" +
    "		/parameter-7 {" +
    "			/key 1936875886" +
    "			/showInPalette -1" +
    "			/type (ustring)" +
    "			/value [ 3" +
    "				312d36" +
    "			]" +
    "		}" +
    "	}" +
    "}" +
    "";
  var tmp = File(Folder.desktop + "/set10.aia");
  tmp.open("w");
  tmp.write(act);
  tmp.close();
  app.loadAction(tmp);
  app.doScript("act2", "set1", false);
  app.unloadAction("set1", "");
  tmp.remove();
}
function u16to8(cd) {
  var out =
    cd < 0x80
      ? toHex2(cd)
      : (cd < 0x800
          ? toHex2(((cd >> 6) & 0x1f) | 0xc0)
          : toHex2((cd >> 12) | 0xe0) + toHex2(((cd >> 6) & 0x3f) | 0x80)) +
        toHex2((cd & 0x3f) | 0x80);
  return out;
}
function toHex2(num) {
  var out = "0" + num.toString(16);
  return out.slice(-2);
}

 

ImageCapture is easier but this is another alternative, I've had clients who specifically use Actions to export and using the same Actions as they do ensures the results are identical especially with CMYK or particular settings.

Inventsable
Legend
December 14, 2021

For PNG24, if you record an Action (destination path doesn't matter) to export to your specs, then export this .aia Action file you can retrofit it into the code above. The concatenation is a bit obnoxious above ha, but essentially you'd swap the above Action text with your own. You'd still need to include the manual section that designates the filepath, which in the above Action is this section:

 

    "/parameter-2 {/key 1851878757/showInPalette 4294967295" +
    "/type (ustring)/value [ " +
    str.length / 2 +
    " " +
    str +
    "]}" +

 

There should be a corresponding parameter-2 looking very similar in yours, you'd have to copy/paste the sections following "/type" and below to replace it.

 

Also! You'd need to accurately have your Action Name and Action Set parameters set in the above snippet within the app.doScript and app.unloadAction methods.

manal shanableh
Legend
December 14, 2021

i was wondering why to use script to export 1 artboard in a file!

 

easy to do

 

file> export to

Doug A Roberts
Community Expert
Community Expert
December 14, 2021

I wouldn't make any assumptions about why a user would want to use a script to export. It might be a snippet of a larger workflow.

Doug A Roberts
Community Expert
Doug A RobertsCommunity ExpertCorrect answer
Community Expert
December 14, 2021

There is no function to set PPI in the scripting reference for either JPEG or PNG exports. See here for a possible workaround:

https://community.adobe.com/t5/illustrator-discussions/setting-the-resolution-output-in-an-export-png-script/td-p/10568663

 

Incidentally, Effect > Document Raster Effect Settings doesn't set PPI for export either, it only sets it for raster effects such as drop shadow within Illustrator.

Participant
December 14, 2021

Thanks, Doug! I'll try it later today.