Sg2.9
Export/Save Image File?
Recent RSS
Export/Save Image File?
Date:  3. April 2006, 11:17

Firstly Scriptographer is a great piece of work, keep it up!

I was wanting to know if there was a capability to Export/Save/Write a bitmap file like a jpeg from Scriptographer on the fly without dialogs?
I'm wanting to write a sequence of files to make a short animation as the script runs.
I did a similar thing once with the Native AI/JS interface - have a look here.

I see there is a mention of writing a file int he 2.0 API, but does anyone have any examples of using this or know what it can do?
write(file[, format[, ask]])

Im using AI CS1 with The 2.0 beta.

ps.

lehni.
Looking forward to checking out Hektor at the exhibition in Tokyo on the 14th.
Will Hektor be drawing the whole time?

Re: Export/Save Image File?
Date:  3. April 2006, 22:46

Thanks for the reply Simon.

A simpler solution, I am not sure if it is possible. Would be to call another JS script using the native Illustrator scripting feature.
Can it be done?

// This script exports the current document as a PNG8 with specific options 
if ( app.documents.length > 0 ) 
{ 
var exportOptions = new ExportOptionsPNG8(); 
var type = ExportType.PNG8; 
var fileSpec = new File("~/sample.png"); 
exportOptions.colorCount = 8; 
exportOptions.transparency = false; 
app.activeDocument.exportFile( fileSpec, type, exportOptions ); 
} 

Re: Export/Save Image File?
Date:  4. April 2006, 11:45

Yes that would be the easiest option.

I looked over the documentation again, http://www.scriptographer.com/api/Document.html#write - The original API being more helpful. But the syntax has changed a bit to Document.fileFormats.

So I managed to get the returning a list of formats:

Quote:
PDF File Format, TIFF, AutoCAD Drawing, AutoCAD Interchange File, GIF file format, Flash Format Writer, JPEG file format, JPX, Enhanced Metafile, Windows Metafile, Macintosh PICT, MPS Parser, MPS Parser, MPS Parser, MPS Parser, Adobe PNG Format, Text, Mic

But I keep getting errors saying "Can't find method com.scriptographer.ai.Document.write(string,string,boolean)."
Here is my code if you want to try, I could be doing something very simple wrong I suspect.

 	var formatz = activeDocument.fileFormats;
	/*
	var formatArray = "";
	
		for(i = 0;i < formatz.length; i++) {
													
			formatArray += formatz[i]+", ";
		
		}
	
	values = Dialog.prompt("Formats:", [

			{ value: formatArray, description: "Format", width: 700 },

	]);
	*/
	
	//activeDocument.write(file[, format[, ask]]);
	activeDocument.write("giddayWorld.tif", formatz[1], true);
Re: Export/Save Image File?
Date:  5. April 2006, 00:02

Ok...

Did you get the same errors with that script?
I have a feeling my script isn't quite right?

From:  Jürg Lehni
Date:  6. April 2006, 18:05

Simon is right:

This part of Scriptographer is still unfinished.

What you could do is using Java ImageIO. You could rasterize all your object into a Raster object:

http://www.scriptographer.com/api/2.0/packages/ai/Art.html#rasterize(int,%20float,%20int,%20float,%20float)

Then get a Java BufferedImage version from it:

http://www.scriptographer.com/api/2.0/packages/ai/Raster.html#getImage()

And then use ImageIO through the Java Bridge to write a JPEG or a TIFF:

http://javaalmanac.com/egs/javax.imageio/Graphic2File.html

It should be rather straight forward:

Packages.javax.imageio.ImageIO.write(raster.getImage(), "jpg", new java.io.File("/test.jpg"))

Document.write will soon be implemented.

I will have to look into how easy it will be to call "native" AI scripts.

And about the Hektor show: Friday evening, Saturday and Sunday afternoon you will be able to see Hekor in action. I hope you have the time to drop by!

Re: Export/Save Image File?
From:  
Date:  17. April 2006, 04:43

Cool.
I got it going fine. Here is my code if anyone is interested.

// group is a group of lineart or whatever
//
// rasterize settings
// FIRST VARIABLE - 0 = RGB, 1 = CMYK, 2 = GREY, 3 = CRASH!
// SECOND VARIABLE - Resolution
// THIRD VARIABLE - Antialiasing setting
myRaster = group.rasterize(0, 72, 4);
myBufferedImage = myRaster.getImage();
Packages.javax.imageio.ImageIO.write(myBufferedImage, "jpg", new java.io.File("/test.jpg")) ;

So yeah, thanks!

PS,
Good to see Hektor Friday night, hope all went well the rest of the weekend.

Re: Export/Save Image File?
From:  Jonathan_
Date:  18. April 2006, 11:24

This is great! I will encorporate it into my little script that throws all the objects in your layers away one by one..

I used to record it with Snapz Pro, but the framerate is much more reliable this way.

Old code that throws away an object of each layer and then loops:

(And yes it keeps going after there's nothing left to remove, it's a bad job! :) )

var timer = new Timer(1, true);

timer.onExecute = function() {
	for(var i=0; i<activeDocument.layers.length; i++){
		if(activeDocument.layers[i].items.length>0){
			activeDocument.layers[i].items[0].remove()
		};
	}
}

timer.start();
Re: Export/Save Image File?
Date:  9. June 2007, 12:49

activeDocument.write();

Lehni wrote:
This part of Scriptographer is still unfinished.
hmm.. this hasn't been resolved yet, has it? Too bad..

Simon mentioned a print script you wrote. Is it possible to set the printer, but bypass the dialog so I can print to PDF without having to manually set the settings? (I'm talking about 1000+ files)

edit:

Illustrator may come equiped with a solution.. in the 'Illustrator\Scripting\Sample Scripts\JavaScript' folder there is a script called 'Save as PDFs.js', wich batch-saves an entire folder of ai's to pdf's.. ^__^

edit2:

I ended up writing a macro wich gets triggered when the 'save as' dialog pops up and saves the document as a PDF

Re: Export/Save Image File?
Date:  18. June 2007, 11:59

thanks for the effort, but I'm on pc.. so applescript won't work..
I wanted to automate exporting my files and printing it to a pdf-writer was one of the solutions.

The second thing I ran into was automated document closure.. each time I called the activeDocument.close() not the document was closed but Illustrator itself.
Like I said, I tried writing a macro, but it acted instable (it wasn't always triggered by the automated pop-up of the save-as dialog).

in the end I exported and closed every document (1152) by hand.. (scriptographer opened new document, did it's thing > command+e / return / command+w > repeat). I'll start a new thread with some pictures of the work if anyone's interested.

Re: Export/Save Image File?
From:  Jürg Lehni
Date:  18. June 2007, 14:00

Wow, saving 1152 files is a lot of work.

I am aware that this feature is very badly needed. I will also look into the problem with Document.close() soon.

I was pretty busy over the last days so I could not take care of it in time, but for future projects it would be great to have this.

And I am curious to see pictures. Maybe it is something for the gallery?

Please send examples to sg AT scriptographer DOT com

Scripts
08.08.14, 15:24
15.05.14, 14:23
02.03.14, 19:16
18.11.13, 14:48
22.03.13, 03:05
22.02.13, 15:45
Posts
10.01.17, 16:37
19.02.16, 06:03
19.02.16, 06:00
17.01.16, 11:00
12.01.16, 13:10
25.11.15, 08:19
Script of the Moment
Stroke 3D 16.06.09