Copy link to clipboard
Copied
I am wanting to scan all textFrames on a layer "Active Text" and have it reference what "grid location" it is in. The grid locations are textFrames on a layer "42 Pg Grid Location".
Here is an example:
I want to know that "Sample text 1" is in "J16" and "Sample text 2" is in "I15"
Here is a sample file for how big the art board is with the grid locations....
Find Text Grid Location.pdf - Google Drive
I'm using Illustrator CS4 and javascript. Any help would be greatly appreciated!!
OK, let's get all done by script.
...(function() {
var d = activeDocument,
ps = d.layers['42 Pg Grid Location'].pathItems,
l = ps.length,
o = 'ABCDEFGHIJKLMNOPQRST...',
W, H,
rows = columns = 1,
origin = d.rulerOrigin,
newOrigin = new Array(2),
t, b, center, gx, gy,
i = 0;
for (; i < l; i++) {
if (ps.width == 0) {
columns++;
!newOrigin[1] && newOrigin[1] = origin[1] + ps.geometricBounds[3];
!H && H = ps.height;
}
if (ps.height == 0) {
row
Copy link to clipboard
Copied
Please select a textframe and test.
it use top and left position to compare, you can change to use center point of bounds.
var W = 226.572,
H = 230.56,
d = activeDocument,
o = 'ABCDEFGHIJ',
t = app.selection[0],
gx, gy;
// this is to make sure the bottom right point of total grids to [0, 0],
// you can do it manally by change to global ruler and drag and set origin to that point.
d.rulerOrigin = [3817.3408203125, 2520 - 2415.908203125];
gx = Math.abs(t.left)/W;
gy = Math.abs(t.top)/H;
alert('' + o[Math.ceil(gy-1)] + Math.ceil(gx));
Copy link to clipboard
Copied
This will be PERFECT! Thanks so much!!!
So can you tell me how you came up with the W and H? I have different grid sizes that I will need to apply this to as well.
Copy link to clipboard
Copied
OK, let's get all done by script.
(function() {
var d = activeDocument,
ps = d.layers['42 Pg Grid Location'].pathItems,
l = ps.length,
o = 'ABCDEFGHIJKLMNOPQRST...',
W, H,
rows = columns = 1,
origin = d.rulerOrigin,
newOrigin = new Array(2),
t, b, center, gx, gy,
i = 0;
for (; i < l; i++) {
if (ps.width == 0) {
columns++;
!newOrigin[1] && newOrigin[1] = origin[1] + ps.geometricBounds[3];
!H && H = ps.height;
}
if (ps.height == 0) {
rows++;
!newOrigin[0] && newOrigin[0] = origin[0] + ps.geometricBounds[2];
!W && W = ps.width;
}
}
d.rulerOrigin = newOrigin;
W = W / columns;
H = H / rows;
t = app.selection[0];
b = t.geometricBounds;
center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];
gx = Math.abs(center[0]) / W;
gy = Math.abs(center[1]) / H;
d.rulerOrigin = origin;
alert('' + o[Math.ceil(gy - 1)] + Math.ceil(gx));
})()
Copy link to clipboard
Copied
moluapple is there a way to make this work on any layer name with a substring of "Location"?
so for this line.....
ps = d.layers['42 Pg Grid Location'].pathItems,
I am looking for something like....
ps = d.layers.[substr(-8)"Location"].pathItems,
I understand that my code isn't written correctly but that is just to try to explain what I am looking for.
This script will be run on different files with layer names
42 Pg Grid Location
36 Pg Grid Location
30 Pg Grid Location
24 Pg Grid Location
16 Pg Grid Location
8 Pg Grid Location
4 Pg Grid Location
Copy link to clipboard
Copied
The entirety of the moluapple's script can be given a name so as to make it a named function, and placed into such a loop where the line
ps = d.layers['42 Pg Grid Location'].pathItems,
is replaced by an argument passed into the function.
ps = d.layers[layerNameArgument].pathItems,
function newMainScript(){
var doc = app.activeDocument;
var thisLayer;
for(var I=0; I<doc.layers.length; I++){
thisLayer = doc.layers;
if(thisLayer.name.match("Location")){
moluapple's script(thisLayer.name);
}
}
}
newMainScript();
Copy link to clipboard
Copied
Silly-V thanks for your response! Sorry but I don't quite follow. Here is the full version of my code that I have modified from moluapple's script.....
#target illustrator
function findGrid() {
var d = activeDocument,
ps = d.layers['42 Pg Grid Location'].pathItems,
l = ps.length,
o = 'ABCDEFGHIJKLMNOPQRST...',
W, H,
rows = columns = 1,
origin = d.rulerOrigin,
/*newOrigin = new Array(2),*/
newOrigin = new Array(0, 0),
t, b, center, gx, gy,
i = 0;
for (; i < l; i++) {
if (ps.width == 0) {
columns++;
!newOrigin[1] && newOrigin[1] = origin[1] + ps.geometricBounds[3];
!H && H = ps.height;
}
if (ps.height == 0) {
rows++;
!newOrigin[0] && newOrigin[0] = origin[0] + ps.geometricBounds[2];
!W && W = ps.width;
}
}
d.rulerOrigin = newOrigin;
W = W / columns;
H = H / rows;
t = app.selection
; b = t.geometricBounds;
center = [b[0] + (b[2] - b[0]) / 2, b[1] + (b[3] - b[1]) / 2];
gx = Math.abs(center[0]) / W;
gy = Math.abs(center[1]) / H;
d.rulerOrigin = origin;
xCoor = -50;
yCoor = 0;
createGridText = d.textFrames.add();
createGridText.position = [xCoor, yCoor = (inc -= 20)];
createGridText.contents = '' + o[Math.ceil(gy - 1)] + "-" + Math.ceil(gx);
}
inc = 0;
incTwo = 0;
d = activeDocument;
for (var z = 0; z < selection.length; z++) {
xC = -350;
yC = 0;
createComponentText = d.textFrames.add();
createComponentText.position = [xC, yC = (incTwo -= 20)];
createComponentText.contents = selection
.contents; findGrid();
}