#target illustrator
Folder.prototype.extension = function()
{
return undefined;
}
File.prototype.extension = function()
{
var dotPosition=this.displayName.lastIndexOf('.');
return this.displayName.substr(dotPosition+1).toLowerCase();
}
var rootFolder = Folder.selectDialog ("Please select folder to process");
var allFiles = rootFolder.getFiles(isAiFile);
var docRef = app.open(allFiles[0]);
for(var i=1;i<allFiles.length;i++)
try{
var newRef = app.open(allFiles[i]);
newRef.selectObjectsOnActiveArtboard();
var bounds = app.selection[0].visibleBounds;
for(var j=0;j<app.selection.length;j++)
try{
var currBounds = app.selection[j].visibleBounds;
bounds[0]=Math.min(bounds[0],currBounds[0]);
bounds[1]=Math.max(bounds[1],currBounds[1]);
}catch(e){};
var deltaX = bounds[0];
var deltaY = bounds[1];
app.copy();
docRef.activate();
app.paste();
bounds = app.selection[0].visibleBounds;
for(var j=0;j<app.selection.length;j++)
try{
var currBounds = app.selection[j].visibleBounds;
bounds[0]=Math.min(bounds[0],currBounds[0]);
bounds[1]=Math.max(bounds[1],currBounds[1]);
}catch(e){log(e)};
for(var j=0;j<app.selection.length;j++)
try{
app.selection[j].translate(-bounds[0]+deltaX,-bounds[1]+deltaY);
}catch(e){};
newRef.close(SaveOptions.DONOTSAVECHANGES);
}catch(e){}
function isAiFile(obj)
{
var result = false;
if (obj.constructor.name === 'File' && !obj.hidden && obj.extension()=="ai") result = true;
return result;
}