Locking images that are in an InDesign document more than once

I’m writing this script that duplicates images, changes the height of the duplicate image and adds a drop shadow to the duplicate image. The problem is some of the documents I’m working with will have some images that are already duplicated and have a drop shadow on the duplicate. I want the script to look through the document and lock all images that are in the document more than once. Then, once those images are locked, it can duplicate and create drop shadows for the unlocked images.

I have everything working EXCEPT the script only locks the duplicate image and not the original image. When it counts the images, it thinks there is only one image the first time it comes across the name, so it doesn’t lock the first image. Since, one copy isn’t locked, it creates a second drop shadow for the images that already had one. How do I change the part that checks how many times the image is in the document so that it locks BOTH copies of the image and not just one?

on open of droppedFiles --> This is the line of code that begins a droplet. 
	
	
	tell application "Adobe InDesign CC 2014"
		
		repeat with afile in droppedFiles
			open afile
			
			-----------------------------------------------------------------------------------------		
			
			
			--CREATING A DROP SHADOW OBJECT STYLE TO BE APPLIED LATER ON
			
			tell document 1
				
				try
					set dropShadowStyle to object style "Drop Shadow"
				on error
					--The character style did not exist, so create it.
					set dropShadowStyle to make object style with properties {name:"Drop Shadow", enable drop shadow:true, enable transparency:true, enable stroke:false}
					
					tell dropShadowStyle
						--Applying the drop shadow to the text
						tell transparency settings
							set properties of drop shadow settings to {mode:drop, use global light:true, opacity:40, spread:10.0, blend mode:multiply, noise:1.0, x offset:".02", y offset:".02", distance:".04", size:".04", effect color:"Black"}
						end tell
					end tell
				end try
				
				---------------------------------------------------------------
				
				
				--COUNTING THE NUMBER OF TIMES AN IMAGE IS IN THE DOCUMENT
				set TheGraphics to all graphics
				set theLinksList to {}
				
				repeat with n from 1 to (count TheGraphics)
					set TheGraphic to item n of TheGraphics
					set theLink to item link of TheGraphic
					set linkName to name of item link of TheGraphic as string
					set theLinksList to theLinksList & linkName
					set matchCount to my count_matches(theLinksList, linkName)
					
					--Getting the full path to each link to be used later on in thes script
					set linkPath to file path of theLink as string
					
					--other information that will be used later on in the script
					set theImage to parent of theLink
					set theFrame to parent of theImage
					
					-----------------------------------------------------------------------------------------	
					
					
					--DUPLICATING PRODUCT IMAGES AND ADDING A DROP SHADOW IF THE FRAME THE IMAGE IS IN DOESN'T ALREADY HAVE A DROP SHADOW
					
					if matchCount is greater than 1 then
						set locked of theFrame to true
					end if
					
					
					if linkPath contains "1_4c IMAGES" then --Choosing everything that has 1_4c IMAGES in the path of it's link (basically all the product images)
						
						--Getting coordinates of each side of the frame to be used in creating the duplicated frame for the drop shadow
						--Geometric bounds is {top of frame, left side of frame, bottom of frame, right side of frame} or {item 1, item 2, item 3, item 4}
						tell theFrame
							set topOfFrame to item 1 of geometric bounds
							set leftSideOfFrame to item 2 of geometric bounds
							set bottomOfFrame to item 3 of geometric bounds
							set rightSideOfFrame to item 4 of geometric bounds
							set frameHeight to bottomOfFrame - topOfFrame as number --Getting height to be used later on in removing short frames with drop shadows
							set offsetTopOfFrame to bottomOfFrame - 0.05 --This is the coordinate for the top of the duplicated frame that contains the drop shadow. We want it to be only slightly above the bottom of the frame.
						end tell
						
						
						--Duplicating the product image frame and resizing so it is only .05" tall instead of the full height of the image
						--We just want a short little box at the bottom of the image for the drop shadow
						if locked of theFrame is false then
							set duplicateFrame to duplicate theFrame
							set geometric bounds of duplicateFrame to {offsetTopOfFrame, leftSideOfFrame, bottomOfFrame, rightSideOfFrame}
							
							
							--Sending the image frame with the drop shadow back one step
							send backward duplicateFrame
							
							
							--Applying the drop shadow to the duplicated image frame
							tell duplicateFrame
								apply object style using dropShadowStyle
							end tell
						end if
						
						
					end if
				end repeat
				
				
				close saving yes
				
				
				
			end tell
		end repeat
	end tell
end open







on count_matches(this_list, this_item)
	set the match_counter to 0
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then ¬
			set the match_counter to the match_counter + 1
	end repeat
	return the match_counter
end count_matches

i think your matchCount is not well placed. in your loop you dont know at this time if the image/graphic is placed more than once.
you need an extra loop in front of your loop (before your loop) to check if images are placed more than once. you can store the infos in a record or something and read them out in your main loop afterwards. or fill two lists and exclude double items, etc…
so, before you start your main loop (make dropshadow) you must have a proper list of images/frames to work with.

Sweet. :slight_smile: I figured it out. What I did was build a list that has all the links, two lists that contain just the links that are in the document more than once, and a final list which extracts duplicates from the former two lists so that it returns just one copy of each duplicated link in the list. Then, I told it to lock the frame if the name of it’s link was in the final list.

