• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Community Beginner ,
Oct 20, 2020 Oct 20, 2020

Copy link to clipboard

Copied

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.

TOPICS
Scripting

Views

382

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Guide , Oct 21, 2020 Oct 21, 2020

embedLinkedFiles and IllustratorSaveOptions are used with the saveAs() function. You do not have a saveAs() function in your code.  Alternatively, you can go through your placedItems and embed them individually

 

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

 

Votes

Translate

Translate
Community Beginner , Oct 22, 2020 Oct 22, 2020

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
++) {
a

...

Votes

Translate

Translate
Adobe
Guide ,
Oct 20, 2020 Oct 20, 2020

Copy link to clipboard

Copied

 

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).

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 20, 2020 Oct 20, 2020

Copy link to clipboard

Copied

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*/}
    }
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

Thank you so much.  I should have clarified, documents are existing, and I'm just needing to save changes (not new documents).

 

And you are correct about centering the view.  I do not want to move objects, just save the file to where the artboard is in full view.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 20, 2020 Oct 20, 2020

Copy link to clipboard

Copied

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.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

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();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

embedLinkedFiles and IllustratorSaveOptions are used with the saveAs() function. You do not have a saveAs() function in your code.  Alternatively, you can go through your placedItems and embed them individually

 

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

Oh man!  That works amazing!  Thank you!

 

So there's 1 more thing, and I don't know if it's possible.  This code will complete all open documents, but it goes in the order of last active document, and it would be awesome to either get it to go from left to right (with respect to document tabs) or in alphanumeric order (with respect to document name).

 

I know that ctrl+tab will cycle through documents from left to right, I don't know if coding that would be the way to go, but that's really the last thing I need to figure out for this.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

A little more testing revealed that the embed and center script only works on the first document and not on all the rest.  Not sure how to fix that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

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();
        }
	}
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Oct 22, 2020 Oct 22, 2020

Copy link to clipboard

Copied

LATEST

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();}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 21, 2020 Oct 21, 2020

Copy link to clipboard

Copied

You can list the open documents, sort them, then save them via your sorted list.

 

if (app.documents.length == 0) {
    var 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
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines