I am trying to figure out a way to add a hair space to both the beginning and end of a text selection in InDesign. I thought the easiest way would be to use text item delimiters, but I don’t know how to replace a character with a hair space? InDesign’s “Find/Change” associates ^| to being a hair space (if that helps?).
My intent is to create rules above a selected roman numeral, i.e., “VII”, I am using an underline and strike through to create the upper and bottom rules”which works great. But I want to add a little extra width to the edges of the “VII”. Unfortunately an extra space is too much. a hair space would be perfect.
Any ideas?
tell application "Adobe InDesign CS4"
activate
set theClass to class of selection
if theClass is in {text, word, paragraph} then
set txtRef to every character of selection
set theInsert to insertion point 1 of selection
set thePointSize to point size of the selection
set theDiv to thePointSize / 4
set underline of the selection to true
set strike thru of the selection to true
set properties of the selection to {applied font:"Trajan Pro", underline offset:0, strike through offset:thePointSize - theDiv}
delete selection
set theString to ("-" & txtRef & "-") as string
set AppleScript's text item delimiters to "-"
set theList to text items of theString -- remove the hyphen
--set AppleScript's text item delimiters to hair spce Š
set AppleScript's text item delimiters to " "
set theString to theList as string -- replace it with hair space
set AppleScript's text item delimiters to ""
set contents of theInsert to theString
end if
end tell
If you select a thin space in ID and look in the Info panel, you’ll see it’s character 0x2009, which is 8201 as a decimal. Assuming you’re running 10.5 or later, you can use:
You probably want to do it after the formatting, because it effectively changes what’s selected.
Yep, once again you pull through Shane! This is perfect.
I modified the script a little to pick up the previous font style and remove the underline and strike through. This script may come in handy about twice a year, but it is pretty cool
tell application "Adobe InDesign CS4"
activate
set theClass to class of selection
if theClass is in {text, word, paragraph} then
set theSel to selection
set txtRef to every character of selection
set theFont to applied font of selection
set theProps to properties of last character of selection
set theCount to count of characters of selection
set theInsert to insertion point 1 of selection
set thePointSize to point size of the selection
set theDiv to thePointSize / 4
set underline of the selection to true
set strike thru of the selection to true
set properties of the selection to {applied font:"Trajan Pro", underline offset:0, strike through offset:thePointSize - theDiv}
delete selection
set theThin to character id 8201
set contents of selection to theThin & txtRef & theThin
set properties of the selection to {applied font:theFont, underline:false, strike thru:false}
end if
end tell
Marc, this is very good to know. Many thanks.
Now How about this. What if I wanted to set those thin spaces to horizontal scale equal to 50%. Is there an easy way to apply this so that the selected text remains unchanged, but the “hair space” ends are scaled 50% ?
Get the offset of the first and last character before the change, adjust the last character offset for the additional characters and change the horizontal scale of the character at the first offset, which should now be the thin space, and the last character +2, which should be the ending thin space. You might consider having a character style set up for this adjustment and apply that so if you decide to change it to 60% later it can be done by adjusting the character style.
Mark, thanks I was looking for that in the dictionary but couldn’t find it.
Hi Jerome,
I am trying to follow what you are referring to.
Do I need to do something like this. (for just one character for now)
set theId1 to object reference of character 1 of selection
run script
then later I need add 1 to this object reference and then apply the properties to that character id.
Pardon my stupidity above because I know it isn’t right. I am not sure how to code for getting the position of a character in a text flow, assign it to a variable, and then later increase the id by 1 so I can target the new character added by the script.
I hope you can help, because this will be very useful for other scripts besides this one.
Your script reworked a bit with some redundancy and excessive calls to ID removed:
tell application "Adobe InDesign CS4"
activate
set theClass to class of selection
if theClass is in {text, word, paragraph} then
set theParent to parent story of selection
set txtRef to contents of selection
set theFont to applied font of selection
set theCount to count of characters of txtRef
set theInsert to index of insertion point 1 of selection
set thePointSize to point size of the selection
set theDiv to thePointSize / 4
set properties of the selection to {applied font:"Trajan Pro", underline:true, underline offset:0, strike thru:true, strike through offset:thePointSize - theDiv}
delete selection
set theThin to character id 8201
set contents of selection to theThin & txtRef & theThin
set properties of character (theInsert) of theParent to {horizontal scale:50}
set properties of character (theInsert + theCount + 1) of theParent to {horizontal scale:50}
end if
end tell
And this eliminates the delete/paste calls:
tell application "Adobe InDesign CS4"
activate
set theClass to class of selection
if theClass is in {text, word, paragraph} then
set theParent to parent story of selection
set txtRef to contents of selection
set theFont to applied font of selection
set theCount to count of characters of txtRef
set theInsert to index of insertion point 1 of selection
set thePointSize to point size of the selection
set theDiv to thePointSize / 4
set properties of the selection to {applied font:"Trajan Pro", underline:true, underline offset:0, strike thru:true, strike through offset:thePointSize - theDiv}
set theThin to character id 8201
copy {character id 8201, character id 8201} to {first insertion point of selection, last insertion point of selection}
set properties of character (theInsert) of theParent to {applied font:"Trajan Pro", underline:true, underline offset:0, strike thru:true, strike through offset:thePointSize - theDiv, horizontal scale:50}
set properties of character (theInsert + theCount + 1) of theParent to {horizontal scale:50}
end if
end tell
Note that I set all the properties of the first thin space but only the horizontal scale of the last one. This is because the when the first one is copied in it picks up the local styling of the space before the selection while the second thin space picks up the local style of the selection that was set earlier in the script.
You could probably increase the speed and eliminate some steps by creating two character styles and applying those instead of using local styles. This could be very useful if you later needed to change the properties of the styling.
Wow, I learned many new key concepts with this one simple script. For example, I needed to get the “index” of the insertion point (Thank you”very helpful). And I now know how to get character id’s of characters, and I also can utilize the copy aspect that I didn’t realize previously.
btw, yes, I could make a style. but this scipt will be shared by multiple machies, so that may not be very applicable.
I am very grateful for everybody’s kindness and willingness to help me out.
Thank you.
-Jeff
Here is the final script (sorry my ending lines to bring back the font characteristics without the underline/strike through are not the best)
tell application "Adobe InDesign CS2"
activate
set theClass to class of selection
if theClass is in {text, word, paragraph, character} then
set theParent to parent story of selection
set txtRef to contents of selection
set theScale to horizontal scale of character 1 of selection
set theFont to applied font of selection
set theCount to count of characters of txtRef
set theInsert to index of insertion point 1 of selection
set thePointSize to point size of the selection
set theDiv to thePointSize / 3.7
set properties of the selection to {applied font:"Helvetica Bold", underline:true, underline offset:0, strike thru:true, strike through offset:thePointSize - theDiv}
--set theThin to character id 8201
copy {character id 8201, character id 8201} to {first insertion point of selection, last insertion point of selection}
set properties of character (theInsert) of theParent to {applied font:"Helvetica Bold", underline:true, underline offset:0, strike thru:true, strike through offset:thePointSize - theDiv, horizontal scale:50}
set properties of character (theInsert + theCount + 1) of theParent to {horizontal scale:50}
--set theSpace to character id 32
copy {character id 32} to {last insertion point of selection}
set properties of character (theInsert + theCount + 2) of theParent to {applied font:theFont, underline:false, strike thru:false, horizontal scale:theScale}
delete last character of selection
else
tell application "SystemUIServer"
activate
display dialog "Did you have highlighted text?" buttons {"Cancel"} default button "Cancel" with icon 2
end tell
end if
end tell