Same artboardRect, different positions
Copy link to clipboard
Copied
I've been scratching my head over this seemingly simple task. I just want to create a new document with the same artboard as my current document. I'm using this code:
function main(){
// Document Reference
var doc = app.activeDocument;
// Current Doc artboardRect
var abRect = doc.artboards[0].artboardRect;
// Create a new document
var newDoc = app.documents.add();
// Set new document artboard to abRect
newDoc.artboards[0].artboardRect = abRect;
}
main();
The size looks right but the artboard is slightly offset from the original (original to the left):
The first document has a pageOrigin of [194, -896] which is also the exact ammount that the "new" artboard is being offset by so it feels I'm getting closer to solving this. BUT, it doesn't matter if I set the first document to [0,0] or the new document to [194, -896], it still gives me the same result for some reason.
Link to AI file: https://drive.google.com/file/d/1nReX0nNQ5HL4wBHtBoRLUluGkJlzKGiP/view?usp=sharing
Explore related tutorials & articles
Copy link to clipboard
Copied
Hi @iLLMonkey, does this help?
function main() {
// Document Reference
var doc = app.activeDocument;
var rulerOrigin = doc.rulerOrigin;
// Current Doc artboardRect
var abRect = doc.artboards[0].artboardRect;
// Create a new document
var newDoc = app.documents.add();
// Set new document artboard to abRect
newDoc.artboards[0].artboardRect = abRect;
newDoc.rulerOrigin = rulerOrigin;
$.writeln('doc.rulerOrigin = ' + doc.rulerOrigin);
$.writeln('newDoc.rulerOrigin = ' + newDoc.rulerOrigin);
$.writeln('doc.artboards[0].artboardRect = '
+ doc.artboards[0].artboardRect);
$.writeln('newDoc.artboards[0].artboardRect = '
+ newDoc.artboards[0].artboardRect);
// now matcht the *views*
var docView = doc.activeView;
var newDocView = newDoc.activeView;
// match the new doc view
newDocView.zoom = docView.zoom;
newDocView.visibleZoom = docView.visibleZoom;
newDocView.centerPoint = docView.centerPoint;
$.writeln('docView.bounds = '
+ docView.bounds);
$.writeln('newDocView.bounds = '
+ newDocView.bounds);
};
main();
That gives you a couple more things to play with to match your original document. There might be other things that need to be matched in the new document also. If matching the document is really important, another approach could be to duplicate documents File object (doc.fullName) and open it (and remove whatever you don't want in the new doc).
- Mark
Copy link to clipboard
Copied
Thanks @m1b Unfortunatly it didn't help. I've tried alot of different values. Strangely enough the properties below can all have the exact same values in two different document and still produce two different artboard placements:
pageOrigin
artboardRect
height
width
geometricBounds
cropBox
rulerOrigin
Am I missing any properties that could affect the artboard placement?
It gets weirder. if I create a rectangle the same size as the artboard in doc and .duplicate() it to the newDoc it gets placed exactly as I want it. This is a potential fix since I could just conform the artboard to the rectangle. But I still want to know why this is 😃
Copy link to clipboard
Copied
It think this is because documents.add() adds a document with whatever default width and size, with the origin (0, 0) being the lower left corner of the document. Any change to the origin will be in reference to that lower left corner.
Copy link to clipboard
Copied
Interesting. I can't reproduce that issue. Can you share a demo .ai file? You can attach it here if you save as pdf with ai editing.
- Mark
Copy link to clipboard
Copied

