15 InDesign Scripts and Snippets You May Have Missed
February 21, 2023 | Snippets | en | fr
If you visit this website from time to time, you know that it contains a plethora of free InDesign scripts, but you may not know that other cool nuggets are regularly open-sourced in our GitHub repository called IdGoodies. The latter being hardly documented, here is an overview of some scripts—and interesting code snippets—you will find there…
1. BookUpdateTOCs (script)
Updates all Tables of Contents in the active Book. — You may find convenient to batch-update all TOCs present in a book, since InDesign doesn't seem to provide such a feature. This script inspects every chapter and runs the Update-Table-Of-Contents command if available. Then it displays a basic report at the end of the process.
TECH NOTE. — Chapters that aren't presently visible are temporarily opened, saved, and closed.
2. DocPreventOpening (script)
Prohibits the opening of certain InDesign documents based on their filename. — This script prevents the opening of documents having a certain filename pattern. Change the RE_EXCLUDE
constant in the code to make it fit your needs. The implementation relies on the event beforeOpen
available in CS5/CS6/CC.
• To install the event listener just execute the script manually (once).
• To uninstall the listener, close your InDesign session. Of course if you have installed the code as a “startup script”, remove it from its folder!
DISCLAIMER. - Do not install this script as a startup script unless you really know what you are doing. Users will be unable to open documents having a certain file name pattern and won't be notified. In case you deploy this solution, make sure your clients are informed. Always provide instructions for uninstalling!
3. DocShowClosestByName (script)
Makes active the closest document (with respect to name). — Based on the discussion “Javascript to find a similar filename among all open documents”, this script automatically selects the document having the most similar filename based on the Levenshtein distance.
TECH NOTE. — A custom RegExp can be supplied to the showClosestDocByName
function in order to filter out specific name parts that must be ignored anyway (typically, shared suffixes among your set of documents.)
4. LinkAutoUpdate (script)
Updates links dynamically. — This script automatically updates ‘out-of-date’ links in InDesign. It is an event listener. You can either run it manually during your session, or load it as a startup script
.
TECH NOTE. — An alternate code exists based on a script from あるふぁ(仮) which listens to the event afterAttributeChanged
and delegates the task to BridgeTalk. The present implementation relies on the event afterLinksChanged
available in CS5/CS6/CC.
5. ReframeAllPages (script)
Reframes all pages with respect to item bounds. — Considering the active document, this script scans the page items of each page, determines the overall bounding box and reframe the page so it exactly fits the area. Page margins are reset to zero too.
TECH NOTE. — The script is undo-able and displays a short report.
6. SelApplyStyleNone (script)
Resets the selection to the [None] style. — This script forcibly applies the [None]
character style to the selected text or insertion point (if any), making sure that all specific character attributes are removed. The code has the same effect than clicking [None]
in the Character Style panel (which unfortunately cannot be easily associated to a KB shortcut despite countless InDesign user requests.) So, you can attach a KB shortcut to this script and have a manual control of the “Apply None” action.
TECH NOTE. — All InDesign versions should be supported from CS4 to CC. Also, the script is undo-able (Cmd Z.)
7. SelPlacePDFPage (script)
Simple interface for changing placed PDF page. — Need to constantly change the page of a placed PDF in the active document? Just select the graphic container and run this script.
ORIGINAL DISCUSSION: adobe.ly/3HoxrZ2
8. DocBackupSave (snippet)
Backups a document before saving. — This function creates a backup of the target document before it saves it, so the consecutive versions are memorized, timestamped, and remain safe in a subfolder. The first time you call the function on a fresh, not-saved document, it simply invokes the native save
method. Then, any subsequent call copies the existing document file in a backup folder before saving the new version. Each backup file is named as follows,
<YYYY>-<MM>-<DD>_<hhmmss>_<DocumentFileName>
where the timestamp <YYYY>-<MM>-<DD>_<hhmmss>
reflects the modified date/time of the document version.
Using backupSave(myDoc)
rather than myDoc.save()
may protect you against doomsday scenarios where a huge, in-progress document gets suddenly corrupted and cannot be recovered from the regular autorecovery feature. (Besides that, having consistent backups is still useful for ‘historizing’ your projects...)
TIP. — Assign your backupSave();
script a KB shortcut, e.g. Cmd+Alt+S, going into Edit > Keyboard Shortcuts…
9. DocGrepTextFindAll (snippet)
Gets all Grep/Text found items in a temp file. — This function lists all text items found by the active GREP query (or TEXT query if no GREP available.) Results are saved in a UTF8-encoded file (typically findGrep.txt
) then shown to the user. The function itself returns true
if all went fine.
TIP. — Assign your showFindAll();
script a KB shortcut, e.g Cmd+Alt+F, going into Edit > Keyboard Shortcuts...
10. FontGlyphCount (snippet)
Fast Glyph Counter for InDesign Font. — This snippet adds a glyphCount
method to Font
instances. You can use it in scripts that need to know how many glyphs are available in a font, in particular those which handle GID.
11. GroupAddItems (snippet)
Adds one or several PageItems to a Group. — This snippet provides a addItems
method to any singular Group instance. Such a feature is not available in InDesign's scripting API and it is hard to implement in a way that does not temporarily destroy the target group. The present solution relies on a brilliant idea proposed by Uwe Laubender — originally discussed in the InDesign Scripting Forum.
TECH NOTE. — The trick is to create a temporary MultiStateObject
(MSO) which can then handle the target group into a State. New items are added to that state. When the state is finally released, the original group is restored with its new components. Issues may still arise in edge cases. The present code should deal with the target group G being part of an AnchoredObject (AO), or inside a ‘nested item’ (i.e pasted into.) However, no solution has been found in the particular case of G being directly nested in a SplineItem
.
12. SelFirstImage (snippet)
Gets the top-left-most graphic. — Given a target page (and optionally a target layer) this snippet identifies and returns the “top-left-most” SplineItem containing a Graphic object. By ‘top-left-most’ we mean the object whose (top+left) coordinate is minimal. On average, this determines what the user perceives as the first geometric container of the page.
13. SelFlattenTransform (snippet)
Flattens the transformations of the selection. — This snippet operates on the selection. It clears the transformation attributes of every SplineItem while keeping path points in place. At the end of the process the objects look the same but they no longer undergo any scaling/rotation/shear component.
TECH NOTE. — The present implementation does not scan inner items from groups, MSOs, etc.
14. TableCellBox (snippet)
Creates a box enclosing a cell (or cell range). — Takes a table cell (single or plural specifier) and creates a Rectangle object that encloses the area, with respect to cell stroke(s) and existing transformations. This snippet is a 'proof of concept' following this technical discussion. The code illustrates and extends an idea from Uwe Laubender.
TECH NOTE. — The implementation deals with various InDesign issues, in particular the obj.duplicate()
bug that affects GraphicCell's inner object. Also, the code supports the case of MSO
or Button
entities in the graphic cell. It can generate multiple ‘boxes’ if the incoming cell range runs across different spreads (due to threaded frames.)
15. TextSortParagraphs (snippet)
Fast sort of InDesign paragraphs. — This snippet re-orders a range of paragraphs according to a sort function. By default Array.prototype.sort
is applied to the contents, but the client code can supply a custom function as 2nd argument.