The following script is a short and sweet way of moving all text in an InDesign anchored object out of its little cage and into the main text flow of the document.
If you have an InDesign document peppered with anchored objects containing text (perhaps some sort of subhead system) and you want to move all that text back into the main flow of the InDesign document and delete the anchored object, this script does that.
The script will move all text out of its anchored object, and, keeping formatting intact, insert it just before the anchored object marker in the main text flow containing the anchored object. It will then delete the anchored object itself.
Instructions
Always back up your document first!
1. Set up a GREP find/change query that will find all the text you are interested in “uncaging”. (This is done in InDesign’s Find/Change window, under the GREP tab; if you are not familiar with GREP, this is a good opportunity to become so – it’s very powerful and an essential tool for any InDesign user.)
2. Once the GREP search is set up as you like, run the script. (You may close the Find/Change window. You do not need to click on Find Next or Find All – it is sufficient just to set up the GREP query.)
Finito! The script will find all text in the document that matches the GREP search. If that text is in an anchored object, the script will then move all the text in the anchored object out of it’s box and into the main text flow, and the script will then delete the anchored object itself.
Here’s the script:
//BS"D //Anchored Text Uncaged //An InDesign script Copyright (c) Bookraft LLC, 2015 //Another freebie script from www.Id-Extras.com // This code is released under the GNU Public License (GPL) main(); function main(){ var d, fs, i, f, p; d = app.activeDocument; fs = d.findGrep(true); for (i = 0; i < fs.length; i++){ f = fs[i]; if ((p = f.parentTextFrames[0].parent) instanceof Character){ f.parent.texts.everyItem().move(LocationOptions.BEFORE, p); p.contents = ""; } } }
If you need this script expanded or developed to meet your needs please get in touch to request a quote!
Questions and comments welcome below.
Alex
July 7, 2022 11:00 amThanks for posting this very useful script. However, I cannot get it to work. My grep query finds the text I want to uncage (page numbers which were anchored in a word document), but the script returns the following error:
Javascript Error!
Error Number: 21
Error String: undefined is not an object
Engine: main
File: xxxxx… AnchoredTextUnchained.jsx
Line: 15
Source: if ((p = f.parentTextFrames[0].parent) instanceof Character){
Ariel
July 7, 2022 11:34 pmThat error often happens if there’s overset text. Check whether eliminating any overset text solves the problem.
profeivan
April 5, 2022 9:51 pmWhat a wonderful script! It saved me hours of tedius work. Thanks for sharing
Boris
August 29, 2015 11:10 pmThanks, now I understood.
( If Interested , see Script create_anchored_frames_from selected_text, http://adobeindesign.ru/wp-content/uploads/create_anchored_frames_from-selected_text.zip + http://adobeindesign.ru/?p=10956 )
Admin
August 29, 2015 9:25 pmHow did you set up the GREP find?
Boris
August 28, 2015 11:43 pmJavaScript Error
Error String:Object contains no text for Find/Change
Source: fs = d.findGrep(true);
(InDesign CC2015)