Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

applescript - grep

New Here ,
Jun 11, 2012 Jun 11, 2012

I'm looking for examples of using grep with InDesign CS5.5.

I have a document that may or may not have one or more lines that contains an asterisk and a one or two digit number followed by a space.

I want to capture the two numbers, change the line from the asterisk to the end of the line to the point size of the numbers, then remove the asterisk and the two numbers and the space.

example:

100 Anystreet, *8 Suite 105

should become

100 Anystreet, Suite 105

I'm not sure if this is possible.  Any input??

TOPICS
Scripting
4.7K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 11, 2012 Jun 11, 2012

Sure it's possible, just not with GREP alone, as it can't replace text with a point size that's in text. I suppose that's why you suggested AppleScript.

How many different sizes do you need? If this is a limited number, you could do it with GREP styles -- one per font size, plus an additional one to hide the combo *(size) from view. A definite plus would be that changing the number immediately will change the text size. Downside would be you need a blindingly fast computer as soon as you cross over some magical threshold of number of GREP styles. I used to think they only got calculated once and then cached, somewhere in each paragraph bowels, but I found out the *very* hard way that's not the case. It seems all of them get re-calculated for your entire document on an event trigger as little as a mouse click ...

I don't know AppleScript, but in Javascript it could be done as simple as:

1. search for asterisk, wildcard digits, a space, and then anything else up to the end of a line.

2. apply font size in that digit to the found string.

3. delete asterisk plus digits plus space.

4. repeat until satisfied.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 11, 2012 Jun 11, 2012

Thank you for that, Jongware.

I'm hoping that it can be as simple in AppleScript as it sounds like it could be in JavaScript.  I don't know javascript very well, as was hoping to write the application I'm working on wholly in applescript to get some experience.

Anyone else have any suggestions???

Any ideas on where to look???

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Jun 14, 2012 Jun 14, 2012

The below code may will help you....

app.findGrepPreferences.findWhat = "\\*[0-9]+[ ]"

app.changeGrepPreferences.changeTo = "";

app.activeDocument.changeGrep();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jun 16, 2012 Jun 16, 2012

Here below is sample code of FindChangeByList.applescript which I think help you to understand.

on myFindGrep(myObject, myFindPreferences, myChangePreferences, myFindChangeOptions)

          tell application "Adobe InDesign CS6"

                    --Clear the find grep/change grep preferences before each find/change operation.

                    set find grep preferences to nothing

                    set change grep preferences to nothing

                    set myScript to "tell application \"Adobe InDesign CS6\"" & return & "set properties of find grep preferences to " & myFindPreferences & return

                    set myScript to myScript & "set properties of change grep preferences to " & myChangePreferences & return

                    set myScript to myScript & "set properties of find change grep options to " & myFindChangeOptions & return

                    set myScript to myScript & "end tell" & return

                    do script myScript language applescript language

                    tell myObject

                              set myFoundItems to change grep

                    end tell

                    --Clear the find grep/change grep preferences after each find/change operation.

                    set find text preferences to nothing

                    set change text preferences to nothing

          end tell

end myFindGrep

Shonky

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 18, 2012 Jun 18, 2012

Shonkyin -

Thank you.  Very helpful.

What wound up working for me as shown below.  Not very elegent, but effective so far:

set fixPointSize to "YES"

repeat while fixPointSize = "YES"

          tell application "Adobe InDesign CS5.5"

                    set find grep preferences to nothing

                    set change grep preferences to nothing

                    set find what of find grep preferences to "(?i)\\*[0-9][0-9]*.*$"

                    tell active document

                              set myFoundItems to find grep

                    end tell

                    if (count myFoundItems) is not equal to 0 then

                              set myItem to (contents of item 1 of myFoundItems) as string

                              set myItemPointSize to _string's stringBetween(myItem, "*", " ")

                              set myItemChangeTo to _string's rightString(myItem, "*" & myItemPointSize & " ")

                              set find what of find text preferences to myItem

                              set change to of change text preferences to myItemChangeTo

                              set point size of change text preferences to myItemPointSize

                              tell active document

  change text

                              end tell

                    else

                              set fixPointSize to "NO"

                    end if

          end tell

end repeat

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Aug 28, 2015 Aug 28, 2015
LATEST

                    --Clear the find grep/change grep preferences after each find/change operation.

                    set find text preferences to nothing

                    set change text preferences to nothing

I may correct:

set find grep prefereces to nothing

set change grep prefereces to nothing

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines