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
  • 1806 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 used what I found on the web and managed to make a script that works the way I wanted. It's not the most beautiful, kind of Frankenstein 🙂
If anyone would like to simplify it - I'd be happy to learn something.

 


// removes everything out of the artboard

app.executeMenuCommand ("selectallinartboard");
app.executeMenuCommand ("Inverse menu item");
app.executeMenuCommand ("clear");

// select all

app.executeMenuCommand("selectall");

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

// rasterize
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 = true;
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();

Mike BroCorrect answer
Legend
March 18, 2023

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