Article View: adobe.scripting.indesign
Article #20926Re: Insert soft return (newby)
From: Olav Kvern
Date: Fri, 24 May 2002 20:42
Date: Fri, 24 May 2002 20:42
19 lines
7164 bytes
7164 bytes
Jay--
Apart from the Guide and the example scripts, this forum, right here, is the best place to pick up the basics. Just ask questions!<g>
I disagree with Dave (must be a first!)--I don't think you need to use tables for this task. I think it's perfectly suited to the original approach you had in mind--adding a line end character when the address spans more than one line.
So I cooked up an example for you. It shows a bunch of stuff, but the part you're probably most interested in are the "repeat" loops following the two "search" events. Take a look at what I did there, and I think you'll see how it can be applied to your text.
--Format telephone listings. --Takes a simple tab-delimited story and converts it to a formatted telephone listing. --Create dummy tab delimited phone listings. set myString to "Lain Iwakura" & tab & "The Wired" & tab & "206.555.1212" & return set myString to myString & "Olav Kvern" & tab & "801 N. 34th, Seattle, WA 98103" & tab & "206.675.7560" & return set myString to myString & "Edgar Allen Poe" & tab & "Rue Morgue, Paris" & tab & "206.555.1212" & return set myString to myString & "Angela Carter" & tab & "Sadly, no longer with us" & tab & "206.555.1212" & return set myString to myString & "Alasdair Gray" & tab & "Probably Glasgow" & tab & "206.555.1212" & return set myString to myString & "Shadow Lady" & tab & "Gray City" & tab & "206.555.1212" & return set myString to myString & "Utena Tenjou" & tab & "Inside room, old haunted dormitory #3, Ohtori Academy" & tab & "011.81.3.35740.2400" & return set myString to myString & "Ruri Hoshino" & tab & "Nadesico" & tab & "206.555.1212" & return tell application "InDesign 2.0" --Clear InDesign's find/change preferences set find preferences to nothing set change preferences to nothing set myDocument to make document tell myDocument --Create dummy character styles. In a "real world" example, you'd fill in the character style formatting here. set myCharStyle to make character style with properties {name:"name"} set myCharStyle to make character style with properties {name:"address"} set myCharStyle to make character style with properties {name:"phone"} set myPage to page 1 set myBounds to my myGetBounds(myPage, myDocument) --Change the width to a narrower column. set item 4 of myBounds to "20p" tell myPage set myTextFrame to make text frame with properties {geometric bounds:myBounds, contents:myString} my myFormatText(parent story of myTextFrame) --Okay, now we want to find addresses that span multiple lines. set myFoundItems to search story 1 of myDocument with find attributes {applied character style:character style "address" of myDocument} repeat with myCounter from (count myFoundItems) to 1 by -1 set myFoundItem to item myCounter of myFoundItems set myFirstBaseline to baseline of character 1 of myFoundItem set myLastBaseline to baseline of character -1 of myFoundItem set myParaEndBaseline to baseline of character -2 of paragraph 1 of myFoundItem --Next, test the found items. Compare the first baseline of the found item to the last --baseline of the found item, and also compare the baseline of the first found item to the --baseline of the last character in the paragraph. If the baseline values are not equal, add --a line break before the first character of the address. if myFirstBaseline is not equal to myLastBaseline � or myFirstBaseline is not equal to myParaEndBaseline then set contents of insertion point 1 of myFoundItem to forced line break end if end repeat --Clear InDesign's find/change preferences set find preferences to nothing set change preferences to nothing --Next, search for phone numbers that have ended up on a line by themselves. set myFoundItems to search story 1 of myDocument with find attributes {find text:"^y"} repeat with myCounter from (count myFoundItems) to 1 by -1 set myFoundItem to item myCounter of myFoundItems set myFirstBaseline to baseline of character 1 of myFoundItem set myLastBaseline to baseline of character -1 of myFoundItem if myFirstBaseline is not equal to myLastBaseline then set contents of insertion point 1 of myFoundItem to forced line break end if end repeat --Clear InDesign's find/change preferences set find preferences to nothing set change preferences to nothing end tell end tell end tell
on myFormatText(myStory) set myOldDelimiters to text item delimiters set text item delimiters to tab tell application "InDesign 2.0" set myDocument to parent of myStory repeat with myCounter from 1 to (count paragraphs in myStory) set myParagraph to paragraph myCounter of myStory set myTextItems to every text item of myParagraph set contents of paragraph myCounter of myStory to item 1 of myTextItems & return set applied character style of paragraph myCounter of myStory to character style "name" of myDocument set contents of insertion point -2 of paragraph myCounter of myStory to " " set applied character style of insertion point -2 of paragraph myCounter of myStory to character style "address" of myDocument set contents of insertion point -2 of paragraph myCounter of myStory to item 2 of myTextItems set contents of insertion point -2 of paragraph myCounter of myStory to right indent tab set applied character style of insertion point -2 of paragraph myCounter of myStory to character style "phone" of myDocument set contents of insertion point -2 of paragraph myCounter of myStory to item 3 of myTextItems --delete the extra return tell character -1 of paragraph myCounter of myStory to delete end repeat end tell end myFormatText
on myGetBounds(myPage, myDocument) tell application "InDesign 2.0" set myPageWidth to page width of document preferences of myDocument set myPageHeight to page height of document preferences of myDocument set myPageMargins to margin preferences of myPage set myX1 to margin left of myPageMargins set myY1 to margin top of myPageMargins set myX2 to myPageWidth - (margin right of myPageMargins) set myY2 to myPageHeight - (margin bottom of myPageMargins) return {myY1, myX1, myY2, myX2} end tell end myGetBounds
Hope this helps!
Thanks,
Ole
Technical Details
Message-ID:
<ef8b754.6@WebX.aaQKaf7vad4>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!priapus.visi.com!news-out.visi.com!hermes.visi.com!newsfeed.esat.net!newsfeed.icl.net!news.algonet.se!algonet!news000.worldonline.se!fr.clara.net!heighliner.fr.clara.net!213.73.101.75.MISMATCH!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebXReferences:
<ef8b754.-1@WebX.aaQKaf7vad4>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: f939a,4c5b67e22acd13de X-Google-Attributes: gidf939a,public X-Google-ArrivalTime: 2002-05-24 14:07:35 PST From: "Olav Kvern" <okvern@adobe.com> Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!supernews.com!priapus.visi.com!news-out.visi.com!hermes.visi.com!newsfeed.esat.net!newsfeed.icl.net!news.algonet.se!algonet!news000.worldonline.se!fr.clara.net!heighliner.fr.clara.net!213.73.101.75.MISMATCH!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!WebX Newsgroups: adobe.scripting.indesign Subject: Re: Insert soft return (newby) Message-ID: <ef8b754.6@WebX.aaQKaf7vad4> Date: Fri, 24 May 2002 13:42:04 -0700 References: <ef8b754.-1@WebX.aaQKaf7vad4> Lines: 151 MIME-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 8bit