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

Scripting advice - Artboard save to new document and scale from document center

Explorer ,
Apr 12, 2023 Apr 12, 2023

Copy link to clipboard

Copied

hello fine members of illustrator community. I work in a prepress tech and am looking to streamline my tasks that i repeat daily as they are tedious and frustrating.

 

I'm seeking a script solution for the following and hopefully someone can point me in the right direction to get started on my first script. What i'd like to do:

 

1. only items on "Artboard 2" from an open file

2. select script opens a dialog box that allows a new document with size to be user entered and "arboard 2" contents placed in center

3. resize all content from document center (not selection center) at an entered scale factor (or scaled constrained proportionally to new document size).

4. bleed added

5. saved in output folder on desktop

 

I'd appreciate any of your knowledgeable advice to help me. I wouldn't think its hard, but my sad attempts at constructing an action/script/workflow has proven to be more difficult because the scale factor changes with each file and breaks all my efforts to streamline the process. 😞

TOPICS
Scripting

Views

1.9K

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 1 Correct answer

Explorer , Apr 27, 2023 Apr 27, 2023

so i think by using your intuition to solve this i've come up with the code i need. here's a copy: 

 

#target illustrator

var artboardIndex = 1;
var artboards = app.activeDocument.artboards;
// Set Destination Folder
var destFolder = Folder.selectDialog("Select the destination folder");
var destFileName = app.activeDocument.name;
var docWidth = prompt("Enter the new document width (in inches):") * 72;
var docHeight = prompt("Enter the new document height (in inches):")* 72;
// select the objects 
...

Votes

Translate

Translate
Adobe
Explorer ,
Apr 13, 2023 Apr 13, 2023

Copy link to clipboard

Copied

did i post in the correct forum? 

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 Expert ,
Apr 13, 2023 Apr 13, 2023

Copy link to clipboard

Copied

You're in the right forum, just need to sit tight and wait for a volunteer to have some free time to spend on proving help. The easier the question, the more chances of getting helped quicker.

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 ,
Apr 13, 2023 Apr 13, 2023

Copy link to clipboard

Copied

thank you, just wanted to make sure i was in the right place. i've read most your posts to date and will try and solve on my own as well.

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 Expert ,
Apr 13, 2023 Apr 13, 2023

Copy link to clipboard

Copied

That's a good start. If you want to learn, start by checking other scripts, copy/paste bits of code, focus on doing one thing at a time like get document size input from the user. Search the forum how to do that, ask a very specific question if you get stuck. Then move on to the next step, like creating a document.

 

We have posted sample code covering pretty much everything that can be done in illustrator.

 

I think that's better and more rewarding than me writing a script for you.

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 ,
Apr 25, 2023 Apr 25, 2023

Copy link to clipboard

Copied

gave it my best shot. lol. still cant get the content to center correctly, but did get it to scale factor correctly by making the formula from the original artboard size as opposed to the objects being selected. so i have the new doc, with the right size artwork, it's just centering to objects bounds and not the clipped trim size. 

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 ,
Apr 21, 2023 Apr 21, 2023

Copy link to clipboard

Copied

I gave it a try. Was able to get part of it to work, but still having trouble getting just the Artboard 2 from the index to get into the new file. Not sure what syntax i'm getting wrong. But here's the code in progress right now.

 

GOAL: This code should select the contents of the second artboard, save them as a new document with prompt for a new document size entered by user. Then scale the selected objects to fit the new document width proportionally, centered in the new document, and save the new document with the original filename and user defined new location.

~~~~~

 

var artboardIndex = 1;
var artboards = app.activeDocument.artboards;
var artboard = artboards[artboardIndex];
var destFolder = Folder.selectDialog("Select the destination folder");
var destFileName = app.activeDocument.name;
var docWidthInches = parseFloat(prompt("Enter the new document width (in inches):"));
var docHeightInches = parseFloat(prompt("Enter the new document height (in inches):"));
var docWidth = docWidthInches * 72;
var docHeight = docHeightInches * 72;
var newDoc = app.documents.add(DocumentColorSpace.CMYK, docWidth, docHeight);
artboards.setActiveArtboardIndex(artboardIndex);
var selection = app.activeDocument.selection;
selection = null;
var pageItems = artboards[artboardIndex].pageItems;
app.activeDocument.selection = pageItems;
app.copy();
newDoc.activate();
app.paste();
var pastedObjects = newDoc.selection;
var pastedObjectsWidth = pastedObjects.width;
var pastedObjectsHeight = pastedObjects.height;
var scaleFactor = Math.min(docWidth / pastedObjectsWidth, docHeight / pastedObjectsHeight);
pastedObjects.width *= scaleFactor;
pastedObjects.height *= scaleFactor;
pastedObjects.position = [docWidth / 2, docHeight / 2];
var newFile = new File(destFolder + "/" + destFileName);
newDoc.saveAs(newFile);
newDoc.close();

 

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 ,
Apr 22, 2023 Apr 22, 2023