on open of droppedFiles --> This is the line of code that begins a droplet. 
	with timeout of 600 seconds --Sets timeout to 10 minutes.
		
		tell application "Adobe InDesign CC 2014"
			
			repeat with afile in droppedFiles
				open afile
				
				
				-----------------------------------------------------------------------------------------		
				
				
				--CREATING AN OBJECT STYLE TO BE APPLIED LATER ON
				
				tell document 1
					
					try
						set dropShadowStyle to object style "Drop Shadow"
						
						
					on error --The character style did not exist, so create it.
						
						set dropShadowStyle to make object style with properties {name:"Drop Shadow", enable drop shadow:true, enable transparency:true, enable stroke:false}
						
						tell dropShadowStyle
							tell transparency settings
								set properties of drop shadow settings to {mode:drop, use global light:true, opacity:40, spread:10.0, blend mode:multiply, noise:1.0, x offset:".02", y offset:".02", distance:".04", size:".04", effect color:"Black"}
							end tell
						end tell
					end try
					
					---------------------------------------------------------------
					
					
					
					
					------------------------------------------------------------------------
					
					--BUILDING A LIST OF THE GRAPHICS THAT ARE IN THE DOCUMENT MORE THAN ONCE
					
					--Looping through every graphic to build a list that contains the name of every link
					set originalList to {}
					set allGraphics to all graphics
					repeat with i from 1 to count allGraphics
						set aGraphic to item i of allGraphics
						set aLink to item link of aGraphic
						set aLinkName to name of aLink as string
						set originalList to originalList & aLinkName
					end repeat
					--returns {AG12345.tif, AG54321.tif, AG54321.tif}
					display dialog originalList as string
					
					
					--------------------------
					
					--Building two lists of every link that has at least 2 copies in the document
					set list1 to {}
					--display dialog aLinkName & "-" & matchCount
					repeat with x from 1 to count of items of originalList
						set n to item x of originalList
						--if n is in list2 and n is not in doubleImageList then set end of doubleImageList to n
						set matchCount to my count_matches(originalList, n)
						if matchCount is greater than 1 then
							set list1 to list1 & n
						end if
					end repeat
					--returns {AG54321.tif, AG54321.tif}
					
					set list2 to {}
					repeat with x from 1 to count of items of originalList
						set n to item x of originalList
						set matchCount to my count_matches(originalList, n)
						if matchCount is greater than 1 then
							set list2 to list2 & n
						end if
					end repeat
					--returns {AG54321.tif, AG54321.tif}
					
					--------------------------
					
					
					--Building final list which only contains the name of duplicate images once
					set doubleImageList to {}
					repeat with x from 1 to count of items of list1
						set n to item x of list1
						if n is in list2 and n is not in doubleImageList then set end of doubleImageList to n
					end repeat
					--returns {AG54321.tif}
					
					-----------------------------------------------------------------------------------------	
					
					
					
					
					-----------------------------------------------------------------------------------------	
					
					--ADDING A DROP SHADOW TO ALL PRODUCT IMAGES THAT DON'T ALREADY HAVE ONE
					
					set theGraphics to all graphics
					repeat with n from 1 to count theGraphics
						
						--Getting information about each image
						set theGraphic to item n of theGraphics
						set theLink to item link of theGraphic
						set theImage to parent of theLink
						set theFrame to parent of theImage
						set linkPath to file path of theLink as string
						set theLinkName to name of theLink as string
						
						--Adding the drop shadow only to images that are product images 
						if linkPath contains "1_4c IMAGES" then 
							
							--Ungrouping all the frames that contain product images
							repeat until (count of groups) = 0
								ungroup every group
							end repeat
							
							--Locking frames that contain images which are used twice
							if doubleImageList contains theLinkName then
								set locked of theFrame to true
							end if
							
							--Getting coordinates of each side of the frame 
							--Geometric bounds is 
							--{top of frame, left side of frame, bottom of frame, right side of frame} 
							--or {item 1, item 2, item 3, item 4}
							tell theFrame
								set topOfFrame to item 1 of geometric bounds
								set leftSideOfFrame to item 2 of geometric bounds
								set bottomOfFrame to item 3 of geometric bounds
								set rightSideOfFrame to item 4 of geometric bounds
								set frameHeight to bottomOfFrame - topOfFrame as number --Getting height to be used later on in removing short frames with drop shadows
								set offsetTopOfFrame to bottomOfFrame - 0.08 --This is the coordinate for the top of the duplicated frame that contains the drop shadow. We want it to be only slightly above the bottom of the frame.
							end tell
							
							--Duplicating the product image frame and resizing so it is only .05" tall 
					
							if locked of theFrame is false then

								set duplicateFrame to duplicate theFrame
								set geometric bounds of duplicateFrame to {offsetTopOfFrame, leftSideOfFrame, bottomOfFrame, rightSideOfFrame} --resizing the frame so it is only 0.08" tall
								
								--Sending the image frame with the drop shadow back one step
								send backward duplicateFrame
								
								
								--Applying the drop shadow to the duplicated image frame
								tell duplicateFrame
									apply object style using dropShadowStyle
								end tell
								
							end if
						end if
					end repeat
					
					-----------------------------------------------------------------------------------------	
					
					
					
					
					-----------------------------------------------------------------------------------------	
					
					--SAVE AND CLOSE DOCUMENT
					close saving yes
					
					-----------------------------------------------------------------------------------------	
					
					
					
				end tell
			end repeat
		end tell
	end timeout
end open


--------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------------------

--HANDLER THAT COUNTS THE NUMBER OF TIMES AN ITEM IS IN THE LIST
on count_matches(this_list, this_item)
	set the match_counter to 0
	repeat with i from 1 to the count of this_list
		if item i of this_list is this_item then ¬
			set the match_counter to the match_counter + 1
	end repeat
	return the match_counter
end count_matches