13 Replies Latest reply on May 13, 2011 5:36 AM by John Hawkinson

    [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters

    Jarema_IV

      ... without doing it for each character in a Story?

      Selektion cannot be used becaus the document has no windows.

       

      Any suggestions?

       

      Thx in advance

        • 1. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
          John Hawkinson Level 5

          How do you want to specify it? Selection shouldn't really enter into it. Do you want to specify it based on the story? The paragraphs? The lines? The individual character offsets?

          • 2. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
            Jarema_IV Level 1

            Hi,

             

            I can use anything what is within a TextFrame. It can be based on Story, Paragraphs, Characters, etc.

            The situation is I got a TextFrame with a Contents inside, I got start character and end character of the characters they have to get another FontStyle whithin a TextFrame. Ilterate with all characters an apply new FontStyle to a single character works fine but is too slow.

             

            Thx

            • 3. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
              John Hawkinson Level 5

              You should use ItemByRange.

              I dont' do VB so I can't really advise you, but I think you want myTextFrame.Texts.ItemByRange(myStartCharacter, myEndCharacter) and set the relevant properties.

              • 4. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                Jarema_IV Level 1

                Sorry but this is not the right way.

                Text is not the same what Character - even if there are many characters there is only one Text in a TextFrame so Texts.ItemByRange(startChar,endChar) simple leeds to an error. I have tried Characters.ItemByRange(startchar,endchar) but the result is a collection of objects/characters and both do not have the right property like FontStyle or CharacterStyle...

                 

                Any other suggestions?

                 

                thx

                • 5. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                  csm_phil Level 4

                  Hi Jerma,

                   

                  i think you expect this?

                  please try to change the VB Script this method you can use it:

                   

                  var myDoc = app.activeDocument;

                  var myStr = myDoc.stories;

                  for (i=0; i<=myStr.length-1; i++){

                      var myPar = myStr[i];

                      for (j=0; j<=myPar.paragraphs.length-1; j++){

                          app.select(myPar.paragraphs[j].characters.itemByRange(0,2));

                          app.selection[0].appliedCharacterStyle="Test";

                          app.selection = null;

                          }

                      }

                   

                   

                   

                  thx

                  csm_phil

                   

                  Message was edited by: csm_phil

                   

                  Message was edited by: csm_phil

                  • 6. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                    John Hawkinson Level 5

                    Your Character range should have a FontStyle proprety, I believe. Can you please show the code where it fails?

                     

                    In Javascript, this code works just fine:

                     

                    app.activeDocument.textFrames[0].characters.itemByRange(10,4000).fontStyle="Italic"
                    

                     

                    which italicizes characters 10 through 4000 of the only textframe in the story.


                    Sorry for suggesting Texts.ItemByRange. I copied it from the VB scripting guide, but I think either I was confused as I was translating javascript into VB, or there was an error in the documentation (or both!).

                     

                    csm_phil: Jarema is trying to do this in VB, not in JS. But I don't see why you would use app.select(). As we've discussed in other threads, that is essentially always a very bad idea, and should never be necessary. Also, your example iterates over paragraphs. I don't believe that is what is wanted here.

                    • 7. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                      Jarema_IV Level 1

                      thx for Your answer but as I wrote above - I cannot use indesign selection because the document has no window!

                      so the trick would be to apply charstyle or simple fontstyle to a few characters NOT selected in indesign and without using 'for next' on each character...

                      I guess there is no such possibility.... the only way is to use Find/Change i guess - not realy what I would like to use

                      thx anyway

                       

                      regards

                       

                      jarema

                      • 8. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                        Jarema_IV Level 1

                        Hi John,

                         

                        the code I tried: (objCallObject is a valid TextFrame with contents, mPub is the active document, the Fonts are present and the char style is also defined)

                        objCallObject.ParentStory.Characters.ItemByRange(mBlocks.Item(a).StartPos, mBlocks.Item(a).EndPos).FontStyle = "77 Bold Condensed"

                        and

                        objCallObject.ParentStory.Characters.ItemByRange(mBlocks.Item(a).StartPos, mBlocks.Item(a).EndPos).AppliedCharacterStyle = mPub.CharacterStyles("Bold_Condensed")

                         

                        in both cases VB report error 438 (translated from german) "object support" no such method or property

                        If I look at the watch window at VB I see that objCallObject.ParentStory.Characters.ItemByRange(mBlocks.Item(a).StartPos, mBlocks.Item(a).EndPos) is a collection with data type variant/object/objects...  each item in this collection is of type variant/object/character

                         

                        regards

                         

                        jarema

                        • 9. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                          John Hawkinson Level 5

                          Maybe this is one of those cases where VB's type system causes a problem.

                           

                          Why don't you try calling JavaScript from your VB code? It's a pain but it'll work!

                          • 10. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                            Jarema_IV Level 1

                            unfortunatly there is no way to bind InDesign library to a Java development - working with external application while using indesign doscript for external java scripts is a terrible slow way and all I need is performance, performance an performance again..

                             

                            anyway thx for helping

                             

                            regards

                            • 11. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                              John Hawkinson Level 5
                              unfortunatly there is no way to bind InDesign library to a Java development

                               

                              Sorry, what? Javascript is not Java, but you can call external libraries from InDesign Javascript, though that's probably not what you mean.

                              Are you saying you can't call InDesign's Javascript from an external Java or Javascript environment? That's mostly true, though you could probably do it with COM+ though it's undocumented and unsupported.

                               

                              - working with external application while using indesign doscript for external java scripts is a terrible slow way and all I need is performance, performance an performance again..

                               

                              As long as you only do a small number of doScript() calls, you should be fine. For instance, one per story. Certainly one per document. You might have to stuff a lot of information into the doScript() call, but that's OK, there's no reason you cannot.

                               

                              But can you give us more information?

                               

                              Also, it should be possible to trick the VB interpreter into letting you do what you want. I think Chuck Weger or Marc Autret pointed out recently an older thread that discussed hand-patching the TLB to fix a typing bug.

                              • 12. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                                Jarema_IV Level 1

                                Well, as I know there is no way to bind InDesign to Java let say JBuilder or other programming tools like RealBasic,etc, the way your can do it with VB or AppleScript. I guess it could be possible (an erlie InDesign version 1.0 and 1.5 I have made it to bind it into C++ Visual Studio showing it at the developmen conference in Amsterdam) but right now I don't know the way. Only InDesign-Server supports Java enviroment directly but my application is working with normal version of InDesign.

                                Anyway, the applikation I am working on is a database publishing tool which use InDesign as output platform. There is a lot of stuff dealing with InDesign reading and seting informations on InDesign items and properties so using DoScript is the worsest way I can go (allready done it with JBuilder and RealBasic and JavaScript). The perfomance is simple awfull and debuging also. In this case I got sometimes hundreds of TextFrames on each page and I need access to FontStyle on almost each frame - no go with DoScript . Better way would be maybe to create small SDK plug-in only for this stuff and I think this ist right now the way for me to go. I will ask at SDK forum if this is possible.

                                 

                                anyway thx for discusion and for helping

                                 

                                regards

                                • 13. Re: [CS5][VB] How to apply FontStyle or CharacterStyle to a range of characters
                                  John Hawkinson Level 5

                                  If the overhead is truly doScript, then you should be able to compose

                                  a single JavaScript that operates on your hundreds of textframes and

                                  doScript it simply once.

                                   

                                  Remember that in performance-critical code

                                  what really hurts is trips between the scripting language and InDesign's

                                  object model. If you iterate over every character, each time you touch

                                  a character, InDesign has to construct a character object and pass it

                                  to the scripting DOM. That's why using itemByRange is key -- it constructs

                                  one object.

                                   

                                  Similary, whenver you loop, you are much better off calling .getElements()

                                  before the loop begins so that you make one call into the object model,

                                  rather than n calls. Especially if a lot of calls are readng.

                                   

                                  I suspect the overhead of the object construction is much worse than

                                  the doScript.

                                   

                                  And if that's the case, perhaps you are correct and you need an SDK plugin.

                                  But you will make yourself a huge maintenance hassle and compatibility

                                  problem.

                                   

                                  But you still haven't explained. How does your application determine which

                                  textframes and character offsets need styling? Does it need to consult

                                  the object model to do so?