Skip to main content
grudgemom
Known Participant
July 31, 2018
Question

Run Applescript on Selection/Story/Document

  • July 31, 2018
  • 4 replies
  • 1398 views

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!

This topic has been closed for replies.

4 replies

grudgemom
grudgemomAuthor
Known Participant
August 15, 2018

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​.

Inspiring
August 5, 2018

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

grudgemom
grudgemomAuthor
Known Participant
August 15, 2018

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.)

Inspiring
August 2, 2018

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

Inspiring
August 3, 2018

"Selection"! I do miss Applescript.

Inspiring
August 1, 2018

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