InDesign GREP issue

Hey guys, i basically know how scripting GREP works. I just cant handle it to let grep only work inside of a specified text frame. Find and replace string.

The idea is

tell application "Adobe InDesign CS6"
--grep reset here
--grep settings here
Tell active document
Tell page 1
Set mytextframe to item 1 of every item whose label is "mytextframe"
-- grep action here... but didnt find out how to get the right reference
End tell
End tell 
End tell

Any help appreciated
John

grep works on text.

You’re missing a “parent story of”.

Hi. The ref should be a specific text frame, or possibly its text (or story). I don’t have access to that version of ID, so I can’t test for you, but try:


--possibly this, if CS6 allows it directly upon a frame:
find grep (change grep) mytextframe
--more likely this:
find grep (change grep) (((text frames whose label is "mytextframe")'s item 1)'s text)

Download Adobe’s latest scripting guide and fiddle around with it, if that’s not exactly right.

Usually I find it helpful to tell the text frame to do the grep change, a bit like this (untested):

tell application "Adobe InDesign CS5"
      set foundItemsList to {}
      set mytextframe to item 1 of every item whose label is "mytextframe"
      tell mytextframe
      set find what of find text preferences to (findObject as string)
      set change to of change text preferences to (replaceObject as string)
      tell mytextframe
            set foundItemsList to change text
      end tell
end tell

The danger of addressing a text frame rather than its parent story is that it will miss any overset text. If you’re doing a series of changes, that can easily happen.

Yes, I meant to say " tell the object to do the grep change". in this case it would indeed be the parent story of the text frame, as Shane said.

This one works, but i cant manage id to access the parent story of the specific text frame

set findObject to "X"
set replaceObject to "Y"
tell application "Adobe InDesign CS6"
	set find what of find text preferences to (findObject as string)
	set change to of change text preferences to (replaceObject as string)
	set foundItemsList to {}
	tell active document
		set mytextframe to item 1 of every page item whose label is "Test"	
		-- set foundItemsList to change text of parent story of mytextframe    <--- DOESNT WORK
		set foundItemsList to change text of mytextframe  -- <-- WORKS
	end tell
end tell

Don’t use “of” after change text: “change text parent story of mytextframe”.

Unfortunately that doesnt work either …


parent story of {text frame id 231 of spread id 198 of document id 1 of application \"Adobe InDesign CS6\"}"cannot be read.
set findObject to "X"
set replaceObject to "Y"
tell application "Adobe InDesign CS6"
	set find what of find text preferences to (findObject as string)
	set change to of change text preferences to (replaceObject as string)
	set foundItemsList to {}
	tell active document
		set mytextframe to item 1 of every page item whose label is "Test"
		return mytextframe
		-- returns {text frame id 231 of spread id 198 of document id 1 of application "Adobe InDesign CS6"}
	end tell
end tell

This is weird. If i use variables it doesnt work, if i dont it works.

set findObject to "X"
set replaceObject to "Y"
tell application "Adobe InDesign CS6"
	set find what of find text preferences to (findObject as string)
	set change to of change text preferences to (replaceObject as string)
	set foundItemsList to {}
	tell active document
		--WORKS
		set mystory to parent story of item 1 of every page item whose label is "Test"
		--DOESNT WORK
		--set mytextframe to item 1 of every page item whose label is "Test"
		--set mystory to parent story of mytextframe
		set foundItemsList to change text of mystory		
	end tell
end tell

As a general rule of thumb, don’t variablize anything that doesn’t need to be”such as values that are only called once”and you avoid that problem, as references will stay in their proto-form. It’s too easy to either dereference or double-reference something, creating inert items.

 
set findObject to "X"
set replaceObject to "Y"
tell application "Adobe InDesign CS6"
   set find what of find text preferences to (findObject)--already text class
   set change to of change text preferences to (replaceObject)
  --set foundItemsList to {}--redundant
   tell active document 
    set foundItemsList to (parent story of item 1 of every page item whose label is "Test")'s change text
end
end

P.S. This is a vanilla find/change”unrelated to GREP.

Argh… the code works but it doesnt do exactly what i need …
to limit the search/replace or grep function to one single page.

I got a document with 1000 pages.
Each page contains a text field with the label “text”
The script loops through the pages an should replace a string inside the Textframe with label “text”. But each page receives a different replaceString.

But the script above immidiately replaces the strings in the full document, all pages.

Is there any way to limit the search/replace to one single page?

Doesnt work:

tell application "adobe Indesign CS6"
     tell active document
          tell page 1 -- <-- Grep only works with document i think
               change grep
          end
     end
end

Hi. Yes, but you limit that search by specifying the object through the hierarchy”merely telling the page isn’t granular enough.
Here is one possible method of a multiple search and replace:

set changeList to {{"first thing", "2nd thing"}, {"whatever", "something else"}} --populate with same pair count as loop count

	tell application "Adobe InDesign CS6" 
--insert value-clearing statements
	repeat with Numbr from 1 to 1000 --loop count
		set {find text preferences's find what, change text preferences's change to} to {changeList's item numbr's item 1, changeList's item numbr's item 2}
 change text ((document 1's page numbr's text frame 1 whose label is "Test")'s parent story)
		end repeat
	end tell

Here is the link to the Adobe guide.
http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/indesign/sdk/cs6/scripting/InDesign_ScriptingGuide_AS.pdf