Skip to main content
sublimechaos
Inspiring
April 12, 2023
Answered

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

  • April 12, 2023
  • 2 replies
  • 3742 views

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

This topic has been closed for replies.
Correct answer sublimechaos

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?

 


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

2 replies

sublimechaos
Inspiring
April 21, 2023

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

 

femkeblanco
Legend
April 22, 2023

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

 

sublimechaos
Inspiring
April 24, 2023

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!

sublimechaos
Inspiring
April 13, 2023

did i post in the correct forum? 

CarlosCanto
Community Expert
Community Expert
April 13, 2023

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.

sublimechaos
Inspiring
April 13, 2023

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.