21 Replies Latest reply: May 13, 2011 3:50 AM by camilo umana RSS

    Possible to Delete Empty Text Frames via a Script?

    kmc27 Community Member

      I've seen a few posts on similar topics, but I'm not sure if they were exactly the same. I am running Mac OS 10.5.8, InDesign CS 4 ver 6. Often our InDesign documents start (from an XML load into a InDesign template) with some empty text frames that we end up manually deleting. I saw the script below on these forums and gave it a try to automate deleting the empty frames - it looks like it should do what I need, but I get this error:

       

      JavaScript Error!

       

      Error Number: 1

      Error String: contentType

       

      File: Applications/Adobe InDesign CS4/Scripts/Scrips Panel/RemoveEmptyFrames.jsx

      Line: 7

      Source: if (myTextFrames[0].contents == "") myTextFrames[0].conentType = ContentType.UNASSIGNED.

       

      Does anyone have advice on how to make this work?

       

       

      var myStories = app.activeDocument.stories.everyItem().getElements();
      for (i = myStories.length - 1; i >= 0; i--){
          var myTextFrames = myStories[i].textContainers;
          for (j = myTextFrames.length - 1; j > 0; j--)    {
              if (myTextFrames[j].contents == "") myTextFrames[j].remove();
              }
          if (myTextFrames[0].contents == "") myTextFrames[0].contentType = ContentType.UNASSIGNED;
          }

        • 1. Re: Possible to Delete Empty Text Frames via a Script?
          P Spier CommunityMVP

          Not sure what else might be wrong, but you've got unbalanced curly braces ...

          • 2. Re: Possible to Delete Empty Text Frames via a Script?
            Harbs. CommunityMVP

            That does not delete all empty frames. It keeps the first frame and converts it to an unassigned graphic frame. If it's tagged as an xml story, that might not work...

             

            Harbs

            • 3. Re: Possible to Delete Empty Text Frames via a Script?
              kmc27 Community Member

              All the frames on the documents I have are xml tagged, but I'd like to be able to delete just the ones that are empty via scripting. Does this mean that if a text frame is xml tagged, it can only be deleted manually?

              • 4. Re: Possible to Delete Empty Text Frames via a Script?
                Harbs. CommunityMVP

                No. Try this instead:

                 

                var myStories = app.activeDocument.stories.everyItem().getElements();
                for (i = myStories.length - 1; i >= 0; i--){
                    var myTextFrames = myStories[i].textContainers;
                    for (j = myTextFrames.length - 1; j >= 0; j--)    {
                        if (myTextFrames[j].contents == ""){
                            myTextFrames[j].remove();
                        }
                    }
                }
                

                 

                Harbs

                • 5. Re: Possible to Delete Empty Text Frames via a Script?
                  rob day CommunityMVP

                  Shane Stanley posted this elegant 1 line AppleScript awhile back:

                   

                   

                  tell application "Adobe InDesign CS5"

                       try

                            delete (every item of all page items of every page of document 1 whose class is text frame and contents = "" and name of fill color = "None" and name of stroke color = "None")

                       end try

                  end tell

                  • 6. Re: Possible to Delete Empty Text Frames via a Script?
                    kmc27 Community Member

                    Thanks for your help on this. I did some testing with Harbs script and am not sure what to make of the results. The content on my InDesign documents are created via an XML import, and maybe that makes a difference.

                     

                    Here is what I found:

                    1. Empty frames created from the XML import remain on the page

                    2. Overset frames created from the the XML import are deleted.

                    3. If I manually created a Text Frame via the Type Tool and it is empty, it is deleted

                     

                    I tested further and found that if I go into the structure pane and delete the empty text frame element and then run the script, the empty text frames are then deleted.

                     

                    Is there something in the XML created text frames that appears as content to the script?

                     

                    An example page is here 

                    • 7. Re: Possible to Delete Empty Text Frames via a Script?
                      rob day CommunityMVP

                      This almost seems like a bug at least with AppleScript

                       

                      If I make and loop through a list of every text frame with an XML tag, and then test with an if statement checking for "" content, the if statement doesn't run even when the "" contents return as true:

                       

                       

                       

                      tell application "Adobe InDesign CS5"

                      --get a list every tagged text box

                      set myXML to every item of all page items of every page of document 1 whose class is text frame and associated XML element is not nothing

                       

                      repeat with i in myXML

                      --even when the contents is equal to "" the delete doesn't happen

                      if contents of item 1 of i is equal to "" then

                      beep

                      delete i

                      end if

                      end repeat

                      end tell

                      • 8. Re: Possible to Delete Empty Text Frames via a Script?
                        kmc27 Community Member

                        Does this mean that XML generated Text Frames have some value that a Text Frame created with the Type Tool does not?

                        • 9. Re: Possible to Delete Empty Text Frames via a Script?
                          rob day CommunityMVP

                          It looks like the XML tags are considered content even when there are no characters in the text box, so if I run a script to count the characters in every text box TextBox4 shows 4 characters even though it is empty on the page. If I delete the Exclusion_Notes tag the character count is 0.

                           

                          Try this script which worked on your sample (it looks for 0 words):

                           

                          tell application "Adobe InDesign CS5"

                          try

                          set theframes to every item of all page items of every page of document 1 whose class is text frame and name of fill color = "None" and name of stroke color = "None"

                          repeat with x in theframes

                          if (count of words of x) is 0 then

                          delete x

                          end if

                          end repeat

                          end try

                          end tell

                          • 10. Re: Possible to Delete Empty Text Frames via a Script?
                            kmc27 Community Member

                            Hi Rob

                             

                            Thanks so much for your help on this. Your solution worked perfectly - the script now deletes all empty frames.

                             

                            I used the find/change example provided in the InDesign scripting examples in an attempt determine if I could edit your script to delete both empty text frames (as in the script you provided) and also the text frames that have the character format of "bt glyphs". I've found I am just going in circles. Do you think you could take a look at this one more time for me to see if it can be made to delete the empty text frames AND all text frames that have the character format of "bt glyphs"?

                             

                            I can identify them frames via a search in InDesigns Find/Change window and the criteria below.

                             

                            Go to Edit > Find/Change

                            Select the "Text" tab

                            Click the "Specify Attributes to Find" button and choose:
                            Basic Character Formats

                            Font Family "BT Glyphs"

                            Font Style "Regular"

                             

                            I believe those frames can be identified via a findtype of "text" containing the properties of font family "bt glyphs" and font style "regular" (got that from the Adobe's InDesign scripting examples), but I am not able to get the results I want.

                             

                            Any (additional) help you can provide would be greatly appreciated.

                             

                             

                             

                             

                            • 11. Re: Possible to Delete Empty Text Frames via a Script?
                              rob day CommunityMVP

                              see if this works:

                               

                              tell application "Adobe InDesign CS5"

                                   try

                                        set theframes to every item of all page items of every page of document 1 whose class is text frame and name of fill color = "None" and name of stroke color = "None"

                                        repeat with x in theframes

                                             if (count of words of x) is 0 then

                                                  delete x

                                             else

                                                  --note the tell is to ID not the document

                                                  tell application "Adobe InDesign CS5"

                                                       --clear find prefs

                                                       set find text preferences to nothing

                                                       set applied character style of find text preferences to character style "bt glyphs" of document 1

                                                       if (find text) is not {} then

                                                            delete x

                                                       end if

                                                       --reclear find prefs

                                                       set find text preferences to nothing

                                                  end tell

                                             end if

                                        end repeat

                                   end try

                              end tell

                              • 12. Re: Possible to Delete Empty Text Frames via a Script?
                                rob day CommunityMVP

                                Reading your post again I see that you are looking for text with a font applied. My script is searching for a character style named bt glyphs, so it's not going to work as I posted it.

                                 

                                Do you want to delete the frame if there is any character with the font BT Glyph applied? Or, does all of the text need to have the font applied before the frame is deleted?

                                • 13. Re: Possible to Delete Empty Text Frames via a Script?
                                  rob day CommunityMVP

                                  This one looks for the BT Glyph font—if there's any character in the frame with that font the frame is deleted:

                                   

                                   

                                  tell application "Adobe InDesign CS5"

                                  try

                                  set theframes to every item of all page items of every page of document 1 whose class is text frame and name of fill color = "None" and name of stroke color = "None"

                                  repeat with x in theframes

                                  if (count of words of x) is 0 then

                                  delete x

                                  else

                                  --note the tell is to ID not the document

                                  tell application "Adobe InDesign CS5"

                                  --set find prefs

                                  set find text preferences to nothing

                                  set font style of find text preferences to "Regular"

                                  set applied font of find text preferences to "BT Glyphs"

                                  if (find text) is not {} then

                                  delete x

                                  end if

                                  --reset find prefs

                                  set find text preferences to nothing

                                  end tell

                                   

                                  end if

                                  end repeat

                                  end try

                                  end tell


                                  • 14. Re: Possible to Delete Empty Text Frames via a Script?
                                    kmc27 Community Member

                                    Hi Rob

                                     

                                    Thanks. I ran the second script. It does remove the Text Frames with the bt glyphs font, but also deletes all the Text Frames. I found that it:

                                     

                                    Deletes overset Text Frames are deleted even though they are not empty - I probably did notice this the first time I tried the script.
                                    The exeception is that I do want to delete any text frames using the bt glyphs font, whether those frames are overset or not.

                                     

                                    Deletes Text Frames that use another font other than bt glyphs

                                     

                                    The text preferences criteria called for in the script match what I set up via the Edit > Find/Change dialogue box. Using the Find/Change dialogue box results in only those Text Frames using the BT Glyphs font being "found", so it seems that criteria should hold for the script too.

                                     

                                    Is there some other criteria I should use to identify the bt glph text frames?

                                    • 15. Re: Possible to Delete Empty Text Frames via a Script?
                                      rob day CommunityMVP

                                      Sorry, I should have tested before I posted the last one. This seems to work—also handles frames that have an overflow:

                                       

                                      tell application "Adobe InDesign CS5"

                                      try

                                      set theframes to every item of all page items of every page of document 1 whose class is text frame and name of fill color = "None" and name of stroke color = "None"

                                      repeat with x in theframes

                                      if (count of words of x) is 0 and overflows of x is false then

                                      delete x

                                      else

                                      --note the tell is to ID not the document

                                      tell application "Adobe InDesign CS5"

                                      --set find prefs

                                      set find text preferences to nothing

                                      set font style of find text preferences to "Regular"

                                      set applied font of find text preferences to "BT Glyphs"

                                      set ftext to find text

                                      if ftext is not {} then

                                      delete parent of item 1 of ftext

                                      end if

                                      --reset find prefs

                                      set find text preferences to nothing

                                      end tell

                                      end if

                                      end repeat

                                      end try

                                      end tell


                                      • 16. Re: Possible to Delete Empty Text Frames via a Script?
                                        kmc27 Community Member

                                        Rob

                                         

                                        Thanks so much for your time and effort in making this work. Your last version does the trick. I had attempted to get the script working by referring to the Adobe InDesign Scripting Guide, but after comparing the non-working vs the latest working script, I can see that I wasn't even close.

                                         

                                        Again, I really appreciate your sharing your time and expertise.

                                         

                                        Kenn

                                        • 17. Re: Possible to Delete Empty Text Frames via a Script?
                                          camilo umana Community Member

                                          Rob:

                                           

                                          Could you please help with this message coming form your script?

                                          IDCS5/7.0.4

                                           

                                          Thank you.Screen shot 2011-05-13 at 4.54.48 AM.png

                                          • 18. Re: Possible to Delete Empty Text Frames via a Script?
                                            [Jongware] CommunityMVP

                                            You copied the script into TextEdit on the Mac. By default, TextEdit saves new files as RTF (formatted) text, and a script should be saved as plain text, without any formatting.

                                            Look in your menus,  there should be an option "Convert to Plain Text" somewhere.

                                            • 19. Re: Possible to Delete Empty Text Frames via a Script?
                                              [Jongware] CommunityMVP

                                              Uh. That's for Javascript, actually. I don't know if Applescript needs a similar treatment.

                                              • 20. Re: Possible to Delete Empty Text Frames via a Script?
                                                camilo umana Community Member

                                                Thank you.

                                                 

                                                Will try via AppleScript editor

                                                • 21. Re: Possible to Delete Empty Text Frames via a Script?
                                                  camilo umana Community Member

                                                  Jong:

                                                   

                                                  You were absolutely right. I copy-pasted the original script in AppleScript app. ( with extension .scpt) and FINALLY this splendid script felt tuned.

                                                   

                                                  Thanks!

                                                   

                                                  Screen shot 2011-05-13 at 5.48.06 AM.png