Select parent of a text flow (InDesign CS2/Tiger)

Hello -

Trying to do something which I think should be simple. InDesign CS2/Tiger. I have a text frame, with an insertion point inside the text frame. I want to select the text frame. (Needless to say, this is part of a bigger script; I could just click on the selection tool if I wanted to do this normally.)

I try doing
select parent of selection

set x to id of parent of selection
select page item x

etc.

and it’s not working.

The object reference of the selected insertion point is

insertion point 1 of text flow id 169 of document “Untitled-1” of application “Adobe InDesign CS2”

which doesn’t show the text frame.

The object reference of the text frame is

text frame id 187 of page id 159 of spread id 154 of document “Untitled-1” of application “Adobe InDesign CS2”

which doesn’t help with the insertion point.

I don’t want to use keystroke because that’d mean that assistive whatsit needs to be turned on, which it can’t be.

How do I select the parent text frame of an insertion point? There wouldn’t be any other linked text frames; it’s just a single, standalone text frame, containing nothing more than an insertion point.

Help much appreciated - 2.5hrs already spent fiddling to no avail.

tell application "InDesign CS"
	set x to selection
	set y to parent of item 1 of x
	set z to parent text frame of item 1 of x
end tell

Make a document with a text frame, select some of the text and run the script. The selection returned is a list with one entry, so I am refering to item 1 of the selection to get the object reference for the parent statements. The parent of the text is the stroy (text flow), asking for the parent text frame of the selection returns the text frame. I don’t think this is well documented so don’t feel bad that you didn’t find it.

Thanks for the reply, but I’m not convinced it’s working…

set x to selection

OK - if I get the object reference of this it says ‘insertion point 1 of text flow id 218 of document “Untitled-1” of application “Adobe InDesign CS2”’

set y to parent of item 1 of x

If I get the object reference of this it says ‘text flow id 218 of document “Untitled-1” of application “Adobe InDesign CS2”’

set z to parent text frame of item 1 of x

And if I get the object reference of this it returns blank. (Did you mean parent text frame of item 1 of y? That’s also blank.)

I’m finding it hard to work out how to get the text frame. I don’t think I can do it with ‘parent of’ because ‘text frame’ isn’t listed as anything in the object reference. The only way I can think of doing it is to get the content, then go through the document and find the text frame which contains that content (which is a tedious and roundabout way of doing it…)

?

Hamish

All you should need it this:

tell application "InDesign CS"
	set x to selection
	set z to parent text frame of item 1 of x
end tell

As I run this on a document with text selected in CS I get the following events:

tell application "InDesign CS"
	get selection
		{insertion point 4 of text flow id 136 of document "Untitled-1"}
	get parent text frame of insertion point 4 of story id 136 of document "Untitled-1"
		text frame id 154 of page id 127 of spread id 122 of document "Untitled-1"
end tell

The parent text frame that you want if for the selection, since the text flow could be spread out over many text frames, and could be “outside” a text frame in the case of overflow text. I don’t have CS 2 here to test it with, and it is possible that Adobe changed the way this works in that but I don’t think so. Without seeing your code I can’t tell if there is another problem that is keeping this from working. Again, I don’t think that this is well documented, and no the parent text frame of the insertionpoint (selection) is not part of the properties, but again it does work on my computer under InDesign CS.

Ho-hum. That doesn’t appear to work in CS2. Nice one, Adobe.
I guess I’ll just have to work out a different way of doing what I was trying to do. Many thanks for your help, though…
Hamish

If you post your code I can take a look at it later. I have CS2 at work and will double check, but there might be something else in the way your script is written that is causing the trouble.

I was using your code on its own - I thought I’d give it a try outside of my script.

tell application "Adobe InDesign CS2"
   get parent text frame of selection
end tell

I’ve got it to work if I select the text boxes with the selection tool, rather than with a cursor inside the box. Annoying, because I didn’t want to do it like that, but it works…

OK, there was a change between CS and CS2 in the syntax for this. The reference is now plural, so:

tell application "Adobe InDesign CS2"
	set ParentFrame to parent text frames of item 1 of selection
end tell

will return a list of the parent text frames. This will allow the return of multiple text frames if the selection crosses two or more text frames. If you just have the cursor placed in the selected text box then it will return the text box that the cursor is in.

Note: both the selection and parent text frames lines return a list of object references. This happens even if there is only one item in the list, so you need call these references in list form and that is why I am using item 1 of selection to reference the text selected. Also to use the variable ParentFrame you need to remember to reference it as a list, since it may have more than one item in it, so you would also need to use them item 1 of ParentFrame. Of course you could set the list variable to the object reference as in the following code:

tell application "Adobe InDesign CS2"
	set ParentFrame to parent text frames of item 1 of selection
	if (count of ParentFrame) is 1 then set ParentFrame to item 1 of ParentFrame
end tell

Hope this helps, and that you didn’t spend too much time trying to find a work around.

Oh for goodness’ sake. All that for the letter ‘s’? Sigh.
Thank you very much indeed for your sleuthing - it really is very much appreciated. The workaround I did will now do what I wanted to do on multiple text boxes, but I’d still like it for a case of a single text box, so I’ll use this too.

For those interested (if anyone!) the code I’m now using is below. What happens:

  • You have several (text) boxes on a page.
  • You type in up to 17 characters - digits, spaces and Xx - into each one.
  • You select all the boxes with the selection tool.
  • You run the script.
  • The script checks each item of the selection and, if it’s a text frame, looks in the folder which you specify at the prompt to find a .tif file which contains the digits which you entered (after removing spaces). If it finds one, it removes the text, changes the box to a picture frame, places the tif, resizes it proportionally, then resizes the box to fit.

Hamish

choose folder with prompt "Choose the folder which holds the images."
set ChosenFolder to the result

tell application "Adobe InDesign CS2"
	activate
	
	try
		tell document 1
			
			repeat with i from 1 to (count of (every item in selection))
				
				if ((class of item i of selection) as string) is equal to "text frame" then
					
					set thesel to contents of item i of selection
					if length of thesel is less than or equal to 17 then
						
						set nospaces to ""
						set alldigits to "1234567890Xx"
						
						repeat with p from 1 to (length of thesel)
							
							if character p of thesel is in alldigits then
								set nospaces to nospaces & character p of thesel
							end if
							
						end repeat
						
						if length of nospaces is less than 14 and length of nospaces is greater than 4 then
							
							
							set imfound to 0
							set imagename to ""
							
							repeat with x in (list folder ChosenFolder)
								if (x as string) ends with ".tif" then
									if imfound is equal to 0 then
										if (offset of nospaces in (x as string)) as text is not equal to "0" then
											set imagename to x
											set imfound to 1
										end if
									end if
								end if
							end repeat
							
							
							if (imagename is not equal to "") then
								
								delete characters 1 through -1 of item i of selection
								set y to id of item i of selection
								set properties of page item id y to {content type:graphic type}
								
								place ((ChosenFolder & imagename) as string) on page item id y
								tell page item id y
									fit given proportionally
									fit given frame to content
								end tell
								
								
							end if
							
						end if
						
					end if
					
				end if
				
				
				
			end repeat
		end tell
	end try
end tell