Automating Adobe InDesign 2017


Script an InDesign Document Book Cover

Thursday, November 23, 2017

Have a Wonderful Thanksgiving

Just a short blog before I start working in the kitchen. It's been a little hectic lately as I have been finalizing the work on a fairly substantial book. It was one of those headaches where the authors worked in Microsoft Word but had little understanding of what styling and working with images was all about.

it is all done but the shouting! And a lot of lessons were learned in the process. To help with the process I used a number of scripts and developed several more. Scripts used were :

  • DocFromTemplate_Word - Document from template with Word file import
  • PlaceAndAnchor - Place image inline with anchoring and object style
  • PlaceAndAnchor_left - Place image inline with anchoring and object style, plus text frame to right for caption grouped with image

Since this last one is still on my mind, I will offer what I came up with in the blog post for this week.

The hardest part of this script would be in creating the user dialog. I will leave that up to you. Of course, if you are the only one using the script, leave it with the variables defined at the top and just change values as needed.

Even if you never create a book cover with a spine, you may get some ideas for a similar project that requires more than one page having different widths to be combined as a spread.

CreateCover Script

The width of the spine for a book is calculated based on the paper thickness and the number of pages in the book. The paper thicknesses shown in the script are from CreateSpace (an Amazon Company). Measurements were all in inches but converted to points as InDesign's resize method requires points for measures.

AppleScript

(*Paper thickness in inches: white book paper = 0.002252; 
cream = .0025; color .002347 *)
set psetName to "6x9Book"
set pThick to 0.002252
set sMargin to 0.875 --spine margins in inches
set bldWid to 0.125 --bleed width in inches
set pgMargins to {0, 0, 0, 0}
set bkWid to 6 --book width
set bkHgt to 9 --book height 
set pageCount to 440
--bar code box
set stWeight to 0 --stroke weight
set stColor to "None" --stroke color
set fColor to "Paper" --fill color
set fTint to 1 --fill tint
set bcOf to 18 --bar code offset from bottom in points
set bcBoxHgt to 86.4 --bar code box height
set bcBoxWid to 144 --bar code box width
--calculations
set pageHgt to bkHgt * 72
set pageWid to bkWid * 72
set bleedWid to bldWid * 72
set spineMargin to sMargin * 72
set spineWid to (pageCount * pThick) * 72
set y0 to pageHgt - (bcBoxHgt + bcOf)
set x0 to pageWid - (bcBoxWid + bcOf)
set gBounds to {y0, x0, pageHgt - bcOf, pageWid - bcOf}
set propRecord to {fill Color:fColor, fill Tint:fTint, stroke weight:stWeight, stroke color:stColor, geometric bounds:gBounds}
tell application "Adobe InDesign CC 2017"
    set measurement unit of script preferences to points
    if not (exists document preset psetName) then
	set presetRef to make document preset with properties {name:psetName}
    else
	set presetRef to document preset psetName
    end if
    tell presetRef
	set create primary text frame to false
	set intent to print intent
	set document bleed top offset to bleedWid
	set document bleed uniform size to true
	set column count to 1
        copy pgMargins to {top, left, bottom, right}
	set pages per document to 1
	set page height to pageHgt
	set page width to pageWid
	set page orientation to portrait
	set facing pages to true
	set start page number to 1
	set slug top offset to 0
	set document slug uniform size to true
    end tell
    set docRef to make document with properties {document preset:presetRef}
    tell docRef
	set ruler origin of view preferences to page origin
	tell spread 1
	    set allow page shuffle to false
	    set centerpage to make page at end
	    make page at end
	end tell
	resize centerpage in inner coordinates from center anchor by replacing current dimensions with values {spineWid, pageHgt}
	tell centerpage
	    make guide with properties {orientation:vertical, location:spineMargin}
	    make guide with properties {orientation:vertical, location:(spineWid - spineMargin)}
	end tell
	tell page 1
	    make rectangle with properties propRecord
	end tell
    end tell
