Is there a way to get InDesign to give me the X/Y coordinates on a page of a selected character in a text frame, or the X/Yposition of the cursor before or after that character in the text frame?
I’m trying to create a rule to point from a text slug to the first character of a text frame whose fill color is different from the beginning character.
I’ve written a script that calls out every font and text color, every image, it’s name and color space, and every eps and its color makeup. Now I’m fine-tuning it, and instead of one slug pointing to the text frame in general and calling out all the fill colors, I would like to create a seperate slug for each text color, pointing to the text specified.
I know I can get the X/Y position of the text frame, but how do I get a character in the text frame.
I’m not 100%, but I don’t believe that text objects have obtainable X, Y coordinates; you could probably calculate a position from the offset of line numbers and their point sizes using the frame’s geometric bounds. It would be considerably easier to insert a dingbat arrow in front of the text.
If I recall right from working on something similar a while ago you need the offset of the character (I believe that you also need to add in the x position of the text frame since the offset is relative to the text frame) and the baseline (I remember this returning the baseline relative to the page). This will get you the lower left of the character. You can calculate the rest with the character height (or ascender) and width and descender.
A place to start is a script from the InDesign 2.0 script samples set:
--BoxCharacters
--An InDesign 2.0 script.
--For more on InDesign scripting, go to
--http://www.adobe.com/products/indesign/scripting.html
--
--This script draws boxes around all characters tagged with a specific character style. You can use it to create a "highlight" effect.
--To use this script, apply a character style to a range of text, then run the script and enter the
--name of the character style you applied. InDesign will draw a box behind each character tagged
--with the character style. This script demonstrates the techniques you use to get
--the page coordinates of text objects.
set myAnswer to display dialog "Enter the name of a character style." default answer "myBoxCharacter"
if button returned of myAnswer is not "Cancel" then
set myStyleName to text returned of myAnswer
end if
set myFoundItems to {}
tell application "Adobe InDesign CS2"
try
set myErrorMessage to "Please open a document and try again."
set myDocument to active document
if horizontal measurement units of view preferences of myDocument is not equal to points then
set myOldXUnits to horizontal measurement units of view preferences of myDocument
set horizontal measurement units of view preferences of myDocument to points
set myResetXUnits to true
else
set myResetXUnits to false
end if
if vertical measurement units of view preferences of myDocument is not equal to points then
set myOldYUnits to vertical measurement units of view preferences of myDocument
set vertical measurement units of view preferences of myDocument to points
set myResetYUnits to true
else
set myResetYUnits to false
end if
try
set myLayer to layer "myCharacterBoxLayer" of myDocument
on error
tell myDocument
set myLayer to make layer with properties {name:"myCharacterBoxLayer", layer color:yellow}
«event Qdrwmove» myLayer given «class insh»:end
end tell
end try
try
set myCharacterBoxColor to color "myCharacterBoxColor" of myDocument
on error
tell myDocument to make color with properties {space:CMYK, color value:{0, 0, 100, 0}, name:"myCharacterBoxColor"}
end try
set find preferences to nothing
set change preferences to nothing
set myErrorMessage to "The character style name you entered does not appear in the document. Please enter another style name and try again."
set applied character style of find preferences to character style myStyleName of myDocument
set myErrorMessage to "No text was found."
set myFoundItems to search myDocument with find preferences
if (count myFoundItems) > 0 then
repeat with myFoundItem in myFoundItems
repeat with myCounter from 1 to (count characters of myFoundItem)
set myCharacter to object reference of character myCounter of myFoundItem
if overflows of text frame -1 of parent of myCharacter is true then
set myLastCharacter to index of character -1 of text frame -1 of parent of myCharacter
else
set myLastCharacter to index of character -1 of parent of myCharacter
end if
--Do not draw the box if the character is in overset text.
if index of myCharacter is less than or equal to myLastCharacter then
set myParent to parent of parent text frame of myCharacter
set myBaseline to baseline of insertion point 1 of myCharacter
if baseline of insertion point 2 of myCharacter is equal to baseline of myCharacter then
set myX2 to horizontal offset of insertion point 2 of myCharacter
set myX1 to horizontal offset of insertion point 1 of myCharacter
set myY1 to (baseline of myCharacter) - (ascent of myCharacter)
set myY2 to (baseline of myCharacter) + (descent of myCharacter)
tell myParent
set myRectangle to make rectangle with properties {geometric bounds:{myY1, myX1, myY2, myX2}, fill color:color "myCharacterBoxColor" of myDocument, stroke color:swatch "None" of myDocument}
end tell
end if
end if
end repeat
end repeat
end if
if myResetXUnits is true then
set horizontal measurement units of view preferences of myDocument to myOldXUnits
end if
if myResetYUnits is true then
set vertical measurement units of view preferences of myDocument to myOldYUnits
end if
on error
activate
display dialog myErrorMessage
end try
end tell
Note that the dictionary has changed since ID2 so this may not work as it did in ID2.
That’s so cool. Your script really helped me see the light.
Here is my solution, simple and sweet (granted, it’s just a bench test script without being hardwired to everything else) but it accomplishes what I needed. It draws a rule from the slug (in this case, I used the X/Y coordinates of 0,0 because no slug is plugged into the script) to the first character in the textbox that isn’t the same color as the beginning text. With a repeat script I can call out every color in this and every text frame, and assign a slug and arrowed rule to each color.
tell application "InDesign CS"
tell document 1
tell selection --select the text frame first
set thecolor to name of fill color of character 1
set totalCount to count every character
set theCount to count ((every character) whose name of fill color is not thecolor)
set X to horizontal offset of character (totalCount - theCount + 2) as string
set thisChar to insertion point (totalCount - theCount)
set Y to baseline of thisChar
--set theHue to object reference of (first character) whose name of fill color is "C=0 M=100 Y=0 K=0"
end tell
make new graphic line with properties {geometric bounds:{0, 0, Y, X}, stroke weight:1}
end tell
end tell