Copy link to clipboard

Copied

There are few syntax and logic errors. 

 

1. This is an invalid statement

app.activeDocument.selection = pageItems

To select, change it to

app.activeDocument.selectObjectsOnActiveArtboard();

 

2. You need to select and copy while you're on the first document, i.e. before you add the second document. So move the statements that select and copy before app.documents.add().

 

3. These are invalid expressions

pastedObjects.width
pastedObjects.height
pastedObjects.position

A selection collection does not have such properties as width, height, position, et cetera. If you wish to manipulate items as a group, you'll have to group them.  The alternative is to manipulate them individually. 

 

This may do what you want:

var artboardIndex = 0;
var artboards = app.activeDocument.artboards;
var destFolder = Folder.selectDialog("Select the destination folder");
var destFileName = app.activeDocument.name;
var docWidth = prompt("Enter the new document width (in inches):") * 72;
var docHeight = prompt("Enter the new document height (in inches):")* 72;
// select
var selection = app.activeDocument.selection = null;
artboards.setActiveArtboardIndex(artboardIndex);
app.activeDocument.selectObjectsOnActiveArtboard();
// copy and paste
app.copy();
var newDoc = app.documents.add(DocumentColorSpace.CMYK, docWidth, docHeight);
var ABR = newDoc.artboards[0].artboardRect = [0, 0, docWidth, -docHeight];
app.paste();
newDoc.views[0].centerPoint = [docWidth / 2, -docHeight / 2];
// group, re-size and re-position
app.executeMenuCommand("group")
var pastedObjects = newDoc.selection[0];
var pastedObjectsWidth = pastedObjects.width;
var pastedObjectsHeight = pastedObjects.height;
var scaleFactor = Math.min(docWidth / pastedObjectsWidth, 
                           docHeight / pastedObjectsHeight);
pastedObjects.width *= scaleFactor;
pastedObjects.height *= scaleFactor;
pastedObjects.position = [docWidth / 2 - pastedObjects.width / 2, 
                          -docHeight / 2 + pastedObjects.height / 2];
var newFile = new File(destFolder + "/" + destFileName);
app.executeMenuCommand("ungroup");

newDoc.saveAs(newFile);
newDoc.close();

 

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 ,
Apr 24, 2023 Apr 24, 2023

Copy link to clipboard

Copied

thank you for the syntax corrections. it definitely is getting very close to what i need. all im running into now is that if there's a clipping mask on an image that goes beyond the artboard. The centering and scaling factor is being determined by the entire image, not the clipped size (trim layer). Not sure if there's a way to get around that, but i'll work with this some more later on today and see if i can finesse it.

I appreciate the time you took to look at it, your cor'x have been a huge help!

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 ,
Apr 24, 2023 Apr 24, 2023

Copy link to clipboard

Copied

so i updated the code to create the scale factor based on the original documents artboard size instead of the objects selected, which corrected the scaling issue. now it's just getting the art to be centered correctly in the new document. here's the updated 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
Explorer ,
Apr 24, 2023 Apr 24, 2023

Copy link to clipboard

Copied

#target illustrator

var artboardIndex = 1;
var artboards = app.activeDocument.artboards;
var destFolder = Folder.selectDialog("Select the destination folder");
var destFileName = app.activeDocument.name;
var docWidth = prompt("Enter the new document width (in inches):") * 72;
var docHeight = prompt("Enter the new document height (in inches):")* 72;

// select the objects on the artboard
artboards.setActiveArtboardIndex(artboardIndex);
app.activeDocument.selectObjectsOnActiveArtboard();

// calculate the scale factor based on the original artboard size
var currentArtboard = app.activeDocument.artboards[artboardIndex];
var currentArtboardBounds = currentArtboard.artboardRect;
var pastedObjectsWidth = currentArtboardBounds[2] - currentArtboardBounds[0];
var pastedObjectsHeight = currentArtboardBounds[1] - currentArtboardBounds[3];
var scaleFactor = Math.min(docWidth / pastedObjectsWidth, docHeight / pastedObjectsHeight);