end tell

ExtendScript

//Variables
var psetName = "6x9 book";
var pThick = 0.002252; //paper thickness
var sMargin = 0.875; //spine margins in inches
var bldWid = 0.125; //bleed width in inches
var pgMargins = [0, 0, 0, 0]; //top, left, bottom, right
var bkWid = 6; //book width
var bkHgt = 9 ;//book height 
var pageCount= 440;
//bar code box
var stWeight = 0; //stroke weight
var stColor = "None"; //stroke color
var fColor = "Paper"; //fill color
var fTint = 1; //fill tint
var bcOf = 18; //bar code offset from bottom in points
var bcBoxHgt = 86.4; //bar code box height
var bcBoxWid = 144; //bar code box width
//Calculations
var pageHgt = bkHgt * 72;
var pageWid =  bkWid * 72;
var bleedWid =  bldWid * 72;
var spineMargin =  sMargin * 72;
var spineWid = (pageCount * pThick) * 72;
var by0 =  pageHgt - (bcBoxHgt + bcOf);
var bx0 =  pageWid - (bcBoxWid + bcOf);
var gBounds =  [by0, bx0, pageHgt - bcOf, pageWid - bcOf];
var presetRef;

app.scriptPreferences.measurementUnit = MeasurementUnits.points;
if(app.documentPresets.item(psetName).isValid){
         presetRef = app.documentPresets.itemByName(psetName);
 } else {
       presetRef = app.documentPresets.add({name:psetName}) ;
        presetRef.intent = DocumentIntentOptions.PRINT_INTENT;
        presetRef.documentBleedTopOffset = bleedWid;
	presetRef.documentBleedUniformSize = true;
	presetRef.columnCount = 1;
	presetRef.top = pgMargins[0];
	presetRef.left = pgMargins[1];
	presetRef.bottom = pgMargins[2];
	presetRef.right = pgMargins[3];
	presetRef.pagesPerDocument =1;
	presetRef.pageHeight = pageHgt;
	presetRef.pageWidth = pageWid;
	presetRef.pageOrientation = PageOrientation.portrait;
	presetRef.facingPages = true;
	presetRef.startPageNumber = 1;
	presetRef.slugTopOffset = 0;
	presetRef.documentSlugUniformSize = true;
}
var docRef = app.documents.add (true, presetRef);
docRef.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;
var pageRef = docRef.pages[0];
var spreadRef = docRef.spreads[0];
var docPref = docRef.documentPreferences;
docPref.allowPageShuffle = false;
spreadRef.allowPageShuffle = false;
var centerPage = docRef.pages.add(LocationOptions.AT_END);
var thirdPage = docRef.pages.add(LocationOptions.AT_END);
centerPage.resize(
      CoordinateSpaces.innerCoordinates, 
      AnchorPoint.centerAnchor, 
      ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, 
      [spineWid, pageHgt]
);
centerPage.guides.add(undefined, {orientation:HorizontalOrVertical.VERTICAL, location:spineMargin});
centerPage.guides.add(undefined, {orientation:HorizontalOrVertical.VERTICAL, location:(spineWid - spineMargin)});
var myRectangle = pageRef.rectangles.add({geometricBounds:gBounds})
myRectangle.properties = {fillColor:fColor, strokeColor:stColor, strokeWeight:stWeight, fillTint:fTint};

The big job here is entering in all of the values for the variables. With a nicely designed custom dialog box, this could be a very valuable script, especially if you create book covers.


Disclaimer: Code samples are to be used as a guide for users to create their own real world scripts and is provided "AS IS". No representation is made as to the accuracy or completeness of the code. Use of the code and its operation is at the discretion and responsibility of the user.

Trackback Link
http://www.yourscriptdoctor.com/BlogRetrieve.aspx?BlogID=18329&PostID=1510760&A=Trackback
Trackbacks
Post has no trackbacks.

Recent Posts


Tags


Archive