Skip to main content
Participant
March 18, 2023
Answered

script that rasterizes selected elements to a single image in Illustrator

  • March 18, 2023
  • 1 reply
  • 1802 views

I want to create a script that rasterizes selected elements to a single image according to defined parameters. I am interested in CMYK, 200ppi, background: white, preserve spot colors.
Can I somehow use app.executeMenuCommand("Rasterize 8 menu item") for this?

I tried to find something on the subject but failed. Does anyone have a hint?

 

I have it so far, but I'm stuck on the pop-up 😕😕

 

// select all
app.executeMenuCommand("selectall");
// rasterize
app.executeMenuCommand("Rasterize 8 menu item")

This topic has been closed for replies.
Correct answer Mike Bro

Hello @irek289170064v73 

 

Give this a try...

raster = function() {

 app.executeMenuCommand("selectall");
 app.executeMenuCommand("group");

   this.doc = app.activeDocument;
   this.options = new RasterizeOptions();
   this.options.resolution = 200;
   this.options.transparency = true;
   this.options.antiAliasingMethod = AntiAliasingMethod.ARTOPTIMIZED;

   var sourceArt = this.doc.selection[0];
   var clipBounds = sourceArt.visibleBounds;
   this.doc.rasterize(sourceArt, clipBounds, this.options);

} 
raster();

 

And as @CarlosCanto stated...

"and this document for all options you need that are missing from the script"

https://ai-scripting.docsforadobe.dev/jsobjref/RasterizeOptions.html

 

otherwise the defaults will be used for the ones missing.

 

Regards,

Mike

1 reply

CarlosCanto
Community Expert
Community Expert
March 18, 2023

check this thread for a how to rasterize an object sample

https://community.adobe.com/t5/illustrator-discussions/raster-resolution/m-p/9747492#M88839

 

and this document for all options you need that are missing from the script

https://ai-scripting.docsforadobe.dev/jsobjref/RasterizeOptions.html

 

let us know if you get stuck and need more help

Participant
March 18, 2023

 

I managed to create something not quite like I wanted because all the elements are separate photos and I wanted one of the whole graphic. How do I correct this?

 

// select all
app.executeMenuCommand("selectall");

 

var Rasterizer = function (resolution) {
this.doc = app.activeDocument;
this.selection = this.doc.selection;
this.options = new RasterizeOptions();
this.options.resolution = resolution || 200;
this.options.transparency = false;
this.options.antiAliasingMethod = AntiAliasingMethod.ARTOPTIMIZED;
};
Rasterizer.prototype.run = function () {
var n = this.selection.length;
for (var i = 0; i < n; i++) {
var sourceArt = this.selection[i];
var clipBounds = sourceArt.visibleBounds;
this.doc.rasterize(sourceArt, clipBounds, this.options);
}
};

var rasterizer = new Rasterizer();
rasterizer.run();