17 Replies Latest reply on Nov 16, 2008 11:28 PM by sstanleyau

    CS3 Scripting Guide AppleScript Ch. 2

    Roy McCoy Level 1
      Hi,

      I'm writing straight to the forum, trying to avoid the website. Hope this works.

      I read the CS3 Scripting Tutorial before starting in now with the CS3 Scripting Guide, but don't recall that it provided information about AppleScript objects. In any event I'm confused by the beginning of Chapter 2 in the Scripting Guide:

      The script preferences object provides objects and properties related to the way InDesign runs scripts. 

      I finally located script preferences in the upper part of the Adobe InDesign CS3 dictionary window in Script Editor, but its description links only to script preference, which lists only the properties class, enable redraw, object reference, parent, etc. So what are the objects provided by the script preferences object, if it provides both objects and properties?


      Thanks,

      Roy McCoy

        • 1. Re: CS3 Scripting Guide AppleScript Ch. 2
          Roy McCoy Level 1
          Page 6:

          Targeting for AppleScripts is done using the tell statement. You do not need a tell statement if you run the script from the Scripts Panel, because there is an implicit tell statement for the application launching the script.

          Why is it, then, that the following doesn't work without a tell statement?

          tell application "Adobe InDesign CS3"
          set hyphenation of selection to false
          end tell


          Thanks,

          Roy

          • 2. Re: CS3 Scripting Guide AppleScript Ch. 2
            Level 1
            I think it depends on whether or not you compiled the script. If you save it as text (with the appropriate extn) then the tell statement might not be necessary -- I'm just guessing, because I don't use AppleScript any more.

            Dave
            • 3. Re: CS3 Scripting Guide AppleScript Ch. 2
              sstanleyau Level 4
              It's really properties only.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
              • 4. Re: CS3 Scripting Guide AppleScript Ch. 2
                sstanleyau Level 4
                On 7/11/08 8:33 AM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />>  Why is it, then, that the following doesn't work without a tell statement?<br /><br />It does. It just doesn't *compile* without one, unless you use something<br />like Script Debugger, which lets you target an application without including<br />the relevant tell blocks.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
                • 5. Re: CS3 Scripting Guide AppleScript Ch. 2
                  Eric @ MCA Level 3
                  It's easy enough to wrap everything in tell application and that always allows you to debug from Script Editor via the Event Log.

                  If you right-click in a Script Editor window, you'll notice a ton of extra contextual menu items have been added at the bottom to save you typing. Look at the "Tell Blocks" submenu. What I did was clone the Finder one into an InDesign CS3 one. I changed the tell application "Finder" line in the if selection = "" section to these two lines and that lets me set up a script to work with the frontmost InDesign window quite easily.
                  set the script_text to the script_text & "tell application \"Adobe InDesign CS3\"" & CR
                  
                  set the script_text to the script_text & "tell active document" & CR

                  If there's text selected (the else part) then I only insert the tell application "Adobe InDesign CS3" line in case I was already working with a document.
                  • 6. Re: CS3 Scripting Guide AppleScript Ch. 2
                    Roy McCoy Level 1
                    Thanks Dave, Shane and Eric. I was somehow expecting to receive replies via e-mail because that's how I'd posted the questions, so I didn't see these until now. I now see I have to check "Subscribe to this discussion by email" on the forum page, so I'll do that in the future.

                    Wrapping InDesign scripts in tell application does seem easy enough, though I'm afraid either I don't understand the method, or it doesn't work for me, or both. When I control-click in a Script Editor window I get menu items all right, but nothing with "Tell Blocks" as a submenu.

                    Maybe this is because I may have moved the Script Editor Scripts folder to my user Library? I can check that quick enough... Yes, that was it. (My few InDesign scripts were already application-wide, except for my three "User" scripts Cocaine.scpt, Heroin.scpt and Marijuana.scpt: set contents of myTextFrame to "I want some cocaine!", etc.)

                    I get that I can insert the "tell app" and "end tell" lines automatically rather than having to type them in or copy them from another script, and I've cloned the Finder script replacing "Finder" with "Adobe InDesign CS3", but I didn't do the rest of it because I didn't understand it. In the selection = "" case I didn't see why adding "tell active document" was advisable or necessary, and it also seemed that an additional "end tell" line would have to be added manually (which seems counter to the automating idea). In the text selected case I didn't see precisely what you did or why or when.

                    Thanks,
                    Roy McCoy
                    • 7. Re: CS3 Scripting Guide AppleScript Ch. 2
                      Eric @ MCA Level 3
                      Whoops, you're right! I had added the additional "end tell" line too and just forgot about it. I did the original script back in CS1 so I've only ever changed the name of the app the last few years, lol.

                      You can do whatever is most convenient for you. I just like to have it generate the tell active doc line too because I am almost always trying to do something in the frontmost document.
                      • 8. Re: CS3 Scripting Guide AppleScript Ch. 2
                        Roy McCoy Level 1
                        On Nov 11, 2008, at 10:59 PM, Eric @ MCA wrote:

                        > Whoops, you're right! I had added the additional "end tell" line too
                        > and just forgot about it. I did the original script back in CS1 so
                        > I've only ever changed the name of the app the last few years, lol.
                        >
                        > You can do whatever is most convenient for you. I just like to have
                        > it generate the tell active doc line too because I am almost always
                        > trying to do something in the frontmost document.

                        What's most convenient for you might very well also be what's most
                        convenient for me, I just need to understand it. In this case, the ID
                        scripts I've used so far haven't had "tell active document" in them
                        but seem to work on the active doc anyway, like that's a default or
                        something. If I ever get around to finishing the scripting guide I'm
                        sure I'll understand this better.


                        Thanks,
                        Roy McCoy
                        • 9. Re: CS3 Scripting Guide AppleScript Ch. 2
                          sstanleyau Level 4
                          On 12/11/08 9:32 AM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> In this case, the ID<br />> scripts I've used so far haven't had "tell active document" in them<br />> but seem to work on the active doc anyway, like that's a default or<br />> something.<br /><br />It's definitely not a default -- it's just that both the application and<br />documents have a selection property. The application's selection is always<br />the selection of the frontmost document.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
                          • 10. Re: CS3 Scripting Guide AppleScript Ch. 2
                            Roy McCoy Level 1
                            Well, that's a something, isn't it? :-)

                            I've been working on my big Esperanto yearbook conversion script. One thing that seemed discouraging about it was that while there are a lot of things that I can confidently change all instances of, there are others that I want to check, or that I'm sure I want to change sometimes but sometimes not. So it was a welcome revelation when I realized that I could work such cases into the script too. I was afraid I wouldn't be able to work out how to do that on my own, but it turned out to be fairly easy:

                            set find what of find text preferences to "Paris"
                            set change to of change text preferences to "Rome"
                            set theFinds to find text document 1
                            repeat with i from 1 to (count of theFinds)
                            select text item i of theFinds
                            display dialog "Change this one?" buttons {"Yes", "No", "Cancel"} ¬
                            default button "Yes"
                            if button returned of result is "Yes" then
                            change text item i of theFinds
                            end if
                            select nothing
                            end repeat

                            An unsolved problem here is how to select "No" without using the mouse. I can try to create a QuicKeys button click... Yes, that works.

                            Thanks,
                            Roy
                            • 11. Re: CS3 Scripting Guide AppleScript Ch. 2
                              [Jongware] Most Valuable Participant
                              Roy,

                              Are you sure this works as you intended? theFinds gets set to the found texts
                              i at the moment of searching.
                              As soon as you actually change the texts, the array will become undefined -- it'll point to the wrong text.

                              A common trick, in JS at least, is to do the looping in reverse, so a change in the document does not interfere with previous found items.
                              • 12. Re: CS3 Scripting Guide AppleScript Ch. 2
                                Roy McCoy Level 1
                                It worked for me, but when I ran it the find text was set to "a" and the change text to "b", thus the same length and so didn't change anything essential at least regarding number of characters or position in the file.

                                To get it to go backwards, that would be, ah,

                                repeat with i from (count of theFinds) to 1 by -1

                                ?

                                Thanks,
                                Roy McCoy
                                • 13. Re: CS3 Scripting Guide AppleScript Ch. 2
                                  sstanleyau Level 4
                                  On 16/11/08 12:01 PM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> repeat with i from (count of theFinds) to 1 by -1<br /><br />Yep.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
                                  • 14. Re: CS3 Scripting Guide AppleScript Ch. 2
                                    Roy McCoy Level 1
                                    >> repeat with i from (count of theFinds) to 1 by -1
                                    >
                                    > Yep.

                                    Thanks. I'll take it that's necessary also with Applescript in such a find/change routine, if nobody says any different. It's no big deal, I'd just prefer to go front to back as I always have when finding and changing.
                                    • 15. Re: CS3 Scripting Guide AppleScript Ch. 2
                                      sstanleyau Level 4
                                      On 16/11/08 2:17 PM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> I'll take it that's necessary also with Applescript in such a find/change<br />> routine<br /><br />Yep.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>
                                      • 16. Re: CS3 Scripting Guide AppleScript Ch. 2
                                        Roy McCoy Level 1
                                        Shane,

                                        >> I'll take it that's necessary also with Applescript in such a find/
                                        >> change routine
                                        >
                                        > Yep.

                                        Thanks again. I gave Ray Robertson a call not so long ago, and it was
                                        nice talking with him again. There was something I wanted to ask him
                                        about, but I forget what it was now. It may have had something to do
                                        with QuicKeys, which he told me he wasn't using anymore, to my surprise.

                                        You got mixed up with Script Debugger in my mind so that I was
                                        thinking you were its developer. I was at the point of buying a copy
                                        out of appreciation for your help, but now I can't find a direct
                                        commercial link between you and the program, either on the web now or
                                        in my old e-mail archives. If there isn't one I can do without it, not
                                        only because my boss certainly doesn't want to pay for it, but also
                                        because its big appeal for me previously was that it had search and
                                        replace and now Script Editor has that and seems generally adequate
                                        for my present fairly humble needs.

                                        I think Ray said you'd either been over here in Holland and done an
                                        AppleScript seminar, or were thinking about it for the future. If the
                                        latter, you'll see me there for sure.


                                        Roy
                                        • 17. Re: CS3 Scripting Guide AppleScript Ch. 2
                                          sstanleyau Level 4
                                          No, I have no financial link with Script Debugger, but I do think that<br />trying to script without it is a bit like having one hand tied behind your<br />back. <br /><br />We've talked of doing AppleScript Pro Sessions in Amsterdam, but alas<br />nothing has come of it so far. Ray did do a session for Apple over there a<br />couple of years back.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au>