I'm stuck with a script and can't find a solution online, hope someone can help me.
In the place where I work, we print on the tiles. One of the main jobs consist in splitting a large image into several squares and print them on tiles like this:
Then the tiler will install the tiles by joining them to form the image. To do this he must have some indications on how to place the tiles.
We usually use a grid like the one in the battleship game, with letters on one side and numbers on the other like the red ones you see in the image (e.g. A1, A2...).
They would not be inside the image, it's just for reference.
I have put together a script to place the square images one per page of an InDesign document, now what I am looking for is insert some text under the images where it marks which tile it is (e.g. A1, C5 ...).
There is a way to manually choose a custom number for the number of horizontal tiles (e.g. 4) so that the script inserts the text A1 to A4, and then starts inserting the letter B all the way to B4, then C, D, etc. etc. until the tile images runs out?
Here is my script so far:
var Pagewidth =app.activeDocument.documentPreferences.pageWidth;
var Pageheight = app.activeDocument.documentPreferences.pageHeight;
var Imagefolder = Folder.selectDialog("Select a folder");
var Images = Imagefolder.getFiles(/.+\.(?:gif|jpe?g|eps|tiff?|psd|pdf|bmp|png)$/i);
for(var i =0; i < Images.length; i++) {
var Placed = app.activeDocument.pages.item(-1).place(Images[i]);
app.activeDocument.align(Placed[0], AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
app.activeDocument.align(Placed[0], AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
Placed[0].parent.fit(FitOptions.FRAME_TO_CONTENT);
app.activeDocument.pages.add(LocationOptions.AT_END);
}
app.activeDocument.pages.item(-1).remove();
EDIT
Just in case I'll try to be more clear, what I'm looking for is adding some text under the images on every page, like this:
I already know how to add the textframe for every image and place it under them.
What don't understand is how to add this kind of textframe content.
UPDATE
Thanks to the code provided by Yuri, I adapted it for my purposes.
Here is the final code:
var Pagewidth =app.activeDocument.documentPreferences.pageWidth;
var Pageheight = app.activeDocument.documentPreferences.pageHeight;
var Columns = prompt("Type the number of columns","");
if(!Columns){
exit();
}
if(!Number(Columns)){
alert("You can only type numbers");
exit();
}
var Imagefolder = Folder.selectDialog("Select a folder");
if(!Imagefolder){
exit();
}
var Images = Imagefolder.getFiles(/.+\.(?:gif|jpe?g|eps|tiff?|psd|pdf|bmp|png)$/i);
for(var i =0; i < Images.length; i++){
var Placed = app.activeDocument.pages.item(-1).place(Images[i]);
app.activeDocument.align(Placed[0], AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
app.activeDocument.align(Placed[0], AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
Placed[0].parent.fit(FitOptions.FRAME_TO_CONTENT);
app.activeDocument.pages.add(LocationOptions.AT_END);
}
app.activeDocument.pages.item(-1).remove();
var Lettersnumber = 26;
var Arr = [];
var Letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(var j = 0; j < Lettersnumber; j++){
for(var k = 1; k <= Columns; k++){
Arr.push(Letters[j] + k);
}
}
for(var l = 0; l < app.activeDocument.allGraphics.length; l++){
if(app.activeDocument.allGraphics[l].parentPage != null){
try{
var Subtext = app.activeDocument.allGraphics[l].parentPage.textFrames.add();
Subtext.textFramePreferences.verticalJustification = VerticalJustification.TOP_ALIGN;
Subtext.contents = Arr[l];
Subtext.paragraphs[0].appliedFont = app.fonts.item("Arial");
Subtext.paragraphs[0].pointSize = "30";
Subtext.paragraphs[0].justification = Justification.CENTER_ALIGN;
var Margin = app.activeDocument.allGraphics[l].parent.visibleBounds;
Subtext.visibleBounds = [Margin[2]+12, Margin[1], Margin[2]+4, Margin[3]];
Subtext.fit(FitOptions.FRAME_TO_CONTENT);
Subtext.fit(FitOptions.FRAME_TO_CONTENT);
Subtext.move(undefined, ["0", "12"]);
}
catch(e){
alert("The text is not enough for the number of images in the document");
break;
}
}
}
Here is the code that asks number columns and rows, makes the captions 'A1', 'A2', 'A3', 'B1', 'B2', 'B3'... etc, and put the texts on pages:
var doc = app.activeDocument;
// ask the number of columns and rows
var cols_rows = prompt('Columns Rows', '4 3');
if (cols_rows == null) exit();
cols_rows = cols_rows.match(/\d+/g);
var cols = cols_rows[0];
var rows = cols_rows[1];
// make the captions A1, A2, A3, B1, B2, B3... etc
var captions = [];
var abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (var r=0; r<rows; r++) for (var c=1; c<=cols; c++) captions.push(abc[r] + c);
// add pages if it's need
while(doc.pages.length < captions.length) doc.pages.add();
// set text frames with the captions on the pages
for (var i=0; i<captions.length; i++) {
var page = doc.pages[i];
var caption = captions[i];
var frame = page.textFrames.add();
frame.geometricBounds = [0,0,20,20];
frame.contents = caption;
frame.paragraphs[0].justification = Justification.CENTER_ALIGN;
frame.paragraphs[0].pointSize = 30;
frame.move([0,200]);
doc.align(frame, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
}
Input for the grid with 4 columns and 3 rows (A, B, C):
Results:
...
But if you have more than 26 rows it's need to decide how to mark the rows after 'Z'.
Update
Here is the full implementation that takes selected picture, ask the number of columns, rows, and the gap, sliced the picture and put every slice on the page with the caption:
var doc = app.activeDocument;
if (app.selection.length == 0) { alert('Nothing selected'); exit() }
var sel = app.selection[0];
// ask the number of columns and rows
var input = prompt('Columns, Rows, Gap', '4 3 2');
if (input == null) exit();
input = input.match(/\d+/g);
var cols = +input[0];
var rows = +input[1];
var gap = +input[2];
// calculate tile width and height
var sel_gb = sel.geometricBounds;
var sel_w = sel_gb[3] - sel_gb[1];
var sel_h = sel_gb[2] - sel_gb[0];
var w = (sel_w - (cols-1) * gap) / cols; // tile width
var h = (sel_h - (rows-1) * gap) / rows; // tile height
// make the tiles
var tiles = [];
var gb = sel_gb.slice(0);
gb[2] = gb[0] + h;
gb[3] = gb[1] + w;
for (var r=0; r<rows; r++) {
for (var c=0; c<cols; c++) {
var tile = sel.duplicate();
tiles.push(tile);
tile.geometricBounds = gb;
gb[1] = gb[3] + +gap;
gb[3] = gb[1] + w;
}
gb[1] = sel_gb[1];
gb[3] = gb[1] + w;
gb[0] = gb[2] + gap;
gb[2] = gb[0] + h;
}
sel.remove();
// make the captions A1, A2, A3, B1, B2, B3... etc
var captions = [];
var abc = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (var r=0; r<rows; r++) for (var c=1; c<=cols; c++) captions.push(abc[r] + c);
// add pages if it's need
while(doc.pages.length <= captions.length) doc.pages.add();
// set text frames with the captions on the pages
for (var i=0; i<captions.length; i++) {
var page = doc.pages[i+1];
// get a tile and copy the tile on the page
var tile = tiles[i].duplicate(page);
doc.align(tile, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
doc.align(tile, AlignOptions.VERTICAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
// get a caption and put the caption on the page
var caption = captions[i];
var frame = page.textFrames.add();
frame.geometricBounds = [0,0,20,20];
frame.contents = caption;
frame.paragraphs[0].justification = Justification.CENTER_ALIGN;
frame.paragraphs[0].pointSize = 30;
frame.move([0, tile.geometricBounds[2] + 20]);
doc.align(frame, AlignOptions.HORIZONTAL_CENTERS, AlignDistributeBounds.PAGE_BOUNDS);
}
Input:
Output:
The row 'A B C D ...' and the column '1 2 3 4 ...' on the first page can be added with a script as well. Let me know if you need it.