6 Replies Latest reply on Aug 15, 2018 8:37 AM by grudgemom

    Run Applescript on Selection/Story/Document

    grudgemom Level 1

      Hi all

       

      Is it possible to tell an Applescript to run on the selected text only? If so, please tell me how!

       

      My script (condensed version):

       

       

      tell application "Adobe InDesign CC 2018"

       

        my GrepSearch("(January|February|March|April|May|June|July|August|September|October|November |December) (\\d{1}|\\d{2}), (\\d{4})")

        my GrepSearch("(January|February|March|April|May|June|July|August|September|October|November |December) (\\d{4})")

       

      end tell

       

      on GrepSearch(f)

        tell application "Adobe InDesign CC 2018"

        set find grep preferences to nothing

        set change grep preferences to nothing

        set find what of find grep preferences to f

        set no break of change grep preferences to true

        change grep

        end tell

      end GrepSearch

       

      (searching for date strings and telling ID to apply No Break character attribute to the strings)

       

      Thank you in advance!

        • 1. Re: Run Applescript on Selection/Story/Document
          Robert Kyle Level 2

          Well, it's been a long time since I've written any AppleScript, but the javascript syntax looks like this:

           

          app.selection[0].changeGrep;

           

          (which assumes that the user has selected some text beforehand. always a good idea to test that)

           

          In your script, the "change grep" line is within a "tell application" block, which gives it a broad scope. Your solution will be something like:

           

          tell selected text

               change grep

          end tell

           

          Or maybe it's "tell selection"?

           

          Bob

          • 2. Re: Run Applescript on Selection/Story/Document
            greenrookie Level 3

            Something like this (as Rob mentioned, best to test if you have selected text first)

             

            tell application "Adobe InDesign CC 2018"

                      set mySelection to selection

                               tell mySelection

                                         set myFoundItems to change grep

                               end tell

            end tell

            1 person found this helpful
            • 3. Re: Run Applescript on Selection/Story/Document
              Robert Kyle Level 2

              "Selection"! I do miss Applescript.

              • 4. Re: Run Applescript on Selection/Story/Document
                S Hopkins Adobe Community Professional

                Be careful. Remember selection returns a list and can return a number of item types. Check for the class of the item selected and then if "text" pass that to the handler and use its reference for your change grep. Also for the change grep preference I prefer using a character style set to no break. That way the modified text does not show up as an override.

                 

                set myRegEx to ("(January|February) (\\d{4})")

                tell application "Adobe InDesign CC 2018"

                  set selList to selection

                  set selItem to item 1 of selList

                  if class of selItem is text then

                set selObj to a reference to selItem

                  my doRegEx(selObj, myRegEx)

                  end if

                end tell

                • 5. Re: Run Applescript on Selection/Story/Document
                  grudgemom Level 1

                  I've already run into problems using a NoBreak character style because ID doesn't let us stack multiple character styles.

                   

                  (Example, I can't apply a style called Bold and a style called Italic at the same time. I need to create a 3rd style called Bold Italic. So when I have text that already has a Bold character style applied, then apply a NoBreak character style with a script, it removes the Bold style and I get my knuckles rapped for screwing up the formatting in our documents.)

                  • 6. Re: Run Applescript on Selection/Story/Document
                    grudgemom Level 1

                    These solutions are beyond my mental capacity at the moment, but thank you all for responding. At least this thread will be archived in perpetuity in case anyone else is ever looking for this. greenrookie, Robert Kyle, and S Hopkins.