// copy and paste
app.copy();
var newDoc = app.documents.add(DocumentColorSpace.CMYK, docWidth, docHeight);
var ABR = newDoc.artboards[0].artboardRect = [0, 0, docWidth, -docHeight];
app.paste();
newDoc.views[0].centerPoint = [docWidth / 2, -docHeight / 2];

// group, re-size and re-position
app.executeMenuCommand("group")
var pastedObjects = newDoc.selection[0];
pastedObjects.width *= scaleFactor;
pastedObjects.height *= scaleFactor;
pastedObjects.position = [docWidth / 2 - pastedObjects.width / 2, -docHeight / 2 + pastedObjects.height / 2];
var newFile = new File(destFolder + "/" + destFileName);
app.executeMenuCommand("ungroup");

newDoc.saveAs(newFile);
newDoc.close();

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 ,
Apr 24, 2023 Apr 24, 2023

Copy link to clipboard

Copied

not sure how you all paste the code in that inset box, sorry about the reposted code. hopefully it's not confusing.

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 ,
Apr 25, 2023 Apr 25, 2023

Copy link to clipboard

Copied

Assuming the clipping path (CP) is the topmost item, you can centre a clipping set based on the centre of the clipping path as such

var docWidth = app.activeDocument.width;
var docHeight = app.activeDocument.height;
var pastedObjects = app.activeDocument.selection[0];

var CP = pastedObjects.pathItems[0];
pastedObjects.position = [
    docWidth / 2 - ((CP.left - pastedObjects.left) + CP.width / 2), 
    -docHeight / 2 + (-(CP.top - pastedObjects.top) + CP.height / 2)];

If this is not what you need, screenshots—including the relevant items visible in the Layers Panel—are suggested. 

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 ,
Apr 26, 2023 Apr 26, 2023

Copy link to clipboard

Copied

Screen Shot 2023-04-26 at 10.49.40 AM.png

here's my concept phase, clients review many designs. only one wins.

 

Screen Shot 2023-04-26 at 10.51.56 AM.png

moving into production, i use the script on design to develop production art in a wide variety of sizes and scales. in this example, i chose a 22'8"x10'5" billboard (272in x 125in). So by using the artboard to develop the scale factor, it's enlarging the layout correctly, making the new doc.... but is centering the content based on the objects and not the visible bounds. as show in next photo....

 

Screen Shot 2023-04-26 at 10.52.18 AM.png

this photo shows the objects ungrouped, etc so you can see it is centering based on these stacked 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 ,
Apr 26, 2023 Apr 26, 2023

Copy link to clipboard

Copied

sorry forgot to include the layers palette. here's the shot with a "typical" layout structure. obviously the complexity of designs can vary greatly.

 

Screen Shot 2023-04-26 at 11.04.58 AM.png

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 ,
Apr 27, 2023 Apr 27, 2023

Copy link to clipboard

Copied

The last position snippet I posted should still work, it being a matter of targeting the relevant clipping path.  In this case, if I am correct, the relevant clipping path appears to be the same size as the artboard.  So this may work

var paths = app.activeDocument.pathItems;
for (var i = 0; i < paths.length; i++) {
    if (paths[i].clipping == true  && 
       (Math.round(paths[i].width) == Math.round(docWidth) ||
        Math.round(paths[i].height) == Math.round(docHeight))) {
            var CP = paths[i];
            break;
        }
}
pastedObjects.position = [
    docWidth / 2 - ((CP.left - pastedObjects.left) + CP.width / 2), 
    -docHeight / 2 + (-(CP.top - pastedObjects.top) + CP.height / 2)];

Or, to put it in the rest of your code

