With the valued assistance of a number of people on this forum I have
written a script which produces in Illustrator CS6 a layout that
is saved on an artboard slightly bigger than itself.
I have tried, using the listing worked between Carlos Canto
and the original enquirer, ScriptingRules, to focus the screen
on my artboard, but, I think, because the page origin details seem to
be variable between the files I am working with, what I get
sometimes is the amusing picture attached! Not the end of the
world, but I'd like to do the job right if I can.
As I know as much as John Snow about ruler origins and so on, please would some
kind person advise me how to set my original parameters (?) correctly for the "ctrl zero"
to function as I'd hope it would. If I start with a template file for my artwork that has
the origins in the right position for processing later, I'm hoping that will eradicate the problem.
This is the part of the script that sizes the artboard and tries to focus the screen:-
app.executeMenuCommand('selectall');
app.executeMenuCommand('group');
var sel1=idoc.selection[0].width
var sel2=idoc.selection[0].height
var artboardsizer=idoc.layers.add()
ab_width=(sel1+25)
ab_height=(sel2+25)
var artrect = idoc.pathItems.rectangle(0, 0, ab_width, ab_height); // create a disposable rectangle a bit bigger than the layout
app.coordinateSystem = CoordinateSystem.ARTBOARDCOORDINATESYSTEM;
var abIdx = idoc.artboards.getActiveArtboardIndex();
var actAbBds = idoc.artboards[abIdx].artboardRect;
var obj2move = artrect;
obj2move.position = new Array ((actAbBds[2]-actAbBds[0])/2 - obj2move.width/2, (actAbBds[3]-actAbBds[1])/2 + obj2move.height/2);
// centre the disposable rectangle around the (already centered) layout
app.executeMenuCommand('selectall');
var indexAB = idoc.artboards.getActiveArtboardIndex();
idoc.fitArtboardToSelectedArt(indexAB); // sets desired size of artboard
artboardsizer.remove(); // remove the disposable rectangle and the layer it was created on
app.executeMenuCommand('deselectall');
centerDocument();
function centerDocument(){ // courtesy of Carlos Canto & Scripting Rules
var artboardRef = idoc.artboards;
idoc.pageOrigin = [0,0];
idoc.rulerOrigin = [0,0];
var screenwidth=artboardRef[0].artboardRect[2]-artboardRef[0].artboardRect[0];
var screenheight=artboardRef[0].artboardRect[1]-artboardRef[0].artboardRect[3];
idoc.activeView.centerPoint = [screenwidth/2,screenheight/2]
if (screenwidth > screenheight)
{
var power = Math.pow((screenwidth/72),(-.962))
var zoom = idoc.activeView.zoom = 20*power
}else{
var power = Math.pow((screenheight/72),(-.962))
var zoom = idoc.activeView.zoom = 13*power
}
}
// var savethefile=false ----- next step is to save the file if it's OK .... only often I can't see anything!