1 返信 最新の回答 日時: Apr 1, 2005 10:39 AM ユーザー:(アドビフォーラム情報)

    【トピック紹介】XML タグまたは XML 要素を同じ文書で一度以上使用する

    Level 1
      【オリジナルトピック】 <br />http://www.adobeforums.com/cgi-bin/webx?128@@.3bb8be17<br /><br />【質問】 <br />AppleScript を使って InDesign で画像をインポートし、あるいはまたインポートした画像を選択しようとしてます。文書には画像の名前と場所を示すテキストがあります。例えば「Macintosh HD:Images:image_020340.pdf」となります。このテキストには特定のスタイルが与えられています。このスタイルのテキストを検索しても、その場所に画像フレームの挿 入、または画像の配置もできません。また画像がインポート済みでも、それを選択して切り取ってペーストする必要があります。ページ上の画像のフレームをカウントする方法はわかりましたが、それ ぞれを選択することができません。<br />よろしくお願いします。<br /><br />【回答】 <br />この両者(例えば、異なるデータのタグの複数インスタンスと同じデータの要素の複数インスタンス)を行うためのスクリプトを用意しました。<br /><br />---------以下は XML データファイルの例です。<br /><br /><students> <br />     <student> <br />          <name> <br />               <first>Utena</first> <br />               <last>Tenjou</last> <br />          </name> <br />          <id>001</id> <br />          <contact_id>002</contact_id> <br />     </student> <br />     <student> <br />          <name> <br />               <first>Anthy</first> <br />               <last>Himemiya</last> <br />          </name> <br />          <id>002</id> <br />          <contact_id>001</contact_id> <br />     </student> <br />     <student> <br />          <name> <br />               <first>Akio</first> <br />               <last>Ohtori</last> <br />          </name> <br />          <id>666</id> <br />          <contact_id></contact_id> <br />     </student> <br /></students><br /><br />---------以下がスクリプト自体です。<br /><br />//XMLExample.js <br />//An InDesign CS JavaScript <br />// <br />//Create an example document <br />main(); <br />function main(){ <br />     var myStudent, myStoryElement, myBounds, myTextFrame, myStory, myPage, myCounter; <br />     var myDocument = app.documents.add(); <br />     myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; <br />     myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; <br />     myDocument.documentPreferences.pageHeight = 144; <br />     myDocument.documentPreferences.pageWidth = 252; <br />     myDocument.documentPreferences.facingPages = false; <br />     myDocument.masterSpreads.item(0).pages.item(0).remove(); <br />     with(myDocument.masterSpreads.item(0).pages.item(0).marginPreferences){ <br />         left = 12; <br />         right = 12; <br />         top = 12; <br />         bottom = 12; <br />     } <br />     //Create styles <br />     var myFirstNameStyle = myDocument.characterStyles.add({name:"first"}); <br />     var myLastNameStyle = myDocument.characterStyles.add({name:"last"}); <br />     var myIDStyle = myDocument.characterStyles.add({name:"id"}); <br />     //Import xml data file <br />     var myRootElement = myDocument.xmlElements.item(0); <br />     //You'll need to fill in your own file path. <br />     myRootElement.importXML(File("/c/students.xml")); <br />     //Set up tag to style mapping <br />     myDocument.xmlExportMaps.add("name", "name"); <br />     myDocument.xmlExportMaps.add("first", "first"); <br />     myDocument.xmlExportMaps.add("last", "last"); <br />     myDocument.xmlExportMaps.add("id", "id"); <br />     var myStudents = myRootElement.xmlElements.item("students"); <br />     myDocument.documentPreferences.pagesPerDocument = myStudents.xmlElements.length; <br />     for(myCounter = 0; myCounter < myStudents.xmlElements.length; myCounter ++){ <br />          myPage = myDocument.pages.item(myCounter); <br />          myPage.appliedMaster = myDocument.masterSpreads.item(0); <br />          myBounds = myGetBounds(myDocument, myPage); <br />          myTextFrame = myPage.textFrames.add(undefined, undefined, undefined, {geometricBounds:myBounds}); <br />          myStory = myTextFrame.parentStory; <br />          myStory.contents = "Ohtori Academy¥rStudent Identification¥rName: first last¥rID: id¥rContact: last, first" <br />          myStory.paragraphs.item(0).justification = Justification.centerAlign; <br />          myStory.pointSize = 12; <br />          myStory.spaceAfter = 0; <br />          myStory.paragraphs.item(1).justification = Justification.centerAlign; <br />          myStory.paragraphs.item(1).spaceAfter = 24; <br />          myStory.paragraphs.item(2).justification = Justification.leftAlign; <br />          myStory.paragraphs.item(2).leftIndent = 24; <br />          myStory.paragraphs.item(3).justification = Justification.leftAlign; <br />          myStory.paragraphs.item(3).leftIndent = 24; <br />          myStory.paragraphs.item(2).words.item(1).applyStyle(myDocument.characterStyle s.item("first")); <br />          myStory.paragraphs.item(2).words.item(2).applyStyle(myDocument.characterStyle s.item("last")); <br />          myStory.paragraphs.item(3).words.item(1).applyStyle(myDocument.characterStyle s.item("id")); <br />          myStory.paragraphs.item(4).leftIndent = 24; <br />          //If we format the second word of the last paragraph, the comma is also formatted. <br />          //We don't want that to happen, so we use itemByRange to format the word "last". <br />          myStartCharacter = myStory.paragraphs.item(4).words.item(1).characters.item(0); <br />          myEndCharacter = myStory.paragraphs.item(4).words.item(1).characters.item(-2); <br />          myStory.paragraphs.item(4).texts.itemByRange(myStartCharacter, myEndCharacter).applyStyle(myDocument.characterStyles.item("last")); <br />          myStory.paragraphs.item(4).words.item(2).applyStyle(myDocument.characterStyle s.item("first")); <br />     } <br />     //Mark up the document according to the style to tag mapping. <br />     myDocument.autoTag(); <br />     //Now move the data from the "clean" xml to the marked up stories. <br />     for(myCounter = 0;myCounter < myStudents.xmlElements.length; myCounter++){ <br />          myStudent = myStudents.xmlElements.item(myCounter); <br />          myStoryElement = myDocument.xmlElements.item(0).xmlElements.item(myCounter + 1); <br />          myStoryElement.xmlElements.item(0).contents = myStudent.xmlElements.item("name").xmlElements.item("first").contents; <br />          myStoryElement.xmlElements.item(1).contents = myStudent.xmlElements.item("name").xmlElements.item("last").contents; <br />          myStoryElement.xmlElements.item("id").contents = myStudent.xmlElements.item("id").contents; <br />          //Look up the first and last name of the student whose ID = contact_id. <br />          myContact = myFindContact(myStudents, myStudent.xmlElements.item("contact_id")); <br />          if(myContact != null){ <br />               myStoryElement.xmlElements.item(3).contents = myContact.xmlElements.item("name").xmlElements.item("last").contents; <br />               myStoryElement.xmlElements.item(4).contents = myContact.xmlElements.item("name").xmlElements.item("first").contents; <br />          } <br />          else{ <br />               myStoryElement.xmlElements.item(3).contents = "none"; <br />               myStoryElement.xmlElements.item(4).contents = ""; <br />               //Remove the comma. <br />               myStory.paragraphs.item(4).search(",", false, false, ""); <br />          } <br />     } <br />} <br />function myFindContact(myStudents, myID){ <br />     myOuterLoop: <br />     for(var myCounter = 0;myCounter < myStudents.xmlElements.length; myCounter++){ <br />          if(myStudents.xmlElements.item(myCounter).xmlElements.item("id").contents == myID.contents){ <br />               return myStudents.xmlElements.item(myCounter); <br />               break myOuterLoop; <br />          } <br />     } <br />     return null; <br />} <br />function myGetBounds(myDocument, myPage){ <br />     var myPageWidth = myDocument.documentPreferences.pageWidth; <br />     var myPageHeight = myDocument.documentPreferences.pageHeight; <br />     with(myPage.marginPreferences){ <br />          var myX1 = left; <br />          var myY1 = top; <br />          var myX2 = right; <br />          var myY2 = bottom; <br />     } <br />     myX2 = myPageWidth - myX2; <br />     myY2 = myPageHeight - myY2; <br />     return [myY1, myX1, myY2, myX2]; <br />}<br /><br />【追加情報】<br />上記のサンプルではオリジナルの XML 要素自体はテキスト内に配置されませんが、そのかわり選択的にマークアップされたテキストコンテナに移動されます。この方法の利点は XML データが他のテキストと分けられたままになることと他のコンテキストでも XML 要素の繰り返し/再利用が自由にできると思います。<br />なお、XML「要素」のテキストは一度しか表示されません。これは InDesign における XML のインポートについての制限です。<br /><br />------------<br /><A HREF="http://forums.adobe.co.jp/cgi-bin/webx?50@@.26b11940">【アドビ米国ユーザフォーラム情報紹介について】</A>
        • 1. Re: 【トピック紹介】XML タグまたは XML 要素を同じ文書で一度以上使用する
          Level 1
          【回答】 <br />この両者(例えば、異なるデータのタグの複数インスタンスと同じデータの要素の複数インスタンス)を行うためのスクリプトを用意しました。<br /><br />---------以下は XML データファイルの例です。<br /><br /><students> <br />     <student> <br />          <name> <br />               <first>Utena</first> <br />               <last>Tenjou</last> <br />          </name> <br />          <id>001</id> <br />          <contact_id>002</contact_id> <br />     </student> <br />     <student> <br />          <name> <br />               <first>Anthy</first> <br />               <last>Himemiya</last> <br />          </name> <br />          <id>002</id> <br />          <contact_id>001</contact_id> <br />     </student> <br />     <student> <br />          <name> <br />               <first>Akio</first> <br />               <last>Ohtori</last> <br />          </name> <br />          <id>666</id> <br />          <contact_id></contact_id> <br />     </student> <br /></students><br /><br />---------以下がスクリプト自体です。<br /><br />//XMLExample.js <br />//An InDesign CS JavaScript <br />// <br />//Create an example document <br />main(); <br />function main(){ <br />     var myStudent, myStoryElement, myBounds, myTextFrame, myStory, myPage, myCounter; <br />     var myDocument = app.documents.add(); <br />     myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points; <br />     myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points; <br />     myDocument.documentPreferences.pageHeight = 144; <br />     myDocument.documentPreferences.pageWidth = 252; <br />     myDocument.documentPreferences.facingPages = false; <br />     myDocument.masterSpreads.item(0).pages.item(0).remove(); <br />     with(myDocument.masterSpreads.item(0).pages.item(0).marginPreferences){ <br />         left = 12; <br />         right = 12; <br />         top = 12; <br />         bottom = 12; <br />     } <br />     //Create styles <br />     var myFirstNameStyle = myDocument.characterStyles.add({name:"first"}); <br />     var myLastNameStyle = myDocument.characterStyles.add({name:"last"}); <br />     var myIDStyle = myDocument.characterStyles.add({name:"id"}); <br />     //Import xml data file <br />     var myRootElement = myDocument.xmlElements.item(0); <br />     //You'll need to fill in your own file path. <br />     myRootElement.importXML(File("/c/students.xml")); <br />     //Set up tag to style mapping <br />     myDocument.xmlExportMaps.add("name", "name"); <br />     myDocument.xmlExportMaps.add("first", "first"); <br />     myDocument.xmlExportMaps.add("last", "last"); <br />     myDocument.xmlExportMaps.add("id", "id"); <br />     var myStudents = myRootElement.xmlElements.item("students"); <br />     myDocument.documentPreferences.pagesPerDocument = myStudents.xmlElements.length; <br />     for(myCounter = 0; myCounter < myStudents.xmlElements.length; myCounter ++){ <br />          myPage = myDocument.pages.item(myCounter); <br />          myPage.appliedMaster = myDocument.masterSpreads.item(0); <br />          myBounds = myGetBounds(myDocument, myPage); <br />          myTextFrame = myPage.textFrames.add(undefined, undefined, undefined, {geometricBounds:myBounds}); <br />          myStory = myTextFrame.parentStory; <br />          myStory.contents = "Ohtori Academy¥rStudent Identification¥rName: first last¥rID: id¥rContact: last, first" <br />          myStory.paragraphs.item(0).justification = Justification.centerAlign; <br />          myStory.pointSize = 12; <br />          myStory.spaceAfter = 0; <br />          myStory.paragraphs.item(1).justification = Justification.centerAlign; <br />          myStory.paragraphs.item(1).spaceAfter = 24; <br />          myStory.paragraphs.item(2).justification = Justification.leftAlign; <br />          myStory.paragraphs.item(2).leftIndent = 24; <br />          myStory.paragraphs.item(3).justification = Justification.leftAlign; <br />          myStory.paragraphs.item(3).leftIndent = 24; <br />          myStory.paragraphs.item(2).words.item(1).applyStyle(myDocument.characterStyle s.item("first")); <br />          myStory.paragraphs.item(2).words.item(2).applyStyle(myDocument.characterStyle s.item("last")); <br />          myStory.paragraphs.item(3).words.item(1).applyStyle(myDocument.characterStyle s.item("id")); <br />          myStory.paragraphs.item(4).leftIndent = 24; <br />          //If we format the second word of the last paragraph, the comma is also formatted. <br />          //We don't want that to happen, so we use itemByRange to format the word "last". <br />          myStartCharacter = myStory.paragraphs.item(4).words.item(1).characters.item(0); <br />          myEndCharacter = myStory.paragraphs.item(4).words.item(1).characters.item(-2); <br />          myStory.paragraphs.item(4).texts.itemByRange(myStartCharacter, myEndCharacter).applyStyle(myDocument.characterStyles.item("last")); <br />          myStory.paragraphs.item(4).words.item(2).applyStyle(myDocument.characterStyle s.item("first")); <br />     } <br />     //Mark up the document according to the style to tag mapping. <br />     myDocument.autoTag(); <br />     //Now move the data from the "clean" xml to the marked up stories. <br />     for(myCounter = 0;myCounter < myStudents.xmlElements.length; myCounter++){ <br />          myStudent = myStudents.xmlElements.item(myCounter); <br />          myStoryElement = myDocument.xmlElements.item(0).xmlElements.item(myCounter + 1); <br />          myStoryElement.xmlElements.item(0).contents = myStudent.xmlElements.item("name").xmlElements.item("first").contents; <br />          myStoryElement.xmlElements.item(1).contents = myStudent.xmlElements.item("name").xmlElements.item("last").contents; <br />          myStoryElement.xmlElements.item("id").contents = myStudent.xmlElements.item("id").contents; <br />          //Look up the first and last name of the student whose ID = contact_id. <br />          myContact = myFindContact(myStudents, myStudent.xmlElements.item("contact_id")); <br />          if(myContact != null){ <br />               myStoryElement.xmlElements.item(3).contents = myContact.xmlElements.item("name").xmlElements.item("last").contents; <br />               myStoryElement.xmlElements.item(4).contents = myContact.xmlElements.item("name").xmlElements.item("first").contents; <br />          } <br />          else{ <br />               myStoryElement.xmlElements.item(3).contents = "none"; <br />               myStoryElement.xmlElements.item(4).contents = ""; <br />               //Remove the comma. <br />               myStory.paragraphs.item(4).search(",", false, false, ""); <br />          } <br />     } <br />} <br />function myFindContact(myStudents, myID){ <br />     myOuterLoop: <br />     for(var myCounter = 0;myCounter < myStudents.xmlElements.length; myCounter++){ <br />          if(myStudents.xmlElements.item(myCounter).xmlElements.item("id").contents == myID.contents){ <br />               return myStudents.xmlElements.item(myCounter); <br />               break myOuterLoop; <br />          } <br />     } <br />     return null; <br />} <br />function myGetBounds(myDocument, myPage){ <br />     var myPageWidth = myDocument.documentPreferences.pageWidth; <br />     var myPageHeight = myDocument.documentPreferences.pageHeight; <br />     with(myPage.marginPreferences){ <br />          var myX1 = left; <br />          var myY1 = top; <br />          var myX2 = right; <br />          var myY2 = bottom; <br />     } <br />     myX2 = myPageWidth - myX2; <br />     myY2 = myPageHeight - myY2; <br />     return [myY1, myX1, myY2, myX2]; <br />}