Search code examples
adobe-indesign

Is it possible to Insert an anchored text frame above a text/word?


I want to mark each CharacterStyle in the document by placing an achored text frame above each text/word. I am quite new to scripting in indesign, so any help is much appreciated.

Here is the expected output:

enter image description here


Solution

  • I found a solution to this, and for those who might encounter the same problem as me here is my code :

        app.findChangeGrepOptions.includeFootnotes = false; 
        app.findChangeGrepOptions.includeHiddenLayers = false; 
        app.findChangeGrepOptions.includeLockedLayersForFind = false; 
        app.findChangeGrepOptions.includeLockedStoriesForFind = false; 
        app.findChangeGrepOptions.includeMasterPages = false; 
        app.findGrepPreferences = NothingEnum.nothing; 
        app.findGrepPreferences.appliedCharacterStyle = style; 
        app.findGrepPreferences.findWhat = ".+"; 
    
        var found_item = doc.findGrep();
        var res_count = found_item.length;
    
        if(res_count > 0){
                var found_text = found_item[0].paragraphs.firstItem();
                var insertion_character =  (found_text.characters.lastItem().index);
                var check_insertion_character = insertion_character + 1;
                var alt_insertion_character = (found_text.characters.firstItem().index);
                var the_story = found_text.parentStory;
                try{
    
                app.selection = the_story.insertionPoints[check_insertion_character];
                app.selection = the_story.insertionPoints[alt_insertion_character];
                }catch(err){
                   alert(err);
                }
    
                var the_anchored_frame = app.selection[0].textFrames.add({geometricBounds:["0","0",1,10],anchoredObjectSettings:{anchoredPosition: AnchorPosition.ABOVE_LINE, horizontalReferencePoint: AnchoredRelativeTo.ANCHOR_LOCATION, verticalReferencePoint: VerticallyRelativeTo.TOP_OF_LEADING}}); 
    
                the_anchored_frame.contents = style;
    
               // the_anchored_frame.appliedObjectStyle = real_anchor_style; 
            }
    

    Cheers