InDesign scripting : lesson 32

there’s quite a farcical discussion going on over at the InDesign forum — Is there really no way to export PDFs as separate pages? as is so often the case in these forums, a person asks HOW to do something and then there’s an extended, often heated, discussion about WHY you would/wouldn’t or should/shouldn’t do it that way.

but the argy-bargy is unnecessary because there are some truly legitimate workflows that would benefit from this capability — like the guy at comment 39 — and because exporting each InDesign page as a separate PDF is really easy with Applescript.

today we’ll be looking at a simplified version of the script from lessons 11 and 12. we start by collecting the filename and file path of the active document. then we shorten the filename to exclude the extension — so “this file.indd” becomes “this file” (mgShortName):

tell application id "com.adobe.InDesign"
  set mgFolder to file path of active document
  set mgName to name of active document
  set text item delimiters of AppleScript to {"."}
  set mgShortName to text item 1 of mgName
  set text item delimiters of AppleScript to ""
end tell

next we’ll create a subfolder for the pdfs to be saved into. you do this through the finder by first checking if the subfolder already exists and, if not, creating it. we’re calling this subfolder “the pdfs” and it will be created in the same folder as the indesign file :

tell application "Finder"
  if (exists folder "the pdfs" of folder mgFolder) is false then
    make folder at mgFolder with properties {name:"the pdfs"}
  end if
end tell

then it’s just a matter of a simple repeat loop, cycling through each page in the document. we’re using the page number (name of page) as part of the filename so, using the example above, we’ll end up with “this file_1.pdf”, “this file_2.pdf”, etc. and we complete the script with a simple dialog to let us know when it has done its thing :

tell application id "com.adobe.InDesign"
  repeat with x from 1 to count pages of active document
    set mgPageName to name of page x of active document
    set page range of PDF export preferences to mgPageName
    set mgFilePath to mgFolder & "the pdfs:" & mgShortName & "_" & mgPageName & ".pdf" as string
    tell active document
      export format PDF type to mgFilePath using "macgrunt offset" without showing options
    end tell
  end repeat
  display dialog "ALL DONE!" buttons {"OK"} default button 1 giving up after 2
end tell

notice that when we specify mgFilepath we add a colon to “the pdfs” — this is what designates it as a folder. the full file path might be something like this “Macintosh HD:Users:macgrunt:Documents:the pdfs:this file_1.pdf” — this shows a hierarchy of five folders — everything after that last colon is the name of the file. so, if we didn’t add the colon after “the pdfs” the filename would have been “the pdfsthis file_1.pdf” (hopefully that makes sense).
note also that “macgrunt offset” is the name of the PDF export preset — you would need to substitute your own preset name here.

so that’s the basic functionality for exporting every page as a separate PDF. copy those three sections into a single script in applescript editor and you’re away. there are a bunch of macgrunt lessons showing other PDF export workflows and considerations — just enter “export pdf” up there in the search field.

the next lesson will look at a couple of other, more complex, ways we could approach the naming of these separate page PDFs.

until then, keep grunting.

macgrunt icon

Advertisements

InDesign tip : #31

