7 Replies Latest reply on Aug 1, 2009 11:50 AM by Emanuele Radaelli

    [JS][CS3] Change the content type of a text frame

    Emanuele Radaelli Level 1

      Hi,

      a bit basic maybe but I'm stuck on this from an hour...

      I need a textFrame to become a graphic object.

      The textFrame has no content.

      How can I change the contentType on that object?

       

      Thanks anticipately.

        • 1. Re: [JS][CS3] Change the content type of a text frame
          Thomas B. Nielsen Level 3

          This snippet wil convert a selected textFrame to graphic type (must select textFrame with black arrow)

           

          var myDocument = app.activeDocument;
          var mySel = myDocument.selection[0];

           

          mySel.contentType = ContentType.GRAPHIC_TYPE;

          • 2. Re: [JS][CS3] Change the content type of a text frame
            Emanuele Radaelli Level 1

            Thanks for your reply Thomas.

            The problem is I'm trying to get the object selected but I can't refer to that object.

            I'll try to explain...

            I place a snippet, the snippet is composed by 4 tagged textFrames.

            One of these textFrame is empty, tagged with "image" tag and I need it to be converted in a graphicFrame.

             

            var curSnippet = new File("/snippet.inds");

            var mySnippet = myDocument.pages.item(0).place(curSnippet);

            for(var c = 0; c < mySnippet.length; c++){

                 if(mySnippet[c].associatedXMLElement.markupTag.name == "image"){

                      HERE I NEED TO SELECT AND CONVERT MY OBJECT...

                 }

            }

             

            How can I select that bloody textFrame?

            That's the real question...

             

            PS: I need that frame to be a text frame in the beginning...

            • 3. Re: [JS][CS3] Change the content type of a text frame
              Thomas B. Nielsen Level 3

              I'm afraid I'm not able to answer your question - can anyone explain why the below acts as it does?

              Especially, what to do, to get a valid reference for a textframe in this snippet, that will allow us to read the associatedXMLElement?

               

              var curSnippet = new File("/snippet.inds");
              var mySnippet = myDocument.pages.item(0).place(curSnippet);
              for(var c = 0; c < mySnippet.length; c++){
                  var myName = "";
                 
                  alert(mySnippet[c]); // alerts story (odd since we have textFrames)

               

                  // but a story have textFrames, so i guess
                  alert(mySnippet[c].textFrames[0]); // will alert object textFrame
                 
                  // however, i dont understand why
                  alert(mySnippet[c].textFrames[0].associatedXMLElement); // will fail
                 
                  // I would have thought that we could just do, but that wont work?:
                  try {
                      var myElm = mySnippet[c].textFrames[0].associatedXMLElement;
                      myName = myElm.markupTag.name;
                  } catch (e) {
                      continue;
                  }
                  if(myName == "image"){
                      try {
                          mySnippet[c].textFrames[0].contentType = ContentType.GRAPHIC_TYPE;
                      } catch (e) {}
                  }
                  // If you actually also want to select the converted frame, you should be able to do:
                  myDocument.select(mySnippet[c].textFrames[0]);
              }

              • 4. Re: [JS][CS3] Change the content type of a text frame
                Emanuele Radaelli Level 1

                Yes Thomas, that's the way I tried...

                Unsuccessfully.

                 

                I also tried to get geometricBounds of the textFrame ( mySnippet[c].textFrames[0].geometricBounds; ) to make another object with the same properties but it doesn't work.

                 

                If you try to get the length of the textFrames array ( mySnippet[c].textFrames.length; ) it returns 0.

                 

                It looks like an ID bug...

                • 5. Re: [JS][CS3] Change the content type of a text frame
                  [Jongware] Most Valuable Participant
                  If you try to get the length of the textFrames array ( mySnippet[c].textFrames.length; ) it returns 0.

                  That would return the number of text frames inside the snippet story.

                   

                  I've done some testing with a snippet consisting of one text frame and one graphic frame -- a rectangle.

                  After importing, the text frame reports its type as "Story", the rectangle as "undefined"! It seems to be ignored completely -- I tried this

                   

                  var myGroup = app.activeDocument.groups.add(mySnippet);

                  and it failed as well, reporting back it received just a Story.

                  • 6. Re: [JS][CS3] Change the content type of a text frame
                    Dave Saunders Level 4

                    This looked like an interesting challenge, so I'm taking a shot at it. I created a new document and added three text frames (unpopulated) to the first page. I tagged them "Image", "Caption" and "credit". I then exported as a snippet, deleted the document and opened another new one. Then I placed the snippet (so far, everything has been manual -- no scripts) and indeed, the snippet placed and the frames were added to the document's structure and were properly tagged -- the three tags were automatically created by the action of placing the snippet.

                     

                    So, let's duplicate this in a script. Even though I know exactly where the snippet is, I'm going ask the user to find it. That way, I eliminate the issue of getting the path to the snippet wrong:

                    var myDoc = app.documents.add();
                    var myPage = myDoc.pages[0];
                    var myPlacePoint = [myPage.marginPreferences.left, myPage.marginPreferences.top];
                    var mySnippetFile = File.openDialog("Choose the snippet");
                    if (mySnippetFile == null) { exit() }
                    var mySnippet = myPage.place(mySnippetFile, myPlacePoint);
                    

                    And that worked. The snippet is on the page exactly where I wanted it. So, now we need to find the text frame that has the "image" tag. First, we must explore just what mySnippet consists of.

                     

                    OK, it's an array of three stories. That's a tad weird. Why isn't it an array of three text frames? I wonder what would happen if two of the frames in the snippet were threaded -- but let's address that later after we solve the immediate issue. The point is that we know that the snippet consists of three separate text frames that aren't threaded. So:

                    for (var j = mySnippet.length - 1; j >= 0; j--) {
                         var myTag = mySnippet[j].associatedXMLElement.markupTag.name;
                         alert(myTag);
                    }
                    

                     

                    And this gives me the three tags (and reminds us that when you tag a text frame you're also tagging the story that holds it. So:

                    var myDoc = app.documents.add();
                    var myPage = myDoc.pages[0];
                    var myPlacePoint = [myPage.marginPreferences.left, myPage.marginPreferences.top];
                    var mySnippetFile = File.openDialog("Choose the snippet");
                    if (mySnippetFile == null) { exit() }
                    var mySnippet = myPage.place(mySnippetFile, myPlacePoint);
                    for (var j = mySnippet.length - 1; j >= 0; j--) {
                         var myTag = mySnippet[j].associatedXMLElement.markupTag.name;
                         if (myTag === "Image") {
                              var myFrame = mySnippet[j].textContainers[0];
                              myFrame.contentType = ContentType.graphicType;
                              break;
                         }
                    }
                    

                    And that does the job.

                     

                    Thomas,

                     

                    You were very close but you forgot that the text frames that make up a story are addressed as textContainers from CS3 onwards. Your code would have worked in CS2.

                     

                    Dave

                    • 7. Re: [JS][CS3] Change the content type of a text frame
                      Emanuele Radaelli Level 1

                      Thanks Dave that works!

                      Anyway, I find it really a strange...

                       

                      Lele