Skip to main content
Inspiring
October 20, 2020
Answered

Trying, as a beginner, to put a script together to print, embed, center, save, and close.

  • October 20, 2020
  • 2 replies
  • 1187 views

Opening multiple .ai documents and placing objects from a library.  I don't know what I'm doing, trying to make scripts, and this is what I'm trying to do:

 

Embed all placed objects,

Center the view of the artboard (ctrl+0)

Print

Save and Close.

 

I'm also trying to get all this to happen in either alphanumeric order (with respect to document name) or from left to right (with respect to document tabs).

 

I've been messing with other scripts trying to frankenstein something together, but I really don't know enough to even know where to start.

 

Thanks for any help or scripts that might come my way.

This topic has been closed for replies.
Correct answer Blake0D45

You need to set the activeDocument to the current iteration in the docs array.

var docs = [];
if (app.documents.length == 0) {
	docs = app.documents;
	docs.sort(function(a, b) {
		if (a.name > b.name) return -1;
		if (a.name < b.name) return 1;
		return 0;
	});
	// Loop through docs array and save each one
	
	for (var i = 0; i < docs.length; i++) {
	    app.activeDocument = docs[i];
	        for (var i = 0; i < app.activeDocument.placedItems.length; i ++) {
            app.activeDocument.placedItems[i].embed();
        }
	}
}

 


So here is what I ended up with.  You must click on the first document tab to begin.

This code centers the artboard, embeds any linked/placed objects, prints, saves and closes each document from left to right (with respect to document tabs) until complete.

 

Thanks for everyone's help!

 

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

while (documents.length > 0) {

executeMenuCommand("fitin");

app.activeDocument.print()

for (var i = 0; i < app.activeDocument.placedItems.length; i
++) {
app.activeDocument.placedItems[i].embed();
}

app.activeDocument.save()

app.activeDocument.close()

if (app.documents.length > 0){

app.documents[app.documents.length-1].activate();}

}

2 replies

iconify
Inspiring
October 21, 2020

I believe this repository should have everything you need. Look in ContactSheet.jsx. If you search for createFromFile that will show you how to import files such as SVG, PDF, AI, etc. You will need to follow through the logic to figure out the other pieces but I've fairly certain everything you've mentioned is in there. I am happy to try to answer any questions I can to explain the code.

 

What this script does is fairly simple : Select a folder of SVG files (or other formats), and enter paramters to import all of the files and place them in rows and columns creating a contact sheet. While this places all of the imports on the same artboard, it is fairly trivial to place each one on a separate artboard. I have an extension I am waiting to be approved that does this. It will be free as soon as it is published. These don't do exactly what you want but I think all of the concepts are covered by the code.

 
Blake0D45Author
Inspiring
October 21, 2020

Ok, here's what I have so far.  It will center print and save a document that I've not made any changes to.  If I make any changes (including placing a linked file) it bugs out at d.print();.  So I'm not yet sure if the embed function works.  See what you think...

 

// JavaScript Document

#target illustrator

app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

var closed = true;

var saveOptions = new IllustratorSaveOptions();

saveOptions.embedLinkedFiles = true;

executeMenuCommand("fitin");

function imprimtous(){

var i, d;

for(i = app.documents.length-1; i>-1; i--) {

d = app.documents[i];

d.print();

if (closed) {

if (!d.saved) d.close(SaveOptions.SAVECHANGES);

else d.close(SaveOptions.DONOTSAVECHANGES);

}

}

}

imprimtous();

Blake0D45Author
Inspiring
October 21, 2020

Weird, I didn't do anything different and now this will run without a bug when I make changes.  However, the embed code is not working yet.

femkeblanco
Legend
October 20, 2020

 

var docs = app.documents;
var saveOptions = new IllustratorSaveOptions();
saveOptions.embedLinkedFiles = true;
for (var i = docs.length - 1; i > -1; i--) {
  var path1 = "/C/Users/.../Desktop/" + docs[i].name;
  var file1 = new File(path1)
  docs[i].saveAs(file1, saveOptions);
  docs[i].print();
  docs[i].close(SaveOptions.SAVECHANGES);
}

 

NB

  1. Change "..." in the path in line 5 to your destination.
  2. I have not tested the print function.
  3. To centre your view (Ctrl+0), you can add

 

executeMenuCommand("fitin");​


but I suspect this is not what you want (it'll centre your view, not your objects).

 

iconify
Inspiring
October 21, 2020

The function below will center an "item" on the parent artboard. An "item" can be a PageItem or GroupItem. I'm not sure how to place library items in an AI document as I have never tried it before. I did a quick search of the Illustrator script guide but did not find anything. I'm sorry this is the only part I can be helpful with at the moment.

 

function centerItem(theItem) {
    try {
        var doc = app.activeDocument,
            artboard = doc.artboards[doc.artboards.getActiveArtboardIndex()];

        if (! artboard) {
            doc.artboards.setActiveArtboardIndex(0);
        }

        var left   = artboard.artboardRect[0],
            top    = artboard.artboardRect[1],
            right  = artboard.artboardRect[2],
            bottom = artboard.artboardRect[3];

        theItem.position = [
            Math.round((right - theItem.width)/2),
            Math.round((bottom + theItem.height)/2)
        ];
    }
    catch(ex) {
        try {
            theItem.position = [0, 0];
        }
        catch(ex) {/*Exit Gracefully*/}
    }
}