#target illustrator
var artboardIndex = 1;
var artboards = app.activeDocument.artboards;
// var destFolder = Folder.selectDialog("Select the destination folder");
var destFileName = app.activeDocument.name;
var docWidth = prompt("Enter the new document width (in inches):") * 72;
var docHeight = prompt("Enter the new document height (in inches):")* 72;
// select the objects on the artboard
artboards.setActiveArtboardIndex(artboardIndex);
app.activeDocument.selectObjectsOnActiveArtboard();
// calculate the scale factor based on the original artboard size
var currentArtboard = app.activeDocument.artboards[artboardIndex];
var currentArtboardBounds = currentArtboard.artboardRect;
var pastedObjectsWidth = currentArtboardBounds[2] - currentArtboardBounds[0];
var pastedObjectsHeight = currentArtboardBounds[1] - currentArtboardBounds[3];
var scaleFactor = Math.min(docWidth / pastedObjectsWidth, docHeight / pastedObjectsHeight);
// copy and paste
app.copy();
var newDoc = app.documents.add(DocumentColorSpace.CMYK, docWidth, docHeight);
var ABR = newDoc.artboards[0].artboardRect = [0, 0, docWidth, -docHeight];
app.paste();
newDoc.views[0].centerPoint = [docWidth / 2, -docHeight / 2];
// group, re-size and re-position
app.executeMenuCommand("group")
var pastedObjects = newDoc.selection[0];
pastedObjects.width *= scaleFactor;
pastedObjects.height *= scaleFactor;
//
var paths = app.activeDocument.pathItems;
for (var i = 0; i < paths.length; i++) {
    if (paths[i].clipping == true  && 
       (Math.round(paths[i].width) == Math.round(docWidth) ||
        Math.round(paths[i].height) == Math.round(docHeight))) {
            var CP = paths[i];
            break;
        }
}
pastedObjects.position = [
    docWidth / 2 - ((CP.left - pastedObjects.left) + CP.width / 2), 
    -docHeight / 2 + (-(CP.top - pastedObjects.top) + CP.height / 2)];
//
// var newFile = new File(destFolder + "/" + destFileName);
app.executeMenuCommand("ungroup");
// newDoc.saveAs(newFile);
// newDoc.close();

 

 

 

 

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 ,
Apr 27, 2023 Apr 27, 2023

Copy link to clipboard

Copied

boom! thank you @femkeblanco that worked. i'm so grateful for your time. you've made my life exponentially better. i'll now have more time to work on creating great art instead of endless repetitive scaling, cutting and pasting. sincerely thank you from the bottom of my endless prepress task pile. 😄

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 ,
Apr 27, 2023 Apr 27, 2023

Copy link to clipboard

Copied

i am curious though. is it possible instead of calculating based on the clipping path, to have it from the child layer "{TRIM} as that is a guaranteed constant as opposed to a clipping path that may or may not be to the correct size?

 

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 ,
Apr 27, 2023 Apr 27, 2023

Copy link to clipboard

Copied

LATEST

so i think by using your intuition to solve this i've come up with the code i need. here's a copy: 

 

#target illustrator

var artboardIndex = 1;
var artboards = app.activeDocument.artboards;
// Set Destination Folder
var destFolder = Folder.selectDialog("Select the destination folder");
var destFileName = app.activeDocument.name;
var docWidth = prompt("Enter the new document width (in inches):") * 72;
var docHeight = prompt("Enter the new document height (in inches):")* 72;
// select the objects on the artboard
artboards.setActiveArtboardIndex(artboardIndex);
app.activeDocument.selectObjectsOnActiveArtboard();
// calculate the scale factor based on the original artboard size
var currentArtboard = app.activeDocument.artboards[artboardIndex];
var currentArtboardBounds = currentArtboard.artboardRect;
var pastedObjectsWidth = currentArtboardBounds[2] - currentArtboardBounds[0];
var pastedObjectsHeight = currentArtboardBounds[1] - currentArtboardBounds[3];
var scaleFactor = Math.min(docWidth / pastedObjectsWidth, docHeight / pastedObjectsHeight);
// copy and paste
app.copy();
var newDoc = app.documents.add(DocumentColorSpace.CMYK, docWidth, docHeight);
var ABR = newDoc.artboards[0].artboardRect = [0, 0, docWidth, -docHeight];
app.paste();
newDoc.views[0].centerPoint = [docWidth / 2, -docHeight / 2];
// group, re-size and re-position
app.executeMenuCommand("group")
var pastedObjects = newDoc.selection[0];
pastedObjects.width *= scaleFactor;
pastedObjects.height *= scaleFactor;

// Find the "{TRIM}" path item
var trimPathItem;
for (var i = 0; i < app.activeDocument.pathItems.length; i++) {
  var pathItem = app.activeDocument.pathItems[i];
  if (pathItem.name === "{TRIM}") {
    trimPathItem = pathItem;
    break;
  }
}

// Use the "{TRIM}" path item to position the pasted objects
if (trimPathItem) {
  var xPos = docWidth / 2 - ((trimPathItem.left - pastedObjects.left) + trimPathItem.width / 2);
  var yPos = -docHeight / 2 + (-(trimPathItem.top - pastedObjects.top) + trimPathItem.height / 2);
  pastedObjects.position = [xPos, yPos];
} else {
  alert("Could not find a path item named '{TRIM}'");
}
var newFile = new File(destFolder + "/" + destFileName);
app.executeMenuCommand("ungroup");
newDoc.saveAs(newFile);
newDoc.close();

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