Indesign CS - script for naming links (IMAGES)

Hi,
I have a simple script to create a box with image name, however it always shows in the corner of an image, is there a way to make it so it will show in the middle (center) of the image, and adjust text box to size of image name? Please let me know if anyone knows, it will be so helpful and time saving for me Thank you… here is a script I have so far:

tell application “InDesign CS”
tell document 1
tell view preferences
set horizontal measurement units to points
set vertical measurement units to points
end tell
set myColor to swatch “Paper”
try
set labelStyle to character style “Black”
on error
set labelStyle to make character style with properties {name:“Black”, applied font:“Helvetica”, font style:“Medium Condensed”, point size:13, fill color:“Black”} – change font or font style to suit your needs.
end try
–myLinksName is the name of the layer containing the links tags.
set myLinksName to “Links Names”
try
–Get the layer on which to place the links tags.
set myLayer to layer myLinksName
on error
–Create the layer if it didn’t already exist.
set myLayer to make layer at beginning with properties {name:myLinksName}
end try
repeat with i from 1 to count of links
set imageClass to class of parent of link i
if imageClass is in {PDF, image, EPS} then
set picFrame to parent of parent of link i
set theBounds to geometric bounds of picFrame
set theBounds to {item 1 of theBounds, item 2 of theBounds, (item 1 of theBounds) + 15, (item 2 of theBounds) + 75}
repeat
set theClass to class of picFrame
if theClass is in {page, spread} then
set theTarget to picFrame
exit repeat
else if theClass is in {character, text, insertion point} then –
inline
set picFrame to parent text frame of character 1 of picFrame
else
set picFrame to parent of picFrame
end if
end repeat
set labelFrame to make text frame at theTarget with properties {geometric bounds:theBounds, label:“Pic label”, contents:{name of link i} as text, fill color:myColor, item layer:myLayer, text wrap preferences:«constant tilpnone»}
set properties of text frame preferences of labelFrame to {inset spacing:{“0p1”, “0p1”, “0p2.5”, “0p1”}, vertical justification:«constant cnalcent», ignore wrap:true}

		end if
	end repeat
end tell
beep
with timeout of 30 seconds
	display dialog "Finished." buttons {"OK"} default button 1
end timeout

end tell

Do you mean something like this:

tell application “InDesign CS”
tell document 1
set justification of text of text frame “Pic label” to center align
end tell
end tell

You have to change the justification of the text OF the text frame, not the text frame itself. (Makes sense, although not immediately.)

As for your other question of making the label box wide enough for the text, you could try getting how many characters are in the text frame, multiplying that by the average width of a character (about 5.5 points) and then use that as the box width instead of the static 75. Like so:

tell application “InDesign CS”
tell document 1
–snip
set linkName to name of link i
set linkNameLen to length of linkName
set theBounds to geometric bounds of picFrame
set theBounds to {item 1 of theBounds, item 2 of theBounds, (item 1 of theBounds) + 15, (item 2 of theBounds) + 5.5 * linkNameLen}
end tell
end tell

InDesign has so much AppleScript support that it’s darn near impossible not to do something. Of course, their use of AppleScript terms (like property names and different children/parents) make it a bit of steep learning curve.

If you haven’t downloaded it yet, get the InDesign CS Scripting Guide here:
http://www.adobe.com/products/indesign/pdfs/indesign_cs_scripting_guide.pdf

Malcom

There’s no need to do that – just get the “horizontal offset” of the first and last insertions points, and subtract them.


Shane Stanley

killswitch:
Thank you very much it worked great!!! box size adjusted just fine, and text was centered in the box, however I was looking for the text box itself to be put right on the center of the picture so it is not on top left corner.
I am using a lot of pictures in our catalog and sometimes it is too confusing to find what text box goes to what picture.
Anyway I am trying to find out how to do it too, and thank you for this tip it is great, you guys rock!

here is what I have so far HOWEVER, I cant figure out some problems, example sometimes text format is tottally different instead of helvetica size sometimes it picks random size or character, kind of weird, anyone ??? Also Killswitch also wrote a script to put it in the middle of an image , and I try that few times could figure out this one neither…If anyone can help to put it all togheter that would be awesome. Thank you

tell application “InDesign CS”
tell document 1
tell view preferences
set horizontal measurement units to points
set vertical measurement units to points
end tell
set myColor to swatch “Paper”
try
set labelStyle to character style “Black”
on error
set labelStyle to make character style with properties {name:“Black”, applied font:“Helvetica”, font style:“Medium Condensed”, point size:13, fill color:“Black”} – change font or font style to suit your needs.
end try
–myLinksName is the name of the layer containing the links tags.
set myLinksName to “Links Names”
try
–Get the layer on which to place the links tags.
set myLayer to layer myLinksName
on error
–Create the layer if it didn’t already exist.
set myLayer to make layer at beginning with properties {name:myLinksName}
end try
repeat with i from 1 to count of links
set imageClass to class of parent of link i
if imageClass is in {PDF, image, EPS} then
set picFrame to parent of parent of link i

			tell application "InDesign CS"
				tell document 1
					--snip 
					set linkName to name of link i
					set linkNameLen to length of linkName
					set theBounds to geometric bounds of picFrame
					set theBounds to {item 1 of theBounds, item 2 of theBounds, (item 1 of theBounds) + 15, (item 2 of theBounds) + 6.5 * linkNameLen}
				end tell
			end tell
			repeat
				set theClass to class of picFrame
				if theClass is in {page, spread} then
					set theTarget to picFrame
					exit repeat
				else if theClass is in {character, text, insertion point} then --
					inline
					set picFrame to parent text frame of character 1 of picFrame
				else
					set picFrame to parent of picFrame
				end if
			end repeat
			set labelFrame to make text frame at theTarget with properties {geometric bounds:theBounds, label:"Pic label", contents:{name of link i} as text, fill color:myColor, item layer:myLayer, text wrap preferences:«constant tilpnone»}
			set properties of text frame preferences of labelFrame to {inset spacing:{"0p1", "0p1", "0p2.5", "0p1"}, vertical justification:«constant cnalcent», ignore wrap:true}
			
			tell application "InDesign CS"
				tell document 1
					set justification of text of text frame "Pic label" to center align
				end tell
			end tell
			
			
			
			
			
		end if
	end repeat
end tell
beep
with timeout of 30 seconds
	display dialog "Finished." buttons {"OK"} default button 1
end timeout

end tell

and here is to put it in the middle,

tell application “InDesign CS”
tell document 1
set pBounds to geometric bounds of page item “picture box”
set picWidth to ((item 4 of pBounds) - (item 2 of pBounds))
set tBounds to geometric bounds of text frame “text box”
set textWidth to ((item 4 of tBounds) - (item 2 of tBounds))
set leftOffset to (picWidth / 2) - (textWidth / 2)

	set newTextBounds to {(item 1 of tBounds), ((item 2 of pBounds) + leftOffset), (item 3 of tBounds), ((item 2 of pBounds) + leftOffset + textWidth)}
	set geometric bounds of text frame "text box" to newTextBounds
end tell

end tell

Does anyone know how to change the main font? For some reason It does not change the font , and it uses font that was used last on the page…
I try a lot of things , but it seams like it does not let me to change it, like it is read only… Anyone had any experience with this?
Thanks