2 Replies Latest reply on Jan 22, 2016 12:42 AM by lucas.maximus

    Search and replace text with a Metadata caption

    lucas.maximus Level 1

      I have a document with a placed image in the center. I want to create a metadata caption beneath it displaying the Author of the image. I have my text box set up touching the placed image with “Replace this text” temporarily.

       

      I choose

      Type > Text Variables > Define > New

       

      I enter the Name: Author, choose Type as Metadata Caption, and choose Author from the Metadata dropdown menu and hit OK so Author appears in my list of text Variables.

       

      I then highlight “Replace this text” and Insert the Author text Variable to replace it, so that the caption shows the Author of whatever image I import into that graphic frame.

       

      Anyway…..my question is - can I script this so that Javascript automatically searches the document for any instances of “Replace this text” and replaces it with a Author metadata caption?

       

       

      This is my attempt, I know this is probably way off but it might make it clear what I’m trying to do.

       

      ——————————————————————

       

      app.findTextPreferences = NothingEnum.nothing; 

      app.changeTextPreferences = NothingEnum.nothing; 

       

      app.findTextPreferences.findWhat = “Replace this text”; 

      app.changeTextPreferences.changeTo = myDocument.textVariableInstances.add({associatedTextVariable:”Author”}); 

       

      //this will show changes count 

      alert(app.activeDocument.changeText().length); 

       

      app.activeDocument.changeText(); 

       

      ————————————————————

       

       

      I'm sure its the 4th line down where I'm going wrong - Any help would be greatly appreciated!!

       

      Luke

        • 1. Re: Search and replace text with a Metadata caption
          Jump_Over Level 5

          Hi,

           

          Script should add new textVariableInstances for each found text separately (using a loop).

          For example:

          app.findTextPreferences = NothingEnum.nothing;
          app.changeTextPreferences = NothingEnum.nothing;
          
          app.findTextPreferences.findWhat = "Replace this text";
          var
               mDoc = app.activeDocument,
               mTextVar = mDoc.textVariables.item("Author"),
               mFound = mDoc.findText(),
               cText;
          
          if (mTextVar.isValid)
               while (cText = mFound.pop()) {
                    cText.contents = "";
                    cText.textVariableInstances.add(LocationOptions.AT_BEGINNING, {associatedTextVariable: mTextVar})
                    }
          

           

          textVariable should be created already.

           

          Jarek

          • 2. Re: Search and replace text with a Metadata caption
            lucas.maximus Level 1

            Thank you so much! It works perfectly!