InDesign select image inside selected rectangle help needed!!

I need to write a script that will take an image from a selected rectangle, cut or copy it, paste that image in place and then delete the originally selected rectangle. The following script works, but it means I have to have the image I want selected, instead of the parent rectangle.

tell application "Adobe InDesign CS4"
	activate
	
	set myDocument to active document
	
	
	set selectedRectangles to selection of active document
	
	
	repeat with i from 1 to count of selectedRectangles
		set deletedRectangle to parent of item i of selection
		set myimage to item i of selection
		cut myimage
		paste in place
		delete deletedRectangle
	end repeat
	
	display alert "Done"
	
end tell

This is my attempt that is giving me errors:

tell application "Adobe InDesign CS4"
	activate
	
	set myDocument to active document
	
	
	set selectedRectangles to selection of myDocument
	
	
	repeat with i from 1 to count of selectedRectangles
		set myFrame to rectangle i of selection
		tell myFrame
			select image 1 of myFrame
			copy
			paste in place
		end tell
		get all graphics of myFrame
		cut myimage
		paste in place
		delete myFrame
	end repeat
	
end tell

I get the following error: Adobe InDesign CS4 got an error: Can’t get image 1 of rectangle id 217 of rectangle id 208 of page id 191 of spread id 186 of document “Untitled-1”.

I have also tried different variations getting errors such as image 1 of myFrame doesn’t understand the select command.

Any help would be much appreciated!

Adam

try this:

tell application "Adobe InDesign CS4"
	activate
	
	set myDocument to active document
	
	
	set selectedRectangles to selection of myDocument
	
	repeat with i from 1 to count of selectedRectangles
		set myFrame to item i of selectedRectangles
		set selection to image 1 of myFrame
		copy
		paste in place
		delete myFrame
	end repeat
	
end tell

Thanks for the reply but when I try your revision I get the following error: Adobe InDesign CS4 got an error: Invalid value for set property ‘selection’. Expected list of objects, object or nothing, but received nothing.

That doesn’t make sense because I selected a rectangle that has an image pasted into it. I ran a get properties for myFrame and it returned the following:
get properties of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”
→ {triggered from script:false, nested in script:false, component id:“”, display error dialogs:false, demo days left:2.147483647E+9, is demo:false, event code:“”, event filter:“”, java script:“”, observers:{scripted plugin id 376 of document “Untitled-2”}, subject script tag filter:“”, subjects:{}, use debugger:false, absolute rotation angle:0.0, absolute horizontal scale:100.0, absolute shear angle:0.0, absolute vertical scale:100.0, id:311, overridden:false, flip:none, associated XML element:nothing, all graphics:{PostScript picture id 315 of rectangle id 312 of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”}, all page items:{rectangle id 312 of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”}, applied object style:object style id 117 of document “Untitled-2”, content type:graphic type, end cap:butt end cap, end join:miter end join, fill tint:-1.0, fill color:color id 11 of document “Untitled-2”, absolute flip:none, gap color:swatch id 14 of document “Untitled-2”, gap tint:-1.0, geometric bounds:{3.905555555556, 0.625, 6.794444444444, 5.8125}, InCopy export options:InCopy export options of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, rotation angle:0.0, lock state:none, stroke tint:-1.0, left line end:none, stroke color:swatch id 14 of document “Untitled-2”, stroke weight:0.0, miter limit:4.0, nonprinting:false, overprint fill:false, overridden master page item:nothing, anchored object settings:anchored object settings of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, frame fitting options:frame fitting options of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, local display setting:default, shear angle:0.0, parent:page id 275 of spread id 270 of document “Untitled-2”, corner option:none, corner radius:0.166666666667, gradient fill angle:0.0, gradient fill length:0.0, gradient stroke angle:0.0, gradient stroke length:0.0, gradient stroke start:{-0.8125, 7.461111111111}, gradient fill start:{-0.8125, 7.461111111111}, horizontal scale:100.0, index:2, item layer:layer id 262 of document “Untitled-2”, locked:false, object reference:rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, label:“”, vertical scale:100.0, right line end:none, story title:“”, stroke alignment:center alignment, stroke type:stroke style id 23081 of document “Untitled-2”, text wrap preferences:text wrap preferences of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, visible bounds:{3.905555555556, 0.625, 6.794444444444, 5.8125}, content transparency settings:content transparency settings of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, fill transparency settings:fill transparency settings of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, transparency settings:transparency settings of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”, stroke transparency settings:stroke transparency settings of rectangle id 311 of page id 275 of spread id 270 of document “Untitled-2”}

Sorry for the gobbledy gook, but I thought it may help. The image inside the rectangle is a postscript image (not sure that means anything). Thanks for your help!

The problem is with this line:

set selection to image 1 of myFrame

Since you have an esp (I assume) it is referenced as a PostScript picture and not an image. A PDF would have a similar problem since InDesign references a PDF as a “PDF.” It should be a simple fix by changing the line to read:

set selection to item 1 of myFrame

This works because you are returned a list of object reference to the graphics placed in the picture frame, so you can use that instead of the image call.

Hi. What purpose does this script serve? The copy/paste approach strikes me as being inefficient in any scenario. Are you just trying to strip attributes from the original container?

Jerome, using item 1 instead of image 1 just duplicates the selected rectangle. It isn’t grabbing the image inside the parent rectangle for some reason.

Marc Anthony, the purpose of this script is to pull out an image (or a group of images) that have been pasted into a rectangle. This is just a repetitive task in my job that I figured would be easily scriptable. I have gotten a version of this to work when I direct select the image I want and run the first script in my first post.

So what happens is that I import two images into a document, group them together, create a rectangle to the dimensions I need it to be, cut the group of images and paste into the rectangle. Down the line, there is sometimes a need to pull those images back out (making sure they stay in the same location) and delete the “parent” rectangle.

I hope this helps and thanks for your suggestions.

Adam

You are correct, it seemed to be working so I didn’t read the events closely enough. The change should be:

set selection to item 1 of all graphics of myFrame

Of course if you have a group of items pasted into the frame then you would need to add in a loop to select each one and copy/paste the items before deleting the parent frame. A quick adjustment to the script gives me:

tell application "Adobe InDesign CS4"
	--activate
	set myDocument to active document
	set selectedRectangles to selection of myDocument
	
	repeat with i from 1 to count of selectedRectangles
		set myFrame to item i of selectedRectangles
		set theGraphics to all graphics of myFrame
		repeat with aGraphic in theGraphics
			set selection to aGraphic
			copy
			paste in place
		end repeat
		
		delete myFrame
	end repeat
end tell

however in testing it there seems to be a problem, the graphics are duplicated by the original parent frame is not being deleted. I have to run out but will try to find some time later to look at it again.

Jerome, This is fantastic. I don’t know why the frame wasn’t getting deleted on your end, but for my test document it worked wonderfully. Thanks again for your help!

I found out the problem I was having with the frame not being deleted, and it was Operator Error (OE). Somehow I had a duplicate of the frame/image behind the ones I selected for testing so when the script ran it deleted the correct one but the one behind it was still there.