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

[Scripting]FitObjectToArtboard batch script.

Community Beginner ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

Hello everyone,

I am using AI Scripts fitObjectToArtboard_v2 in case you are familiar with it.


Code below.

 

#target Illustrator

// script.name = fitObjectToArtboardBounds_v2.jsx;
// script.description = resizes selected object(s) to fit to the Artboard(s) Bounds;
// script.required = select ONE object before running; CS4 & CS5 Only.
// script.parent = carlos canto // 08/05/12;
// script.elegant = false;


// script.updates = first version fitted a single object to the active artboard.
// for this version, I added options for fitting selected object to all the artboards.
// it also works with multiple selections, all selected objects will be processed at once.
// Bonus: there's an option for adding padding to object's final size.


var idoc = app.activeDocument;
selec = idoc.selection;
if (selec.length>0) {

var margins = 0;
//var margins = Number(Window.prompt (msg, 0, title))*2.834645669; // 1 mm = 2.834645669 pts

var allArtboards = true;

for (j=0; j<selec.length; j++) {
// get selection bounds
var boundsdiff = getPgItemGBoundsvsVBoundsDifference (selec[j]);

if (allArtboards) {
//alert(allArtboards);

var ABs = idoc.artboards;
for(i=0; i<ABs.length; i++) {
var activeAB = ABs[i];
var ABprops = getABbounds (activeAB, margins); // get top, left, width & height

var sel = selec[j].duplicate(); // idoc.selection[0];
fitPgItem (sel, ABprops, boundsdiff);
}
}
else {
//alert(allArtboards);
var activeAB = idoc.artboards[idoc.artboards.getActiveArtboardIndex()]; // get active AB
var ABprops = getABbounds (activeAB, margins); // get top, left, width & height
var sel = selec[j].duplicate(); // idoc.selection[0];
fitPgItem (sel, ABprops, boundsdiff);
}
} // end if there's something selected

//idoc.selection = null;
//selec[0].selected = true;
}
else {
alert("select something before running");
}

//--------------------------------------------------------------------------------------------------------
// move and resize pgItem
function fitPgItem(pgItem, containerProps, pgItemBoundsDiff) {
var sel = pgItem;
var ABprops = containerProps;
var boundsdiff = pgItemBoundsDiff;

sel.width = ABprops.width-boundsdiff.deltaX; // width is Geometric width, so we need to make it smaller...to accomodate the visible portion.
sel.height = ABprops.height-boundsdiff.deltaY;
sel.top = ABprops.top; // Top is actually Visible top
sel.left = ABprops.left; // dito for Left
sel.selected = false;
}
//--------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------
//returns a pageItem's VisibleBounds vs GeometricBounds Difference
// needed to substract from an item's width/height since they're based on Geometric width/height
function getPgItemGBoundsvsVBoundsDifference (pgitem) {
// get selection bounds
var sel = pgitem;
var selVB = sel.visibleBounds;
var selVw = selVB[2]-selVB[0];
var selVh = selVB[1]-selVB[3];

var selGB = sel.geometricBounds;
var selGw = selGB[2]-selGB[0];
var selGh = selGB[1]-selGB[3];

// get the difference between Visible & Geometric Bounds
var deltaX = selVw-selGw;
var deltaY = selVh-selGh;

return deltaxy = {deltaX:deltaX, deltaY:deltaY};
}
//--------------------------------------------------------------------------------------------------------

//--------------------------------------------------------------------------------------------------------
// returns an Artboard with top, left, width & height properties, including Margins
function getABbounds (artboard, padding) {
var activeAB = artboard;
var margins = padding;

var abBounds = activeAB.artboardRect; // get bounds [left, top, right, bottom]
var ableft = abBounds[0]-margins;
var abtop = abBounds[1]+margins;
var abright = abBounds[2]+margins;
var abbottom = abBounds[3]-margins;

var abwidth = abright-ableft;
var abheight = abtop-abbottom;

return AB = {left:ableft, top:abtop, width:abwidth, height:abheight};
}
//--------------------------------------------------------------------------------------------------------


I have eliminated any dialog boxes requirements as I have set the margins to exactly what I require and I've been trying to get this script to run in batches on a folder of images. 

What I want to do is a script that gets a folder of images, imports each of them into my active document, selects them, runs the above script, and "Exports as" using All artboards into a newly created folder for each source image in the initial source folder containing the source images.

Would it be better to modify the existing script above to do that or do I need to make a new script that calls upon it? I've been smashing my head at this with the help of GPT for the past few days but to no avail. I would appreciate any guidance I can get.

Thank you all sincerely

TOPICS
Import and export , Scripting

Views

326

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
Adobe
Community Expert ,
Apr 19, 2023 Apr 19, 2023

Copy link to clipboard

Copied

LATEST

Either way, try writing a separate script that only imports (places) the images from a folder. That's it, once you accomplish that we can move on to applying the above script.

 

You can ask chatGPT or search the forum for samples of how to place an image from a folder

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