the last couple of scripting lessons (#30 & #31) showed a couple of examples of how to use applescript to assist with reformatting an existing table. but if you are setting up your own tables you’d use table and cell styles to make your life a little easier.

as with paragraph, character and object styles — if you are going to use particular formatting settings more than once in a document, it’s smart to create a style for those settings. so, if your document has several tables with similar formatting, you’d create a table style. this example only has one table, so we’ll just look at cell styles.

when we first change our text to a table it will look something like this :
screen grab of unformatted table

then we start to create the table formatting we want :
screen grab of partially formatted table

to create the cell style for the reversed row, highlight that row and then choose “New Cell Style…” from the flyout menu on the cell styles panel (windows > styles > cell styles) :
screen grab of cell styles panel flyout menu

you can also access this functionality directly from your control panel :
screen grab of cell styles menu in control panel

the new style takes on the formatting of the selected cells. we’re calling that style ‘header row’ and you can see that, as well as the colour of the cells, the cell style will also apply a paragraph style to the text in those cells. that is, the paragraph style does not need to be applied separately — it’s part of the cell style :
table15

then repeat the process for each different cell type. for this table we’ve created three different cell styles :
screen grab of new styles in cell styles panel

the easiest way to proceed from here is to first apply the most common style to the entire table. to select every cell in a table, first place your cursor somewhere within the table and then move your mouse to the top left corner of the table until you see a downward pointing arrow, then click.

here we’ve applied the ‘basic row’ style to the entire table by clicking that style in the cell styles panel (or from the control panel dropdown) :
screen grab showing entire table with bsaic row style applied

then go through and apply the other styles to their relevant cells (here we’re doing entire rows in the same style) :
screen grab of table with complete formatting

now here’s why you’ve gone to all that trouble … when the client comes back and says they want all the reversed rows to be thicker and all the blank rows to be thinner (or any of a gazillion other possible alterations), you don’t have to change each row individually (or use applescript) you just change the cell styles. unfortunately InDesign does not yet allow you to specify a specific cell height in the cell style (who knows why), but you CAN change the spacing — or cell insets. here we’re changing the top and bottom insets for the ‘header row’ style from 1mm to 2mm :
screen grab showing cell style options for the header row style

and like magic the entire table is updated automatically :
table13

so, if you’re working with tables but not using cell styles, you’re probably working way too hard.

macgrunt icon

InDesign scripting : lesson 31

in lesson 30 we looked at an applescript to change rows of a particular height to a new height throughout an entire InDesign table. here’s an example of slightly more complex table formatting.

no doubt you would use table and cell styles when setting up your own tables — to assist with formatting changes further down the track. but sometimes you need to work on tables that other, less diligent InDesign users have set up. and if you’re doing that on a regular basis, or if the tables are extensive, it makes sense to automate the tedium.

we’ll look again at the table from the last lesson :
screen grab of table before formatting

in this table, all cells have the same settings :
screen grab of control panel showing table settings

but now we want the reversed row heights to be a little wider and the blank rows to be narrower. first we need to change all the rows to a specific height before working on the other rows. to do this we change the ‘at least’ specifier to ‘exactly’ by changing auto grow to false :

tell application id "com.adobe.InDesign"
  tell active document
    tell table 1 of selection
      set auto grow of every row to false
      set height of every row to 5
      -- rest of script to go here
    end tell
  end tell
end tell

then we can use the cell fill colour to address the reversed rows only :

set height of (every row whose name of fill color is "C=0 M=100 Y=100 K=50") to 6.5

we need to use a different tactic for the blank rows. here’s one way — address every row that comes before a reversed row :

repeat with x in (every row whose name of fill color is "C=0 M=100 Y=100 K=50")
  set mgName to name of x
  try
    set height of row (mgName - 1) to 2
  end try
end repeat

the ‘name’ of a row is its number — so the first row in a table is row 1. that’s why we need the try statement — the first reversed row is row 1 and there is no row 0 to change to a height of 2 — so the script would throw an error without that try.

if we run that script the table will look like this :
screen grab of table after initial script run

the only remaining problem, as the observant will notice, is that there is at least one cell (and probably more) which is now too small and has overset text. an extra line in the script will fix that :

set height of every cell whose overflows is true to 9

the compiled script is short and simple :
screen grab of compiled script

… and the completed table looks like this :
screen grab of completed table

this is just a taster for what can be done with table formatting through applescript. whether or not you end up using scripting with your own tables depends on your workflow, how big your tables are, how often you work with tables, etc, etc. sometimes table are just too small to justify the time it takes to write or edit a script. but table scripting could be another powerful tool in your production arsenal — anything to minimise the monkey-work.

macgrunt icon

InDesign scripting : lesson 30

here’s a little quickie script to help those of you who like to specify exact row heights in your InDesign tables. changing column widths is easy, but changing row heights throughout a table is a pita.

let’s say you want to change all the reversed rows in a table like this, from 9mm high to 6mm high :
screen grab of original table

ordinarily you’d have to select each row, one after another, and set it to the new height manually, but with applescript we can automate it.

first we’ll create a dialog for the user to enter row heights into :

tell application id "com.adobe.InDesign"
  
  set mgDialog to make dialog
  tell mgDialog
    tell (make dialog column)
      tell (make dialog row)
        make static text with properties {static label:"Enter row height you'd like to change."}
      end tell
      tell (make dialog row)
        set mgOldHeightField to make text editbox with properties {edit contents:"current row height", min width:250}
      end tell
      tell (make dialog row)
        make static text with properties {static label:""}
      end tell
      tell (make dialog row)
        make static text with properties {static label:"Enter height you'd like it changed to."}
      end tell
      tell (make dialog row)
        set mgNewHeightField to make text editbox with properties {edit contents:"new row height", min width:250}
      end tell
      tell (make dialog row)
        make static text with properties {static label:""}
      end tell
    end tell
  end tell
  
  set mgResult to show mgDialog

  if mgResult is true then
    set mgOldHeight to edit contents of mgOldHeightField as number
    set mgNewHeight to edit contents of mgNewHeightField as number
    destroy mgDialog
  else
    error number -128
    destroy mgDialog
  end if

  -- rest of script to go here
  
end tell

first the specifications for the dialog are created, then the dialog is displayed to the user, then the results are captured into two variables — mgOldHeight and mgNewHeight. notice these variables are forced into numbers — for obvious reasons — so, if the user enters non-numerical data the script throws an error.

the resulting dialog looks like this :
screen grab of generated dialog

the rest of the script is simple as :

  tell active document
    tell table 1 of selection
      set height of (every row whose height is mgOldHeight) to mgNewHeight
    end tell
  end tell

here you’ll notice we’re addressing “table 1 of selection” which means that, for the script to work in this form, the first text frame containing the table must be selected (as shown in the screen grab above). you can also have all the text frames selected, but if you have nothing selected, or only the second frame selected, you’ll get an error.

that’s it. run the script, fill in your details, and all matching rows throughout the table will be changed almost instantaneously :
screen grab of filled dialog

screen grab of altered table

of course there are other ways to write the script — so that no selection is necessary, or every table in the document is addressed, or whatever — this is just the simplest form for this functionality.

the next scripting lesson will look a bit more at formatting InDesign tables with applescript.

til then, keep grunting.

macgrunt icon

InDesign tip : #30

adding automatic page numbering to your InDesign document is easy. just create a text frame on your master page and then hit cmnd-opt-shift-N or choose this from your Type menu :
screen grab of insert special character flyout menu
you can style the page number the same way as you would any other text. if you type in “page ” before the page number, your master page will look like this :
screen grab of master page with basic page numbering
and your document page looks like this :
screen grab of document page with basic page numbering
but you probably already knew that.

you’ll notice that in the insert special character flyout menu there are a few other markers including one called ‘section marker’ which we’ll talk about in a moment. but there’s no marker for ‘total pages’ — so how do we get automatic numbering in this format? :
screen grab of extended page numbering on document page

well, it’s a bit of a mystery why adobe decided to put that particular functionality in a different place. but here’s where you’ll find that second special character from the Type menu :
screen grab of text variables flyout menu

ok, now, you can also add a ‘section marker’ from the insert special character flyout menu. on the master page it looks like this :
screen grab of master page showing complete numbering line
just to make things clear, that’s : {section marker}+” ~ page “+{current page number marker}+” of “+{last page number text variable}

then you need to specify what you want the section marker to say — under your numbering and section options :
screen grab of numbering and section options

which will render your document page thus :
screen grab of document page showing complete numbering line

so far so good.

unfortunately…
the last page number text variable is just a tad clunky — even in CS6. so far we’ve been looking at page two of this document :
screen grab of pages panel showing six pages in two sections
six pages with a two-page section followed by a four-page section. and the text variable has been used with these options :
screen grab of last page number text variable options
notice that the scope is set to ‘section’. so we get ‘2 of 2’ because there are only two pages in the section.

but, if we change the scope to ‘document’ we don’t get ‘2 of 6’ :
screen grab of page numbering showing '2 of 4'

why? because the text variable is for ‘last page number’ (4) not ‘total page count’ (6). your challenge is to come up with even one scenario where you’d want to number your pages in that fashion.

you’d have to agree that that’s pretty crap. at the very least, InDesign should offer the option to use absolute page numbering for that text variable — maybe one day.

macgrunt icon