Back

If your email is not recognized and you believe it should be, please contact us.

  • You must be logged in to reply to this topic.Login

Script for aligning objects to a text character

Return to Member Forum

  • Author
    Posts
    • #67787

      Hi, I am new to scripting and was wondering if someone can tell me if this is even possible:

      Is there a way to create a script that will align an object to text or invisible marker?

      Basically, i have a document with hundreds of anchored objects (created using in-tools amazing SideHeads plugin), and now I would like to either center align each anchored object between it’s anchor marker and the previous one. Or, center align the anchored object to the word under under it – see screenshot.

      It sounds like something that can be scripted, but i am not even sure what the commands should be doing, I am not even sure how to do this manually using inDesign’s tools!

      Here is a screenshot of my page: https://dl.dropboxusercontent.com/u/1097176/sampletext.PNG
      (Highlighted in yellow is the two anchor points i am trying to center between, the red arrows show that each object is anchors to the anchor point to its left).

      Thanks!

    • #67859

      Ok, so i had my web programmer create this script for me, though he has no experience with inDesign scripting!

      The script looks for the X position of the word proceeding the text anchor and updates the anchored object offset to center it above the word.

      If anyone has any suggestions on simplifying the script or making it run faster, please let me know.

      Here is the script:

      main();

      function main(){
      //Make certain that user interaction (display of dialogs, etc.) is turned on.
      app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
      if(app.documents.length != 0){
      if(app.selection.length != 0){
      //Get the first item in the selection.
      var mySelection = app.selection[0];
      //Process the selection. If text or a text frame is
      //selected, do something; otherwise, do nothing.
      switch(mySelection.constructor.name){
      case “Text”:
      case “InsertionPoint”:
      case “Character”:
      case “Word”:
      case “Line”:
      case “TextStyleRange”:
      case “Paragraph”:
      case “TextColumn”:
      case “TextFrame”:
      var story = mySelection.parentStory;
      processStory(story);
      break;
      default:
      alert(“Please select some text or a text frame and try again.”);
      }
      }
      else{
      alert(“Please select some text or a text frame and try again.”);
      }
      }
      else{
      alert(“Please open a document and try again.”);
      }
      return ‘done’;
      }

      function processStory(story) {
      story.recompose()
      for (var iLine = 0; iLine < story.lines.length; iLine++) {
      //if (iLine > 0) break;
      var sLine = story.lines[iLine];
      //alert(sLine.characters.length);
      sLine.showText();
      processLine(sLine);
      //$.writeln(sLine.characters.length);
      }
      }

      function processLine(sLine) {
      var iStart = 0, iEnd = 0, bGetStart = true, lEndOffset = 0;
      var reIgnore = new RegExp(‘[ ,\.: ]’);
      for (var iChar = 0; iChar < sLine.characters.length; iChar++) {
      //if (iChar > 10) { break; }
      var c = sLine.characters[iChar];
      if (reIgnore.test(c.contents)) {
      if (bGetStart) {
      //do nothing, just ignore character
      } else {
      // add to offset
      lEndOffset-= (c.endHorizontalOffset – c.horizontalOffset); //using – because we are going right to left and the endHorizontalOffset is less than the horizontalOffset
      }
      } else {
      if (bGetStart) {
      iStart = c.horizontalOffset;
      bGetStart = false;
      }
      //$.writeln(c.endHorizontalOffset);
      // $.writeln(iChar + ‘: ‘ + c.contents);
      if (c.pageItems.length>0) {
      var pi = c.pageItems[0];
      if (pi.hasOwnProperty(‘anchoredObjectSettings’)) { //found anchor character
      pi.anchoredObjectSettings.anchorXoffset = lEndOffset + ((iStart-iEnd)/2)
      bGetStart = true;
      lEndOffset = 0;
      }
      } else {
      iEnd = c.endHorizontalOffset;
      lEndOffset = 0; //reset offset if we find a regular character after an ignore character before the next anchor
      }
      }
      }
      }

Viewing 1 reply thread
  • You must be logged in to reply to this topic.
Forum Ads