21 Replies Latest reply on Dec 7, 2008 4:01 PM by Roy McCoy

    CS3 Scripting Guide AppleScript

    Roy McCoy Level 1
      Shane Stanley wrote:

      > No, I have no financial link with Script Debugger, but I do think that
      > trying to script without it is a bit like having one hand tied behind your
      > back.
      >
      > We've talked of doing AppleScript Pro Sessions in Amsterdam, but alas
      > nothing has come of it so far. Ray did do a session for Apple over there a
      > couple of years back.

      Okay, I'll download the Script Debugger demo at some point and give it
      a run, but I'd better stick to one thing at a time at this point considering
      how long it's taking me just to get through the ID Scripting Guide. I have,
      however, managed to get beyond Chapter 2, and have renamed this topic
      accordingly.

      Thanks,
      Roy
        • 1. Re: CS3 Scripting Guide AppleScript
          Roy McCoy Level 1
          P.S. I managed to get a find-and-replace script to work with the confirmation dialog I wanted, and to use the list method for find and change terms that you recommended before, but it's a little clunky since I couldn't find an elegant solution for the problem I ran into, that finds after the first one weren't displayed when they were on different pages. I wanted to simply get the page the selected find was on and go to that page, but that turned out to be easier said than done. The only thing that worked for me was sending cmd-y to System Events, which toggled me between edit and layout views. I tried inserting a second cmd-y to keep me in layout view, but that didn't work.

          So here's what I wound up with that works, at least. Any suggesting for refining it so that the script goes from page to page in layout view would be very welcome, thanks.

          tell application "Adobe InDesign CS3"
          activate
          set find text preferences to nothing
          set change text preferences to nothing
          set case sensitive of find change text options to true
          set include footnotes of find change text options to true
          set include hidden layers of find change text options to true
          set include locked layers for find of find change text options to true
          set include locked stories for find of find change text options to true
          set include master pages of find change text options to true
          set whole word of find change text options to false

          set theToBeFounds to {"ch", "gh", "hh", "jh", "sh", "au", "eu", "Ch", "Gh", "Hh", "Jh", "Sh", "Au", "Eu"}
          set theToBeChangedTos to {"ĉ", "ĝ", "ĥ", "ĵ", "ŝ", "aŭ", "eŭ", "Ĉ", "Ĝ", "Ĥ", "Ĵ", "Ŝ", "Aŭ", "Eŭ"}

          repeat with i from 1 to count of theToBeFounds

          set find what of find text preferences to (item i of theToBeFounds)
          set change to of change text preferences to (item i of theToBeChangedTos)
          set theFinds to find text document 1
          repeat with j from (count of theFinds) to 1 by -1
          select text item j of theFinds

          tell application "System Events"
          keystroke "y" using command down
          keystroke "y" using command down
          end tell

          display dialog "Change this one?" buttons {"Yes", "No", "Cancel"} ¬
          default button "Yes"
          if button returned of result is "Yes" then
          change text item j of theFinds
          end if

          end repeat

          end repeat

          end tell
          • 2. Re: CS3 Scripting Guide AppleScript
            sstanleyau Level 4
            It's a tad more elegant to leave System Events out of it and use something<br />like this:<br /><br />  set zoom percentage of layout window 1 to (get zoom percentage of layout<br />window 1)<br /><br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
            • 3. Re: CS3 Scripting Guide AppleScript
              Roy McCoy Level 1
              Shane Stanley wrote:

              > It's a tad more elegant to leave System Events out of it and use
              > something
              > like this:
              >
              > set zoom percentage of layout window 1 to (get zoom percentage of
              > layout window 1)

              I'd say it was quite a bit more elegant, and I would probably never
              have figured that out on my own. I'll probably wind up using this line
              in all such s&r scripts. I was disconcerted at first by the way the
              pages seemed to be jumping all over the place, but this bug became a
              feature when I started appreciating that the found instance is always
              in the same place at the middle of the screen and is never covered up
              by the dialog window, so I don't have to move that or shove the
              document window to the side.

              I nonetheless wanted to find a way to keep the pages from jumping
              around, so I went for that. It again seemed odd that, the same as with
              getting the page number of selected text and moving to that page,
              there didn't seem to be a scriptable zoom property that could be set
              to fit in window. Sending command-0 to System Events didn't seem to
              work, oddly, and additionally occasioned some other problem - I lost
              my trackpad click, I think. Finally, after a good bit of Googling and
              a little delving into the Scripting Guide, I found out how to script a
              menu action, so that the page with the found instance now always fits
              the window:

              set zoom percentage of layout window 1 to (get zoom percentage of
              layout window 1)
              set myMenuAction to menu action "Fit Page in Window"
              invoke myMenuAction

              The jumping pages, however, were now replaced by an equally
              disconcerting jiggle. I figure I might be able to get rid of this by
              turning off the window display while this adjustment is going on, but
              I previously couldn't figure out how to do this. I saw how to do it
              when opening a new document, but as I recall the command I saw worked
              only then and I couldn't find a general alternative. I don't know that
              I'd wind up using it here, since as I said I'll likely stick with
              having the found instances in the middle of the screen, but hiding the
              document window was something I wanted to do before and will likely
              want to do again, and so I'd like to know how to do it if it's possible.


              Thanks,

              Roy McCoy
              • 4. Re: CS3 Scripting Guide AppleScript
                sstanleyau Level 4
                On 19/11/08 1:46 AM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> there didn't seem to be a scriptable zoom property that could be set<br />> to fit in window.<br /><br /> zoom layout window 1 given fit page -- or fit spread<br /><br />> I figure I might be able to get rid of this by<br />> turning off the window display while this adjustment is going on, but<br />> I previously couldn't figure out how to do this.<br /><br />You can't, for what you're after. The best you can do is minimize and<br />maximize the layout window.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                • 5. Re: CS3 Scripting Guide AppleScript
                  Roy McCoy Level 1
                  Shane Stanley wrote, quoting me:

                  >> there didn't seem to be a scriptable zoom property that could be
                  >> set to fit in window.
                  >
                  > zoom layout window 1 given fit page -- or fit spread

                  I put that in my script and saw that it worked, then immediately
                  commented it out when I kept having to move the dialog window when the
                  finds got up to under it. Always middle of the screen under the
                  dialog, yeah.

                  My question was why I didn't find what I needed on this with all my
                  searching with the PDF docs and application library. I think I've kind
                  of answered it myself now, that if I want a command like "zoom", I
                  should look at the C's in the light blue circles rather than getting
                  lost in a maze of too-numerous Spotlight finds on my search term.


                  Thanks,

                  Roy McCoy
                  • 6. Re: CS3 Scripting Guide AppleScript
                    Roy McCoy Level 1
                    I had this, with and without a space preceding "f":

                    set properties to {find text preferences:{find what:" f P}"}, change text preferences:{change to:" P}"}}
                    change text document 1
                    set properties to {find text preferences:{find what:"f P}"}, change text preferences:{change to:"P}"}}
                    change text document 1

                    I saw I could do that with GREP in a single command, so I made that:

                    set properties to {find grep preferences:{find what:" ?f P\\}"}, change grep preferences:{change to:"P}"}}
                    set myFoundItems to change grep document 1
                    display dialog ("Found " & (count myFoundItems) & " instances of the search string.")

                    This works, only when I run it on a test file with this:

                    f P}
                    f P}
                    f P}
                    f P}
                    f P}
                    f P}
                    f P}
                    f P}
                    f P}
                    f P}
                    f P}
                    f P}

                    the dialog says only 11 instances were found, though there actually are 12 and 12 are changed. Is there an explanation for this discrepancy, and perhaps a way to avoid it?

                    Thanks,
                    Roy McCoy
                    • 7. Re: CS3 Scripting Guide AppleScript
                      sstanleyau Level 4
                      On 21/11/08 5:06 AM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> Is there an explanation for this discrepancy, and perhaps a way to avoid it?<br /><br />It looks to me like you've fond a rather nasty bug, and that the results<br />returned from the change are inaccurate if there's a change in the story<br />length.<br /><br />The workaround, if you want an accurate count, is to do a "find grep", and<br />base your count on the result from that.<br /><br />And it looks like the problem is still there in CS4.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                      • 8. Re: CS3 Scripting Guide AppleScript
                        Roy McCoy Level 1
                        Find grep, check, thanks.

                        I'm now using Script Debugger, and though I went through the PDF carefully I don't know how to take advantage of it.

                        For example, I tried to do our recent routine below with grep rather than text.

                        repeat with i from (count of myFoundItems) to 1 by -1
                        select text item i of myFoundItems
                        set zoom percentage of layout window 1 to (get zoom percentage of layout window 1)
                        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 myFoundItems
                        end if
                        select nothing
                        end repeat

                        However, text item i (or what at first appeared to be text item i) was different than following a search with grep rather than text, appearing to contain all the finds. Anyway, I didn't know how to use the SD tools to dig in there and figure out what was going on. I'm a little lost here, but I can still formulate the question: Why doesn't this work with grep as it does with text, and how might I get it to work the same way?

                        Thanks,
                        Roy McCoy
                        • 9. Re: CS3 Scripting Guide AppleScript
                          sstanleyau Level 4
                          "select text item i of myFoundItems" should be "select item i of<br />myFoundItems" -- a "text item" is a completely different thing.<br /><br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                          • 10. Re: CS3 Scripting Guide AppleScript
                            Roy McCoy Level 1
                            Though it works with text if not grep, yeah.

                            But hey - it looks like Script Debugger has changed all my Unicode
                            characters to question marks!

                            Bummer.

                            I suppose it's Unicode-friendly, and the Edit Menu help has "Special
                            Characters... Brings up the systemâs Character Palette for entering
                            Unicode characters." But I don't know what caused this highly unwanted
                            conversion, or how to avoid it.


                            Thanks,
                            Roy McCoy
                            • 11. Re: CS3 Scripting Guide AppleScript
                              sstanleyau Level 4
                              On 21/11/08 2:07 PM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> But hey - it looks like Script Debugger has changed all my Unicode<br />> characters to question marks!<br /><br />The current version isn't Unicode-aware. That should change in the very near<br />future.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                              • 12. Re: CS3 Scripting Guide AppleScript
                                Roy McCoy Level 1
                                Yagh! Looks like I'm immediately back to Script Editor then. It's
                                okay, I missed its nice green and red buttons anyway.

                                Problems problems problems. I'm dealing with three paragraph styles at
                                this point. The text is originally all in [Basic Paragraph]. I managed
                                to apply two of the three styles to their respective text okay, using
                                TeX codes deleted at the same time, but then I couldn't change [Basic
                                Paragraph] to the third style. I finally got it to select both [Basic
                                Paragraph] and one of the other two styles (why it did that I didn't
                                know, I only put the one), but then it looked like it wasn't going to
                                work anyway, as InDesign apparently doesn't like to look for nothing.

                                So I went back and started with:

                                set applied paragraph style of every paragraph of parent story of
                                selection to paragraph style "dr" of document 1

                                So the styles are now all applied okay. But I'm curious about an
                                apparent problem with [Basic Paragraph], and about how to do a
                                universal change from one style to another generally, seeing as how I
                                did it with the help of the TeX codes this time.

                                I've made considerable progress today (my sixtieth birthday) and am
                                very encouraged. I can get retrieve my Unicode things from backup
                                files tomorrow. Many thanks again for your invaluable help. If you
                                feel like putting me somewhere where other Applescripters are
                                pitching, just tell me where to go.


                                Roy McCoy
                                • 13. Re: CS3 Scripting Guide AppleScript
                                  sstanleyau Level 4
                                  Try using the apply paragraph style command. Also, when you get a bit of<br />time fire up Script Debugger, open ID's dictionary, and click on Explorer.<br />Lots of useful stuff...<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                                  • 14. Re: CS3 Scripting Guide AppleScript
                                    Roy McCoy Level 1
                                    > Try using the apply paragraph style command.

                                    Do I detect the slightest bit of sarcasm there,
                                    or am I just being paranoid? :-)

                                    Seriously, I think I'll be able to dig out the commands
                                    okay, but it's harder to figure out why it says it can't
                                    get something I think it should be able to get just fine,
                                    or why it can't display an apparently textual value as
                                    text, etc.

                                    > Also, when you get a bit of time fire up Script Debugger,
                                    > open ID's dictionary, and click on Explorer.
                                    > Lots of useful stuff...

                                    Dictionary, Explorer, yeah. Interesting that I can still
                                    use Script Debugger for some functions - at least for the
                                    next 17 or 18 days. After that I'll probably be waiting a
                                    long time for the dictionaries again (though if I get all
                                    those old, no-longer-working Classic applications off my new
                                    computer, that will help) and loping from suite to suite again,
                                    but happily the blue circles are at the top.


                                    Thanks,
                                    Roy McCoy
                                    • 15. Re: CS3 Scripting Guide AppleScript
                                      Eric @ MCA Level 3
                                      Generally I just snag properties directly from a selection, or at least use it as a clue for directions to look in the script dictionary.

                                      I start out with "get properties of selection", then start refining. Like, for example to figure out how to get the page number that an inline graphic occurs in:

                                      get properties of selection turns into
                                      get parent text frames of selection turns into
                                      get parent of item 1 of parent text frames of selection

                                      which eventually turned into this function (after hitting my first grouped text box, lol):

                                      on getParentPage(thisObject)
                                      
                                        tell application "Adobe InDesign CS2"
                                          if class of thisObject = page then
                                            return thisObject
                                          else
                                            return my getParentPage(parent of thisObject)
                                          end if
                                        end tell
                                      end getParentPage


                                      which I call with getParentPage(item 1 of parent text frames of thisInlineGraphic)
                                      • 16. Re: CS3 Scripting Guide AppleScript
                                        sstanleyau Level 4
                                        On 22/11/08 12:48 AM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> Do I detect the slightest bit of sarcasm there,<br />> or am I just being paranoid? :-)<br /><br />You're being paranoid. There are two ways to apply styles: using the<br />command, or setting the applied paragraph style property. The command gives<br />you control over overrides.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                                        • 17. Re: CS3 Scripting Guide AppleScript
                                          Roy McCoy Level 1
                                          > It looks to me like you've fond a rather nasty bug, and that the
                                          > results
                                          > returned from the change are inaccurate if there's a change in the
                                          > story
                                          > length.
                                          >
                                          > The workaround, if you want an accurate count, is to do a "find grep",
                                          > and base your count on the result from that.
                                          >
                                          > And it looks like the problem is still there in CS4.

                                          I've now installed and started using CS4 myself, and have noticed
                                          some problems, both scripting and not, that are likely not new. Not
                                          scripting, InDesign will say it made a replacement when it couldn't
                                          have, because the search text can't be found. Scripting, I don't seem
                                          to be able to search for a ^p paragraph ending at the end of a search
                                          string.

                                          Being now on CS4, and having read of an available CS4 Scripting Guide,
                                          I guess I'm set for yet another subject-line change and another round
                                          of Scripting Guide procrastination. Seems to kind of justify the
                                          previous
                                          procrastination, that's pleasant.


                                          Thanks,
                                          Roy McCoy
                                          • 18. Re: CS3 Scripting Guide AppleScript
                                            Roy McCoy Level 1
                                            On Nov 21, 2008, at 10:33 PM, Shane Stanley wrote:

                                            > You're being paranoid.

                                            Glad to hear it, thanks. At the moment:

                                            (1) Couldn't get this to work:

                                            set properties to {find text preferences:{find what:"a{'"}, change
                                            text preferences:{change to:"á"}}
                                            change text document 1
                                            set properties to {find text preferences:{find what:"A{'"}, change
                                            text preferences:{change to:"Ã"}}
                                            change text document 1
                                            set properties to {find text preferences:{find what:"a{`"}, change
                                            text preferences:{change to:"à"}}
                                            change text document 1
                                            set properties to {find text preferences:{find what:"A{`"}, change
                                            text preferences:{change to:"Ã"}}
                                            change text document 1

                                            It apparently kept changing a{` to acute á rather than the desired
                                            grave à - damnedest thing! I finally figured out that it actually
                                            wasn't changing a{` to á, but was catching it along with a{' so that
                                            it was already changed when the search for a{` occurred.

                                            I switched the order of the replaces - also at e/E, i/I, o/O and u/U -
                                            and the script now works. But it seems kind of dangerous. I don't
                                            suppose there's anyway to fix something so that there's no chance this
                                            could happen again, is there?

                                            (2) Another question I'm having is whether there's an important
                                            difference as far as TextWrangler and AppleScripting for InDesign are
                                            concerned, between "UTF-16 Little-Endian" and "UTF-16 Little-Endian,
                                            no BOM". I have to use Little-Endian, something told me, but sometimes
                                            I don't have a choice between the two variants. I can see at http://en.wikipedia.org/wiki/Byte_Order_Mark
                                            what "BOM" means, but not whether it matters for me and whether I
                                            want to avoid it or not.


                                            Thanks,

                                            Roy
                                            • 19. Re: CS3 Scripting Guide AppleScript
                                              sstanleyau Level 4
                                              On 28/11/08 6:11 AM, "Roy McCoy" <member@adobeforums.com> wrote:<br /><br />> Another question I'm having is whether there's an important<br />> difference as far as TextWrangler and AppleScripting for InDesign are<br />> concerned, between "UTF-16 Little-Endian" and "UTF-16 Little-Endian,<br />> no BOM". I have to use Little-Endian, something told me, but sometimes<br />> I don't have a choice between the two variants. I can see at<br />> http://en.wikipedia.org/wiki/Byte_Order_Mark<br />>   what "BOM" means, but not whether it matters for me and whether I<br />> want to avoid it or not.<br /><br />BOMs tend to matter only when you're moving stuff between systems using<br />different endian processors, eg Intel vs PPC. It's a pity in a way that BOMs<br />aren't used more widely, but much of the industry seems stuck in good-enough<br />mode with ASCII variants. if you're not seeing text get garbled, you're<br />fine.<br /><br />-- <br />Shane Stanley <sstanley@myriad-com.com.au><br />AppleScript Pro Florida, April 2009 <a href=http://scriptingmatters.com/aspro>
                                              • 20. Re: CS3 Scripting Guide AppleScript
                                                Roy McCoy Level 1
                                                Shane Stanley wrote:

                                                > BOMs tend to matter only when you're moving stuff between systems
                                                > using
                                                > different endian processors, eg Intel vs PPC. It's a pity in a way
                                                > that BOMs
                                                > aren't used more widely, but much of the industry seems stuck in
                                                > good-enough
                                                > mode with ASCII variants. if you're not seeing text get garbled,
                                                > you're
                                                > fine.

                                                I did get some garbled text the other day, but I don't think it was a
                                                BOM issue. Setting the TextWrangler encoding preferences for Little-
                                                Endian seems to occasion some problems, mainly undesired alerts, so I
                                                changed them back to Western (Mac OS Roman). As far as I can remember,
                                                I needed Little-Endian only for doing something with ID tagged text
                                                that I'm now done with for the time being.

                                                Hey, here's one I managed to do on my own:

                                                *****

                                                tell application "Adobe InDesign CS4"
                                                activate
                                                set myDocument to make document
                                                set myMarginPreferences to margin preferences of myDocument
                                                set bottomMargin to bottom of myMarginPreferences
                                                set bottomYPoint to (page height of document preferences of
                                                myDocument) - bottomMargin
                                                set rightMargin to right of myMarginPreferences
                                                set rightXPoint to (page width of document preferences of myDocument)
                                                - rightMargin
                                                tell page 1 of myDocument
                                                set myTextFrame to make text frame with properties {geometric bounds:
                                                {top of myMarginPreferences, left of myMarginPreferences,
                                                bottomYPoint, rightXPoint}}
                                                select insertion point -1 of myTextFrame
                                                end tell
                                                end tell

                                                *****

                                                I suppose that's what you'd call trivial, but I didn't have it before
                                                and now I do - assigned to cmd-n, understandably.

                                                It wasn't easy for me and I blew too much time on it. I think the main
                                                problem was that I kept hitting this point:

                                                tell application "Adobe InDesign CS4"
                                                activate
                                                set myDocument to make document
                                                set myMarginPreferences to margin preferences of myDocument
                                                tell page 1 of myDocument
                                                set myTextFrame to make text frame with properties {geometric bounds:
                                                {top of myMarginPreferences, left of myMarginPreferences, (page height
                                                of document preferences of myDocument) - (bottom of
                                                myMarginPreferences), [...]}}

                                                at which it would keep telling me:

                                                Canât make «class marb» of «class impp» of document "Untitled-
                                                [n]" of application "Adobe InDesign CS4" into type number.

                                                I couldn't figure out what was going on there, and still can't. There
                                                wasn't any problem with top of myMarginPreferences. bottom of
                                                myMarginPreferences had to be a number, in order to work with top of
                                                myMarginPreferences which evidently was text, and, uh... confused again!

                                                Oh... top of myMarginPreferences is text, while
                                                page height of document preferences of myDocument is a number, right?
                                                If so, that doesn't seem to be intuitively evident, exactly.


                                                Thanks,

                                                Roy
                                                • 21. Re: CS3 Scripting Guide AppleScript
                                                  Roy McCoy Level 1
                                                  Eric,

                                                  Could you please send me a script containing the getParentPage function you posted on Nov. 21? I didn't quite get it but I think I might if I saw it in the context in which it was used.

                                                  Thanks,

                                                  Roy McCoy
                                                  roymccoy dot nl at gmail dot com