11 Replies Latest reply on Dec 5, 2008 5:16 PM by Roy McCoy

    Find to end of document or story

    Level 1
      Is there a way to limit the find text so that it only goes to the end of the document or story?

      When the cursor is planted in text the find dialog has a "To End of Story" option but I don't see any thing equivalent in the scripting. Of course, finding this stuff hasn't been by InDesign scripting forte.

      Thanks,
      Ken
        • 1. Re: Find to end of document or story
          Level 1
          Hi Ken,

          If you have a cursor in text, you can use item by range to get a single text object from that point to the end of the story. Then tell that object to do the find/change operation. How, exactly, you do that depends on the scripting language you're using.

          Thanks,

          Ole
          • 2. Re: Find to end of document or story
            Roy McCoy Level 1
            Olav Kvern wrote:

            > If you have a cursor in text, you can use item by range to get a
            > single text object from that point to the end of the story. Then
            > tell that object to do the find/change operation.

            That's kind of the way I was trying to do it too, selecting to the end
            and searching the selection - but as usual I couldn't get anything to
            work (in AppleScript).

            "Item by range" isn't found in the Scripting Guide or the Scripting
            Reference, and the one Google find on it is a broken link. So "item by
            range" doesn't help me. I don't know about Ken.

            This is yet another thing I never had any trouble doing with PM
            scripting. Maybe InDesign scripting is better, but I don't see how
            this can be if you can't figure out how to do things at all, or it
            takes you hours and hours to finally accomplish the most simple task,
            if you finally succeed at all. Please excuse my grumbling, but I've
            lost a lot of time here and I still don't know how to select text to
            the end of a story.


            Thanks,

            Roy McCoy
            Rotterdam, NL
            • 3. Re: Find to end of document or story
              Level 1
              Hi Roy,

              AppleScript doesn't *need* an item by range because that's a basic part of the language. We added it to JavaScript and VBScript because we thought it'd be nice.

              Something like this:

              --FindChangeToEndOfStory.applescript
              
              --An InDesign CS4 AppleScript
              --
              --Runs a search on the text from the current selection to
              --the end of the story.
              main()
              on main()
              tell application "Adobe InDesign CS4"
                if (count documents) is not equal to 0 then
                 set mySelection to selection
                 if (count mySelection) is 1 then
                  if class of item 1 of selection is insertion point then
                   my myFindChangeToEndOfStory()
                  end if
                 end if
                end if
              end tell
              end main
              on myFindChangeToEndOfStory()
              tell application "Adobe InDesign CS4"
                --Clear find/change preferences
                set find text preferences to nothing
                set change text preferences to nothing
                set find what of find text preferences to ".  "
                set change to of change text preferences to ". "
                set myStory to parent story of item 1 of selection
                set myIndex to index of item 1 of selection
                --Here's how you'd select it:
                --select text 1 of text from insertion point myIndex to insertion point -1 of myStory
                --do a find/change
                tell text 1 of text from insertion point myIndex to insertion point -1 of myStory
                 change text
                end tell
                --Clear find/change preferences
                set find text preferences to nothing
                set change text preferences to nothing
              end tell
              end myFindChangeToEndOfStory

              One thing with AppleScript--because AppleScript assigns a *string* to the variable myText when you do something like this:

              set myText to text 1 of text from insertion point myIndex to insertion point -1 of myStory

              ...you often need to use an object reference:

              set myText to object reference of text 1 of text from insertion point myIndex to insertion point -1 of myStory

              The above example gets around this by not assigning the text to a variable.

              re: PageMaker scripting

              Let's be honest: It was awful. I loved it, because it was better than nothing, but it was fragile and incredibly limited. InDesign scripting is far superior, once you get your head out of PageMaker scripting--and I worked on/contributed to both implementations.

              Stop trying to think of things in terms of selection, menu choices, and other user actions (the PageMaker way) and embrace the object model, and you'll be much happier. ScriptDebugger's excellent dictionary viewer can help with this.

              Thanks,

              Ole
              • 4. Re: Find to end of document or story
                Level 1
                Roy, I feel your pain. I've been wring code for many years in various languages and have found this very frustrating to learn. Ole is right though, once you start to understand the object oriented approach, things get much easier. I'm getting there, albeit slowly.

                Ole, I've looked up ItemByRange in the Scripting Guide (VB, Windows) and THINK I have from the cursor to the end of the story assigned to SrchTxt with these statements but can't seem to do anything with it.

                Set SChar = Ind.Selection(1).ParentStory.Characters(Ind.Selection(1).InsertionPoints(-1).Index)
                Set EChar = Ind.Selection(1).ParentStory.Characters(-1)
                Set SrchTxt = Ind.Selection(1).ParentStory.Texts.ItemByRange(SChar, EChar)

                Any help with what I'm missing is greatly appreciated.

                Ken
                • 5. Re: Find to end of document or story
                  Level 1
                  Sorry, I got it.

                  I wasn't using the Items element.

                  set Foundd = SrchTxt.Item(1).FindText worked great!

                  Thanks,
                  Ken
                  • 6. Re: Find to end of document or story
                    Roy McCoy Level 1
                    Ken Krugh wrote:

                    Roy, I feel your pain. I've been wring code for many years in various languages and have found this very frustrating to learn. Ole is right though, once you start to understand the object oriented approach, things get much easier.

                    Thanks for the sympathy and encouragement. As I've said several times, I'm sure the new approach is just fine. My main problem has been getting into it via the provided documentation. Nobody learns a spoken language using a dictionary and a couple of high-level essays, and I think it's the same with a computer language. That said, I've always had to use a "missing manual" before to get up to speed with something - a couple of Ole's books, for example - and I don't suppose this is an exception on that either. I'm not sure I've ever looked forward to a book more than Shirley Hopkins' AppleScripting InDesign, which may have arrived and be waiting for me in my office right now, and which I'm sure will help immensely. I guess I should have studied the numerous Adobe sample scripts more, which would have helped also, but it's so much better when such specimens are woven into explanatory text - like in the Scripting Guide, only more comprehensively so you can find the basic things you need. Without this arcane guru/initiate vibe, I might add.

                    Roy McCoy
                    Rotterdam, NL

                    • 7. Re: Find to end of document or story
                      Level 1
                      Roy wrote: "Without this arcane guru/initiate vibe, I might add."

                      I hope I'm not giving that impression--I certainly don't mean to! If I have, please accept my apologies. I know that sometimes my responses are terse, but my time and (especially!) brainpower are sometimes in short supply.

                      re: "Nobody learns a spoken language using a dictionary and a couple of high-level essays, and I think it's the same with a computer language."

                      Agreed--and the best way to learn either is to speak the language--or write it, in the case of scripting. We're all going to make mistakes, and there are things that we're going to be frustrated about. But with the help of a few friendly "native" speakers, we can make progress. I hope that you can find a supportive atmosphere in this forum--I believe it's here, once we give each other the benefit of the doubt.

                      Thanks,

                      Ole
                      • 8. Re: Find to end of document or story
                        Level 1
                        For what it's worth, Ole, I think this forum is, indeed, to say the least, a supportive atmosphere. With people like Dave Sauders and yourself it has made my experience learning the InD scripting bearable a FAR shorter endeavor that it might have been.

                        I hope to give back a bit one I get a bit more under my belt.

                        All the Best,
                        Ken
                        • 9. Re: Find to end of document or story
                          Roy McCoy Level 1
                          Hi Ole,

                          "Arcane guru/initiate vibe" apparently came across more personally and negatively than I intended - sorry. I wasn't thinking particularly of you or anyone, and what I meant was simply that scripting information seems to be being transferred from person to person, like a guru giving a mantra to an initiate or something, and that this seems to be necessary owing to large gaps in the InDesign scripting documentation. If Shane could say, for example, that I should look for info about something that I'm having trouble with on page such-and-such of the Scripting Guide, I'm fairly sure he would. From the fact that he doesn't, and from the fact that generally I've already searched there myself and not found it, I have to conclude that it likely isn't there. It all seems somewhat regrettable, not the least because it presumably results in less scripting and fewer scripters than would otherwise be the case. With AppleScript in particular, as I just wrote to Dave Saunders, I've been Googling on "tell application Adobe InDesign" and very common functions ("find text", "select text", etc.) and getting barely a dozen finds, with some of these being my own posts here. None of my mentions of scripting in the InDesign Yahoo Group have met with any response at all, and all of this suggests that whatever the cause, fewer people are scripting than should be - which is a shame, particularly given the elegance of AppleScript and the fact that it's supposed to be relatively easy to learn and use.

                          I mean - that guy wanted to script writing text with a certain character style to a text file, and I told him it wasn't worth the trouble if he was only doing it once. I probably should have heeded my own advice, as I've again blown gobs of time today experiencing the customary and nightmarish Can't do that! Can't do that! Can't do that! thing, and never having figured out either how to determine the file without using "choose file", or how to append to the end of the text file once I got it. I was sure the GetMenuActions script would help and it did to a certain extent, but the file-writing routine there is only ancillary to a complex sequence that no novice can comprehend, and isn't explained in any basic way. A few friendly native speakers are VASTLY appreciated, but I don't think they can take the place of...

                          Well, now that I'm on the line let me go back to your earlier message, where you wrote:

                          re: PageMaker scripting 

                          Let's be honest: It was awful. I loved it, because it was better than nothing, but it was fragile and incredibly limited.

                          My answer here was going to be that you didn't know what you were walking into. I'm sure it had serious limitations as you said, but it was generally quite adequate for my limited needs, with the main thing being that I could generally do what I wanted to do without much trouble. And I did quite a lot actually, though it wasn't very fancy. I had 3,397 QuicKeys sequences with OS 9 on my beige G3, hundreds of which were PM scripting shortcuts that I used all the time, and this is why I took so inordinately long to move to OS X and InDesign. So here I am now, pretty much still without this former functionality and having really a devil of a time simply getting back to where I was before. I haven't attacked the general job of getting all or the better part of my former scripts running again in AppleScript, but will be doing that over the coming months. I still have a tendency to think, well, those were simple things, it's surely easy to do them over with InDesign scripting in AppleScript - but we'll see. I've already experienced several things that were no trouble with PM's scripting but very difficult with InDesign's... hopefully there won't be too many more.

                          Just one side note before I close, as I can't resist saying something about scripting tool selection. Shane questioned the necessity or advisability, and the guy surprisingly came up with a good reason. The funny thing is that with PageMaker, as far as I know scripting was the only way to switch tools with full versatility. You could use cmd-space to switch between the current tool and the selection tool, but otherwise you were largely dependent on manual selection, which was a pain among other things because the toolbar wasted space on the screen. I remember that the PM scripting even gave you a choice on how to select a tool, either by name or number. I did it by number because I triggered the changes with mnemonics anyway (opt-ctrl-a for arrow, opt-ctrl-t for text, opt-ctrl-c for crop, etc.), and it was quite convenient. Fortunately InDesign now provides keyboard shortcuts for all the tools, and you can make your own if you don't like the ones they give you.... Just made a couple of those opt-ctrl ones myself. I hate not knowing whether v or Escape is going to get me to the selection tool or not, so I'll just do it with opt-ctrl-a all the time, default and text both.


                          Thanks,

                          Roy

                          • 10. Re: Find to end of document or story
                            Level 1
                            Hi Roy,

                            While I'm the first to admit that the InDesign scripting documentation is not perfect, I have to point out--as gently as I can--that many of the problems you're having have to do with your knowledge of AppleScript, not InDesign scripting per se.

                            The OpenDocument.applescript tutorial script shows how to open a file, but it's not going to go into all of the ins and outs of talking to the Mac OS file system using AppleScript. It's also possible to use debugging and/or "display dialog" to solve file problems yourself. If you can get it to work by using "choose file", but can't get it to work when you enter a string, take a look at the string returned by "choose file" and the string you entered in the script. What's different? That's where you'll find the problem.

                            The FindText.applescript tutorial script goes over the basics of finding text in InDesign, and FindChangeText.applescript shows how to change text. While they can't cover every possible combination of find/change attributes, I think they do their job. Let me know if that's not the case!

                            re: tool selection

                            InDesign scripting doesn't really deal with user interface interactions--it's not really designed that way. In PageMaker scripting, you *had* to select tools, because the script was running the user interface. In InDesign, scripts don't do anything with the user interface--they work on the object model directly.

                            This means that a script won't fail if/when the window view changes, when the selection state isn't what you expect, etc. (all of the things that made PageMaker scripting so fragile).

                            If you'd like, we could take a shot at converting some of your PageMaker scripts to InDesign as test cases--I still remember enough of PageMaker scriptign that I should be able to translate.

                            Thanks,

                            Ole
                            • 11. Re: Find to end of document or story
                              Roy McCoy Level 1
                              Olav Kvern wrote:

                              main() 
                              on main() 
                              tell application "Adobe InDesign CS4" 
                              if (count documents) is not equal to 0 then 
                              set mySelection to selection 
                              if (count mySelection) is 1 then 
                              if class of item 1 of selection is insertion point then 
                              my myFindChangeToEndOfStory() 
                              end if 
                              end if 
                              end if 
                              end tell 
                              end main 

                              You know, e e cummings had nothing on you.

                              This is poetry, really.

                              It's maybe being a little overly picky about when it's going to do its thing, but in such a charming way. Reminds me of  http://www.dailymotion.com/video/x1p48e_emily-loizeauhommage-a-j-prevert_creation (translation at  http://olisipus.blogspot.com/2006/11/pour-faire-le-portrait-dun-oiseau.html).

                              on myFindChangeToEndOfStory() 
                              tell application "Adobe InDesign CS4" 
                              --Clear find/change preferences 
                              set find text preferences to nothing 
                              set change text preferences to nothing 
                              set find what of find text preferences to ".  " 
                              set change to of change text preferences to ". " 

                              Or:
                              set findText to text returned of (display dialog "Find what:" ¬
                              buttons {"Cancel", "OK"} default button 2 default answer "")
                              set changeText to text returned of (display dialog "Change to:" ¬
                              buttons {"Cancel", "OK"} default button 2 default answer "")
                              set find what of find text preferences to findText
                              set change to of change text preferences to changeText

                              You don't know of a way to have the cursor come up in the first dialog if this is run from Script Editor, do you? It doesn't matter since if I used this it would probably only be via the scripts palette, but I'm curious anyway.

                              set myStory to parent story of item 1 of selection 
                              set myIndex to index of item 1 of selection 
                              --Here's how you'd select it: 
                              --select text 1 of text from insertion point myIndex to insertion point -1 of myStory

                              If you really wanted to but you don't. Okay.

                              --do a find/change 
                              tell text 1 of text from insertion point myIndex to insertion point -1 of myStory

                              I see what you're doing, but I don't understand text 1 of text. Is there a text 0, -1, -2 of text?

                              change text 
                              end tell 
                              --Clear find/change preferences 
                              set find text preferences to nothing 
                              set change text preferences to nothing 
                              end tell 
                              end myFindChangeToEndOfStory

                              And they all lived happily forever after.

                              One thing with AppleScript--because AppleScript assigns a *string* to the variable myText when you do something like this: 

                              set myText to text 1 of text from insertion point myIndex to insertion point -1 of myStory

                              I was going to ask what text was if not a string, but I guess I can put that up in the Script Editor result pane and see for myself.... Hmm, text 1 of text and text yield identical results, and it works with simply text as well. So why "text 1" of text, aside from what is it?

                              ...you often need to use an object reference: 

                              set myText to object reference of text 1 of text from insertion point myIndex to insertion point -1 of myStory 

                              The above example gets around this by not assigning the text to a variable. 

                              Yeah. The next time something doesn't work, I'm going to add "object reference of". If that doesn't work, I'll add "of parent story".

                              re: PageMaker scripting 

                              Let's be honest: It was awful. I loved it, because it was better than nothing, but it was fragile and incredibly limited.

                              I think maybe I just used it different than other people, to get simple things I could trigger easily (using QuicKeys) rather than doing more complex things of the type you found it inadequate for. I believe you, really.

                              *****

                              CS2 doesn't have GREP, so charles darwin can't use that.

                              I think he'll have to evolve, then.

                              CS4 has GREP nested styles. You'll dig them.

                              You said it, daddy-o. I'll check out the scene.

                              *****

                              While I'm the first to admit that the InDesign scripting documentation is not perfect, I have to point out--as gently as I can--that many of the problems you're having have to do with your knowledge of AppleScript, not InDesign scripting per se. 

                              Or my bad memory, or my denseness. I read the Danny Goodman AppleScript book, Tom Trinko's (gigantic) Applied Mac Scripting, The Tao of AppleScript, Leer Jezelf Makkelijk AppleScript (in Dutch), Apple and Adobe PDFs, and I don't know what. But I never used most of what I read, and so forgot it.

                              The FindText.applescript tutorial script goes over the basics of finding text in InDesign, and FindChangeText.applescript shows how to change text. While they can't cover every possible combination of find/change attributes, I think they do their job. Let me know if that's not the case! 

                              I only looked at what was in the Scripting Guide, which did seem inadequate for some of the things I wanted to do in the scripts I recently did. If there was more in the samples, then I should have paid attention to them. If I have a look and find something missing there too, I will let you know.

                              re: tool selection 

                              InDesign scripting doesn't really deal with user interface interactions--it's not really designed that way. In PageMaker scripting, you *had* to select tools, because the script was running the user interface. In InDesign, scripts don't do anything with the user interface--they work on the object model directly. 

                              Okay, but I haven't wanted or needed to script a tool change myself. Like I said, I did it in PageMaker basically only because that was the only way to do it with keycodes.

                              This means that a script won't fail if/when the window view changes, when the selection state isn't what you expect, etc. (all of the things that made PageMaker scripting so fragile). 

                              Aha, now we understand your e e cummings paranoia. :-)

                              If you'd like, we could take a shot at converting some of your PageMaker scripts to InDesign as test cases--I still remember enough of PageMaker scripti[ng] that I should be able to translate. 

                              That's very kind of you, thanks, but I think my problem will be finding the time to go through so many of them rather than figuring out the conversions. I'll post a problem if/when I hit one, but... are you perhaps in applescript-users? This forum is great, but I think I can have enough scripting without running into so much JavaScript all the time.


                              Thanks,

                              Roy