Copy link to clipboard
Copied
Hello!
I'm trying to improve GuideGuide's​ handling of Illustrator guides. So far I haven't found a built-in way to add guides, so I draw a path and then set it's guides value to true. While this works, it has the drawback that I have to figure out the boundary locations of the guides in addition to where I want to place them.
For my example, I'm using a horizontal guide.
Looking at how Illustrator handles things, it appears to add guides at the max left and max right bounds of the document. Mind you, it's possible to place objects even farther beyond those bounds, but they're the bounds that Illustrator visually shows. This is the behavior I'm trying to emulate.
So far I haven't found a scripted way to extract those boundaries from Illustrator, but my testing has thus far shown that the document max height and width is 16383pts. One would think that it would be as simple as placing a guide edge at 0 and 16383, however the document origin appears to be in the center. One would then think that (16383/2) * -1 would be the document edge, but this also does not turn out to be true. So far, in my testing the document origin is fluid, sometimes in the center, sometimes offset. I tried incorporating the document pageOrigin, but that has also not proven to be helpful. I'm even taking into account the artboard/document coordinate system differences and have found no obvious solution yet.
Am I missing some obvious details that can be used to pull these values from Illustrator? This seems like something that would be relatively easy to do.
Only tested with CS6 but it works, no matter artboards been resized or not.
...var d = activeDocument,
rect,
rects = [],
abs = d.artboards,
z = d.views[0].zoom,
cp = d.views[0].centerPoint,
i;
abs.add([0, 1, 1, 0]);
abs.add([0, 1, 1, 0]);
for (i = abs.length - 3; i >= 0; i--) {
rects.push([abs.artboardRect, abs.name]);
abs.remove();
}
d.rearrangeArtboards(DocumentArtboardLayout.Row, 1, 0, false);
rect = abs[0].artboardRect;
d.pathItems.rectangle(rect[1] - 0.5 + 16383 / 2, rect[0] +
Copy link to clipboard
Copied
Theoretically, it can still be calculated. We have the initial offset that is the half size of the document profile. You can get the position of an artboard and save it, drag a new origin, and get the artboard's position again. Then calculate the difference between two positions. That'd give the offset.
But the trouble is implementing this in Illustrator. I don't know if there are event listeners in Illustrator. For example, adding an event listener to "drag a new origin" and doing the calculation at that moment would really help.
Copy link to clipboard
Copied
The users can drag their own origins, create their own document profiles and change the artboards and do so at any time, so I think it would be up to an .aip plugin to get that pasteboard- I'm going to get reading that PDF now, it's been a long time coming.
Copy link to clipboard
Copied
tl;dr: Illustrator bases the measurement on some sort of "original document size" value, not the current artboard size. Does anyone know how to find this value?
Following up here, this code has been running in production for a few months now and hasn't had any origin related issues as feared, however I've discovered a pretty strange quirk. Before I get into the next phase of this discussion, here's the coffeescript that is powering it:
DOC_MEASURE = 16383
pasteboardRect = ->
artboards = activeDocument.artboards
i = artboards.getActiveArtboardIndex()
abRect = artboards.artboardRect
abWidth = abRect[2] - abRect[0]
abHeight = abRect[3] - abRect[1]
topLeft = Math.ceil(-DOC_MEASURE / 2)
centerX = (abRect[2].toFixed(5) - abRect[0].toFixed(5)) / 2
centerY = (abRect[3].toFixed(5) - abRect[1].toFixed(5)) / 2
xSubstr = if centerX < 0 then Math.floor(centerX) else Math.ceil(centerX)
ySubstr = if centerY < 0 then Math.floor(centerY) else Math.ceil(centerY)
[
topLeft + xSubstr # left
-topLeft + ySubstr # top
-((topLeft + xSubstr) - abWidth) # right
-((-topLeft + ySubstr) - abHeight) # bottom
]
The idea here is essentially:
1. Get the rect of the artboard
2. Determine the center of the artboard
3. Subtract half of the document width/height from half of the max pasteboard width/height.
This works perfectly… if you don't change the artboard size from the original value. However, if you resize the artboard, it throws the math off. This is not because the math is wrong, it's because the position appears to be tied to *the original artboard size*, not the current artboard size. I've tested this by swapping out the calculations for hard coded measurements based on the original size of the document and the guides go back to lining up.
Does anyone know if there's some store of data in Illustrator documents that record the original size of the document when it was created? One would think that since this is part of the calculation, it would also be part of the APIs, but I've come to always assume the worst.
Copy link to clipboard
Copied
Well, the code from akinuri above seems to not have this issue. Pixxxel's does however.
In fact, the only time akinuri's code fails is in my above outlined scenario where the user changes their global ruler origin - changing artboard size, position, or artboard origin are ok.
Otherwise, about your question - it may be possible - as long as the document has been saved, to glean the preset's name from the XMP data.
When I open a saved document and go to File > File Info, in the "Basic" section, there I can see the preset's name.
Problem is, not only does it have to be saved, but files that are created with any other way besides New File dialog, will have this field blank. These could be files generated by other programs or image files opened with AI too.
Copy link to clipboard
Copied
Hello all!
You know, when rearrange artboards, it always center them in the Canvas. So I get this.(the sample code only deal with document with one artboard)
var doc = activeDocument,
rect = doc.artboards[0].artboardRect,
rect2;
doc.artboards.add([0, 0, 100, -100]);
doc.artboards.add([0, 0, 1, -1]);
doc.artboards[0].remove();
doc.rearrangeArtboards(DocumentArtboardLayout.Row, 1, 0, false);
rect2 = doc.artboards[0].artboardRect;
doc.pathItems.rectangle(rect2[1] - 50 + 16383 / 2 + 24.5, rect2[0] + 50 - 16383 / 2 - 24.5, 16383, 16383).guides = true;
doc.artboards.add(rect);
doc.artboards[0].remove();
doc.artboards[0].remove();
Copy link to clipboard
Copied
This works! I also realize that the code from akinuri also was susceptible to this issue, but I didn't happen to test with any other document than a default Print one, for which the values from that code were built to fit.
Copy link to clipboard
Copied
I use this:
/**
* Adobe ExtendScript for Illustrator CS4+
* (c)Marat Shagiev
* 04.12.2016
* */
//@target illustrator
try {
getWorkAreaBnds();
} catch (e) {}
/**
* get the Illustrator's Document work area maximal bounds
* work in document with multiple artboards, shifted center and selected objects
*
* this method using:
* * copy-past max-size-rectangle (16383*16383 pt)
* * the Document.view properties (zoom, centerPoint, screenMode)
*
* @return{Array} workAreaBnds - Illustrator's Document work area maximal bounds
* */
function getWorkAreaBnds() {
var sel = _deselAll();
var WORK_AREA_SIZE = 16383,
d = activeDocument,
rect = d.pathItems.rectangle(0, 0, WORK_AREA_SIZE, WORK_AREA_SIZE),
workAreaBnds,
screenModeStore = d.views[0].screenMode,
zoomStore = d.views[0].zoom,
centerPntStore = d.views[0].centerPoint;
rect.stroked = false;
rect.selected = true;
cut();
d.views[0].screenMode = ScreenMode.FULLSCREEN;
d.views[0].zoom = 64;
d.views[0].zoom = 0.0313;
paste();
rect = selection[0];
workAreaBnds = rect.geometricBounds;
rect.remove();
d.views[0].zoom = zoomStore;
d.views[0].screenMode = screenModeStore;
d.views[0].centerPoint = centerPntStore;
for (var i = 0; i < sel.length; i++) {
var item = sel;
item.selected = true;
}
function _deselAll() {
var sel = selection;
for (var i = 0; i < activeDocument.layers.length; i++) {
activeDocument.layers.hasSelectedArtwork = false;
}
return sel;
}
return workAreaBnds;
}
Copy link to clipboard
Copied
Wow, that's very creative! It really works!
Copy link to clipboard
Copied
So far all of the examples are susceptible to the artboard resize issue with the exception of o-marat's version. That one works well, but unfortunately I'm not thrilled about the flash of UI that comes with it. The search continues…
Copy link to clipboard
Copied
Only tested with CS6 but it works, no matter artboards been resized or not.
var d = activeDocument,
rect,
rects = [],
abs = d.artboards,
z = d.views[0].zoom,
cp = d.views[0].centerPoint,
i;
abs.add([0, 1, 1, 0]);
abs.add([0, 1, 1, 0]);
for (i = abs.length - 3; i >= 0; i--) {
rects.push([abs.artboardRect, abs.name]);
abs.remove();
}
d.rearrangeArtboards(DocumentArtboardLayout.Row, 1, 0, false);
rect = abs[0].artboardRect;
d.pathItems.rectangle(rect[1] - 0.5 + 16383 / 2, rect[0] + 0.5 - 16383 / 2, 16383, 16383).guides = true;
for (i = rects.length - 1; i >= 0; i--) {
abs.add(rects[0]).name = rects[1];
}
abs[0].remove();
abs[0].remove();
d.views[0].zoom = z
d.views[0].centerPoint = cp;
Copy link to clipboard
Copied
Ah nice, that one does seem to work on the test file I'm using. While I generally understand what's happening with the code, I'm curious what prevents it from actually rearranging the artboards?
Copy link to clipboard
Copied
Well, let's add some comments:
Line 13: log the position and name of original artboards,
Line 14: remove them, so rearranging (Line 17) only affect 2 new added artboards,
Line 23: Re-create artboards.
Copy link to clipboard
Copied
A great puzzle of our time, solved with the means we have! Wonderful!
Copy link to clipboard
Copied
Ah, ok that makes sense now. I was wondering why the name was being set. Thanks a lot moluapple!